Jump to content

using a session "token"...


tefuzz

Recommended Posts

I am trying to use a session token to protect my form a little bit for hijacking, and from timeouts in case the user takes 4 hours to complete it, the information will be cleared so someone couldn't hijack their data either. i have it creating a token automatically, and passing it through $_POST inside a hidden input 'token'

 

I can't however get it to work...my code is this...seemed like it would work, but I always get "session error!". and yes, the hidden input for 'step1' is there.

 

<?php
session_start();

if (!isset($_POST['step1'])) { 
$_SESSION['token'] = uniqid(md5(microtime()), true); 
} elseif ($_POST['token'] !== $_SESSION['token']) { 
   echo "session error!";
} else {

...this is where the validation goes...

Link to comment
Share on other sites

Based on what I see there, since step1 is set, than your session "token" never gets its value set, and because of that, the next elseif check runs true every time (since that session isn't set, the post value of token cant equal nothing, assuming that that post value is also sent)

 

 

Link to comment
Share on other sites

Based on what I see there, since step1 is set, than your session "token" never gets its value set, and because of that, the next elseif check runs true every time (since that session isn't set, the post value of token cant equal nothing, assuming that that post value is also sent)

 

I have the first IF checking to see if they pressed the "next" button. If not, then it must be the first time on the page, so I am assigning a token. If it is pressed, then it is checking the token against the hidden input to see if they match. Or at least that what I thought it was doing. ;)

Link to comment
Share on other sites

OK, well I think I fixed one part of it...

 

I changed this line of my page:

 <form action="step1.php" method="post" enctype="multipart/form-data" name="tutorApp" class="tutorAppForm">

 

to this:

 <form action="<?php echo($_SERVER['PHP_SELF']); ?>" method="post" enctype="multipart/form-data" name="tutorApp" class="tutorAppForm">

 

Link to comment
Share on other sites

sorry about the long reply, i was gone for a little bit. Ok i didn't know that you were on that page twice, so yeah that if statement would work in that case.

 

And the change you made, technically shouldn't really matter, since both point to the same location (which is step1.php I assume)

 

try to use the != comparison, instead of the !==. the latter may be making your test run true for some reason (maybe they aren't the same type, or there is some other issue making them not "identical")

Link to comment
Share on other sites

ok, now I have another problem.  I have the form validation set so that if there are errors it automatically fills in the valid fields again so the user doesn't need to retype everything for 1 error. However, If i run the script with an error, it fills in my valid data just like I want. Now the catch is, If I leave the page and go to say google.com, and go back, my form fields will still be filled in with that data.

 

What can I do to make sure that if they leave the "session" it clears, and they have to start over again?  Only if they leave the form (multi part) and come back. If they go to step2 and come back to step1, I want to keep the data.

Link to comment
Share on other sites

Maybe this will be of any help?

 

http://www.phpfreaks.com/tutorial/php-security/page8

 

took a look at that, but the page you linked to shows about having some malicious code within a page. when pulled up by a user with permissions it executes. should I be using something like $_SERVER['HTTP_REFERER'] to check? basically, if the token is set, but the referer is not my site, i can show them a session error or something?

 

but looking at the PHP manula it says HTTP_REFERER "...This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted..."

 

any ideas?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.