Dragen Posted May 18, 2008 Share Posted May 18, 2008 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> Quote Link to comment https://forums.phpfreaks.com/topic/106178-session-variable-set-twice-on-form-submit/ Share on other sites More sharing options...
PFMaBiSmAd Posted May 18, 2008 Share Posted May 18, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/106178-session-variable-set-twice-on-form-submit/#findComment-544245 Share on other sites More sharing options...
pocobueno1388 Posted May 18, 2008 Share Posted May 18, 2008 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> Quote Link to comment https://forums.phpfreaks.com/topic/106178-session-variable-set-twice-on-form-submit/#findComment-544246 Share on other sites More sharing options...
Dragen Posted May 18, 2008 Author Share Posted May 18, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/106178-session-variable-set-twice-on-form-submit/#findComment-544250 Share on other sites More sharing options...
Dragen Posted May 22, 2008 Author Share Posted May 22, 2008 any help? EDIT: it actually seems to be working now, but I haven't changed anything. Very strange. Not sure what was causing the problem though.. Quote Link to comment https://forums.phpfreaks.com/topic/106178-session-variable-set-twice-on-form-submit/#findComment-547655 Share on other sites More sharing options...
munsiem Posted May 22, 2008 Share Posted May 22, 2008 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... Quote Link to comment https://forums.phpfreaks.com/topic/106178-session-variable-set-twice-on-form-submit/#findComment-547713 Share on other sites More sharing options...
Dragen Posted May 22, 2008 Author Share Posted May 22, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/106178-session-variable-set-twice-on-form-submit/#findComment-547722 Share on other sites More sharing options...
munsiem Posted May 23, 2008 Share Posted May 23, 2008 Ok I see now. I thought that all you were trying to do was set the variable once... ookkk, back to the matrix I go Quote Link to comment https://forums.phpfreaks.com/topic/106178-session-variable-set-twice-on-form-submit/#findComment-547846 Share on other sites More sharing options...
PFMaBiSmAd Posted May 23, 2008 Share Posted May 23, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/106178-session-variable-set-twice-on-form-submit/#findComment-547856 Share on other sites More sharing options...
PFMaBiSmAd Posted May 23, 2008 Share Posted May 23, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/106178-session-variable-set-twice-on-form-submit/#findComment-547861 Share on other sites More sharing options...
Dragen Posted May 23, 2008 Author Share Posted May 23, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/106178-session-variable-set-twice-on-form-submit/#findComment-547952 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.