PHP Foreach Loop and Sessions - Strange Behavior

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
petsoukos
Forum Newbie
Posts: 3
Joined: Sun Aug 14, 2011 4:24 am

PHP Foreach Loop and Sessions - Strange Behavior

Post by petsoukos »

Hello fellow PHP developers, this is my first post here. I'm trying to find a solution to a really weird problem I'm having with my script.

I'm trying to pass a token to the SESSION like this, in the index.php file:

Code: Select all

session_start();
$_SESSION['token'] = sha1(mt_rand(100, 10000));
This variable will get assigned to a Javascript variable as well, on the same index.php script. That variable gets sent by an AJAX request:

Code: Select all

var the_token = '<?php echo $_SESSION['token']; ?>';

// jQuery Post
$("element").click(function() {
    $.post('ajax_io.php', {
        token: the_token
    }, function(response) {
        // Do something with response
    });
Then I have another script named, ajax_io.php, that is verifying that token after being send by an AJAX request:

Code: Select all

session_start();
if($_REQUEST['token'] != $_SESSION['token']) die("Horribly!");
The logic works, but for some odd reason the $_SESSION['token'] will get reset by another random SHA1 string and the test fails on the ajax_io.php script.

I should also mention at this point that the index.php has a Foreach loop that loops 3 times to display the tabbed interface.

Code: Select all

<?php 
// pseudo-code
foreach(expression) : ?>
    <div class="tab"><?php echo $data['html_string']; ?></div>
<?php endforeach; ?>
The problem occurs at this point in the loop. As you can see I don't assign a newly random token to the session but for some odd reason it does it by itself. If I comment out the echo $data['html_string'];, the session token variable remains unaffected and the script logic functions as expected.

Any idea why would an echo statement, withing a foreach loop, trigger the 2nd line of script ($_SESSION['token'] = sha1(mt_rand(100, 10000));) again?

UPDATE:
Fixed a typo, was multitasking at the time and mixed up the loop types. It was a foreach loop and not a while loop!
Last edited by petsoukos on Mon Aug 15, 2011 11:12 pm, edited 1 time in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP While Loop and Sessions - Strange Behavior

Post by Christopher »

Your loop is a "while" but your end tag is "endforeach;". I would recommend using braces instead.
(#10850)
petsoukos
Forum Newbie
Posts: 3
Joined: Sun Aug 14, 2011 4:24 am

Re: PHP While Loop and Sessions - Strange Behavior

Post by petsoukos »

Christopher wrote:Your loop is a "while" but your end tag is "endforeach;". I would recommend using braces instead.
I should have read it through before posting it. Original post is fixed now. Thanks.
petsoukos
Forum Newbie
Posts: 3
Joined: Sun Aug 14, 2011 4:24 am

Re: PHP Foreach Loop and Sessions - Strange Behavior

Post by petsoukos »

Found out that the cause of the double request was an IMG tag on the rendered text inside the foreach loop. The tag had an empty src parameter, that was triggering a request in the background. That request was refreshing the generated key, but since it was in the background, the front-end part wasn't getting the new key. Stupid problem.
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: PHP Foreach Loop and Sessions - Strange Behavior

Post by phphelpme »

Hi,

I think next time it would be a great idea to show more of your syntax as we could not see from what you posted what turned out to be the problem.

Best wishes
Post Reply