Forum Topic: Securing $_post

(188 views • 13 replies)

This topic is 1 page long.

<< < > >>
Blushing

Gateau

Reply To Post Reply & Quote

Posted at: 4/27/09 09:13 PM

Gateau FAB LEVEL 21

Sign-Up: 07/09/05

Posts: 1,438

Hey guys!
I'm trying to make a simple crediting system, and I was wondering if there is a way to secure a post parameter. I know that if I use GET it would be harder for exploiters to trick the system, but I'm just trying to keep it simple for now (until I get the time to convert everything). Is there a way to make it so a php page can track where the form post came from? Example: If $_POST['this data']; came from test.php Then continue.

Interested? Probably not. It's okay, I wouldn't be either. Just PM me
Je parle Français, Ich spreche Deutsch! Don't worry, I speak English too!

BBS Signature

None

Thomas

Reply To Post Reply & Quote

Posted at: 4/27/09 09:40 PM

Thomas LIGHT LEVEL 13

Sign-Up: 02/14/05

Posts: 2,830

You can probably use the $_SERVER superglobal, specifically with 'HTTP_REFERER'.

//This variable will be the last page address
$lastPage = $_SERVER['HTTP_REFERER'];

//Use a condition to check the last page against the allowed page
if($lastPage != 'http://example.com/page.php') {
//False
}

You can probably easily break down the previous address so that you can have only the page name (just 'page.php').

Something like this:

<?
//Variable containing last page address
$lastPage = $_SERVER['HTTP_REFERER'];

//Split, or 'explode', the address into pieces every time there is a slash (/)
$ad_array = explode('/',$lastPage);

//Reverse the array so that the first value is whatever is past the last slash
$ad_array = array_reverse($ad_array);

//This variable SHOULD contain the page name of the last address (page.php, etc)
$pageName = $ad_array[0];

//Condition to check the last page name against what you want it to be
if($pageName != 'page.php') {
//False
}
?>

I didn't test this, it's just a guess, but I'm sure it will work.

You can experiment a bit more with that. More information on $_SERVER here:
http://us.php.net/reserved.variables.ser ver


Elated

Gateau

Reply To Post Reply & Quote

Posted at: 4/27/09 09:44 PM

Gateau FAB LEVEL 21

Sign-Up: 07/09/05

Posts: 1,438

Thank you so much Thomas! This is going to help tremendously.

Interested? Probably not. It's okay, I wouldn't be either. Just PM me
Je parle Français, Ich spreche Deutsch! Don't worry, I speak English too!

BBS Signature

None

Gateau

Reply To Post Reply & Quote

Posted at: 4/27/09 10:28 PM

Gateau FAB LEVEL 21

Sign-Up: 07/09/05

Posts: 1,438

Also, I might as well post my other problem in here too:

$q = mysql_query("UPDATE `alerts` SET notice = $notice WHERE notice != $currentNotice")
 or die(mysql_error());

I'm getting an error in my sql syntax, this is probably clear but can anyone spread some light? :P sorry.

Interested? Probably not. It's okay, I wouldn't be either. Just PM me
Je parle Français, Ich spreche Deutsch! Don't worry, I speak English too!

BBS Signature

None

Thomas

Reply To Post Reply & Quote

Posted at: 4/28/09 12:38 AM

Thomas LIGHT LEVEL 13

Sign-Up: 02/14/05

Posts: 2,830

$q = mysql_query("UPDATE `alerts` SET notice = $notice WHERE notice != $currentNotice")
 or die(mysql_error());

Well it's already obvious, you can't do 'WHERE obj != $var'.

If you are trying to update notices (I'm assuming after a form), then just do this:

mysql_query('UPDATE myTable SET col=\''.$var.'');

That should just update every 'notice' to '$currentNotice'. Most likely not the most efficient way, but it would work.

It would also help if you could make your goal a bit more clear. My thoughts on your question could be wrong :)


None

DFox

Reply To Post Reply & Quote

Posted at: 4/28/09 01:01 AM

DFox LIGHT LEVEL 30

Sign-Up: 08/09/03

Posts: 9,483

At 4/28/09 12:38 AM, Thomas wrote: $q = mysql_query("UPDATE `alerts` SET notice = $notice WHERE notice != $currentNotice")
or die(mysql_error());

Well it's already obvious, you can't do 'WHERE obj != $var'.

You can't?


None

Thomas

Reply To Post Reply & Quote

Posted at: 4/28/09 01:09 AM

Thomas LIGHT LEVEL 13

Sign-Up: 02/14/05

Posts: 2,830

At 4/28/09 01:01 AM, DFox wrote:
At 4/28/09 12:38 AM, Thomas wrote: $q = mysql_query("UPDATE `alerts` SET notice = $notice WHERE notice != $currentNotice")
or die(mysql_error());

Well it's already obvious, you can't do 'WHERE obj != $var'.
You can't?

You tell me. I assume not, but then again, I myself am pretty new to MySql.


None

Afro-Ninja

Reply To Post Reply & Quote

Posted at: 4/28/09 01:13 AM

Afro-Ninja EVIL LEVEL 38

Sign-Up: 03/02/02

Posts: 13,467

if notice is a string value then whatever value you set it to needs to be wrapped in single quotes

notice = '$notice'

BBS Signature

None

DFox

Reply To Post Reply & Quote

Posted at: 4/28/09 01:18 AM

DFox LIGHT LEVEL 30

Sign-Up: 08/09/03

Posts: 9,483

If notice is a string then his problems begin with poor database structuring...

But, if it's a number like his query suggests, then yes, a_field != $a_NUMBER_variable is perfectly valid.


None

Fruitpastles

Reply To Post Reply & Quote

Posted at: 4/28/09 01:59 AM

Fruitpastles NEUTRAL LEVEL 10

Sign-Up: 10/13/05

Posts: 91

Anything (or at least the majority of) the $_SERVER superglobal comes from the clients request, therefore its easy for a user to change their referrer value (meaning it's not secure).

There are other options, but it depends on how secure it needs to be. What exactly are you trying to do?


None

yhar

Reply To Post Reply & Quote

Posted at: 4/28/09 03:45 AM

yhar NEUTRAL LEVEL 03

Sign-Up: 04/02/08

Posts: 1,769

At 4/27/09 09:13 PM, Gateau wrote: I know that if I use GET it would be harder for exploiters to trick the system

You've got that backwards, GET is much less insecure than POST.

THIS IS CITRICSQUID POSTING


None

DFox

Reply To Post Reply & Quote

Posted at: 4/28/09 03:53 AM

DFox LIGHT LEVEL 30

Sign-Up: 08/09/03

Posts: 9,483

At 4/28/09 03:45 AM, yhar wrote:
At 4/27/09 09:13 PM, Gateau wrote: I know that if I use GET it would be harder for exploiters to trick the system
You've got that backwards, GET is much less insecure than POST.

There both equally "insecure" because you would never use either for something that couldn't be modified by the user.


None

DearonElensar

Reply To Post Reply & Quote

Posted at: 4/28/09 05:52 AM

DearonElensar LIGHT LEVEL 18

Sign-Up: 06/10/02

Posts: 1,731

For his problem it would be the easiest (and most secure) to just use sessions, tho depending on the rest of the setup it might be better to change the entire way it works :p

BBS Signature

Elated

Gateau

Reply To Post Reply & Quote

Posted at: 4/28/09 03:52 PM

Gateau FAB LEVEL 21

Sign-Up: 07/09/05

Posts: 1,438

At 4/28/09 05:52 AM, DearonElensar wrote: For his problem it would be the easiest (and most secure) to just use sessions, tho depending on the rest of the setup it might be better to change the entire way it works :p

I'm currently using session variables (for things like user accounts and other data), however I need a way to insert data into a database for specific admin functions (i.e making a notice). The admin accounts are able to access forms that users cannot, however they should be able to post that form to insert the data. Currently the form posts to a separate directory where all of the admin-related scripts are hosted, which is why I initially wanted the script to only accept data from specific pages.

Thank you all for your assistance and opinions with these minor problems; it's a relief for me to know that this board is such a reliable resource! For someone who is learning independently, communities such as this one are essential for minor tips to avoid similar problems in the future. All of your help is much appreciated, thank you for all of your input :).

Interested? Probably not. It's okay, I wouldn't be either. Just PM me
Je parle Français, Ich spreche Deutsch! Don't worry, I speak English too!

BBS Signature

All times are Eastern Standard Time (GMT -5) | Current Time: 06:52 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!