Jump to content

Recommended Posts

okay, I'm having a strange problem with setting a session variable.

I've got a form which sets the session variable, so I can then check it against the user input. For some reason though it's setting the variable twice (setting it, then setting it again with a different value), but it doesn't show that it's changed a second time until the page is reloaded.

It's causing a lot of problems as I can't validate any input. I've set the session var as an array to check what's happening.

This only happens when I submit the form using 'post'. It doesn't happen using 'get' or on a plain reload of the page.

 

You can take a look here: http://www.test.gimppro.co.uk/test.php

Thanks

 

 

EDIT: forgot, here is the exact code that I am using on the link above:

<?php
session_start();
$_SESSION['bot'][] = date('U');
print_r($_SESSION['bot'])
?>
<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<input type="submit" value="go" name="go" />
</form>

It sounds like a register globals problem and program/post/session variables with the same name.

 

Just posting a link lets someone see the symptoms, but you need to post your code if you want anyone to help find what the problem is.

It's because your setting the variable every time the page is loaded, not just when the form is submitted.

 

<?php
session_start();

if (isset($_POST['go'])){
   $_SESSION['bot'][] = date('U');
}

print_r($_SESSION['bot'])

?>

<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<input type="submit" value="go" name="go" />
</form>

I'd edited my first post with the code.

It's also happening on my local server and both servers have globals turned off.

I'm sure I'm probably just being a numpty and missing something really obvious as I've been staring at my code too long..

 

pocobueno1388, I don't see how that would create the variable twice? It's for a anti-bot feature, so I need it to reload every time the form is loaded.

On the actual form I've got my error checks on the same page, before the code to create the session variable, so it shouldn't be effected anyway as the session variable shouldn't be changed until after the error checking.

If you are only using one variable (which seems like you are), you may just want to do this:

 


<?

session_start();

$_SESSION['bot'] = date('U');
print_r($_SESSION['bot'])

?>

 

I noticed that in your code you had : $_SESSION['bot'][] and $_SESSION['bot']. 

 

You were trying to print "$_SESSION['bot']", and "$_SESSION['bot']" wasn't even being checked.

 

Just thought this might help if this was the case...

sorry but you've lost me there..

Whenever $_SESSION['bot'] was being set it was as an array. I then used $_SESSION['bot'] to print out the whole array so that I could see that it was in fact changing the variable twice, instead of just the once.

 

In the actual form it is not an array.

Not quite sure what you mean by:

"$_SESSION['bot']" wasn't even being checked.

Could you post the actual incorrect and correct output you are talking about.

 

I tried your code (with post and get methods) and visited your link and each singular visit (closing the browser between visits) and multiple visits with a browser window kept open (so the session persisted), each form submission, and each page refresh resulted in the correct number of new values (1.)

 

If you post the actual code you are using to test the value when it is not matching, someone can probably see what is wrong.

 

Also, what browser are you using.

 

Edit: I tried the posted code in both IE7 and FF, with both post and get methods and it operates the same and operates as expected.

At this point, my guess is your actual code is putting the value from the session variable into a hidden field. If your actual code posts to itself, like your test code is, then the session variable is getting unconditionally overwritten with a new value and that won't ever match what was placed into the hidden field.

 

Post actual code please.

It's actually working now as I said above. I'm not sure what the problem was though, it just started working. I hadn't changed any of the code, so I'm wondering if perhaps my browser was playing up or something.

 

The code I'm using is very long but the basics of it is this:

<?php
session_start();
print_r($_SESSION['bot'])
?>
<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
$_SESSION['bot'] = //a random created value here
<input type="text" name="bot" value="" />
<input type="submit" value="go" name="go" />
</form>

 

The correct output is something like:

Array ( [0] => 1211535296 [1] => 1211535298 [2] => 1211535299 )

Where each variable is created on page load, so 3 page loads here.

 

an incorrect output would be something like:

Array ( [0] => 1211535296 [1] => 1211535298 [2] => 1211535299 [4] => 1211535305 [5] => 1211535306 )

where, apart from the first page load (because not using POST), it created two value each time, with a one second interval.

 

As I've said though it's working now without any change to the code, so I'm not sure what was wrong.

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.