[web] Sessions not working correctly

Started by
1 comment, last by grc 12 years, 5 months ago
Simple sessions not working:

1.php


<?php
session_start();
$_SESSION[test] = 3;
echo "<a href='2.php'>2</a>";
?>


2.php


<?php
session_start();
echo $_SESSION[test];
?>





It will not display the 3 on the second page. But if they keep trying and trying and trying and trying like 50 times, eventually it works.

Then it works forever.

So what gives? The browser have to be "broken in" to sessions or something?

And why don't my sessions work but logins on other websites work fine the first time, despite sessions being how they do it as well.
Advertisement

Simple sessions not working:

1.php


<?php
session_start();
$_SESSION[test] = 3;
echo "<a href='2.php'>2</a>";
?>


2.php


<?php
session_start();
echo $_SESSION[test];
?>





It will not display the 3 on the second page. But if they keep trying and trying and trying and trying like 50 times, eventually it works.

Then it works forever.

So what gives? The browser have to be "broken in" to sessions or something?

And why don't my sessions work but logins on other websites work fine the first time, despite sessions being how they do it as well.


try putting quotes around test


<?php
session_start();
$_SESSION['test'] = 3;
echo "<a href='2.php'>2</a>";
?>



<?php
session_start();
echo $_SESSION['test'];
?>
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
You should probably change

[php]$_SESSION[test][/php]
to

[php]$_SESSION['test'][/php]


If that still isn't working, here are a few ideas you might want to try:

  • Add [font="Courier New"]error_reporting(E_ALL); [/font]straight after the [font="Courier New"]<?php [/font]line.

  • Check that cookies are enabled in your browser. You could also test it in a different browser (especially if you're using IE).

  • Make sure that the code you posted is the very first thing in each file (no whitespace or html before it).

  • Run this code:
[php]
<?php
phpinfo(INFO_MODULES);
?>
[/php]
Skip down to the section titled 'session', and check that session support is enabled. If you have been messing around with the settings, you might want to compare them to the defaults found here.


I hope I've been able to help smile.gif

This topic is closed to new replies.

Advertisement