Forum Moderators: coopster

Message Too Old, No Replies

$_SESSION["varname"] remember value after unset

         

Superbuis1

3:43 pm on May 7, 2004 (gmt 0)

10+ Year Member



Hello!

I have a problem with a session variable:
in my main php page I have the following code :
session_start();
if (!isset($_SESSION["rndOrderNR"]))
...
...
$_SESSION["rndOrderNR"]=rand(0, 999);

This put a random number in the session variable. And if I do an unset from an include page like:
unset($_SESSION["rndOrderNR"]);
session_destroy();
it even clears the session variable. but when I run my main php again to calculate a new random number, it radomizes the same number?!

in my php.ini I have: register_globals :off

Anyone who can help me?

Thanks!

coopster

4:01 pm on May 7, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You aren't seeding the random number generator first, are you? Which version of PHP? As of PHP 4.2.0, there is no need to seed the random number generator with srand() or mt_srand() as this is now done automatically.

The rand() function has had some issues so you may want to check the bug reports for your OS and PHP version. [bugs.php.net...]

Superbuis1

6:21 pm on May 7, 2004 (gmt 0)

10+ Year Member



Thanks Coopster!

I'm using php 4.3.4. But you're right, there seem to be some bugs with the rand() function. At least I know that's not a coding mistake of mine. Is there a simple way to solve this with srand() instead? Because at the site you refer to they talk about CVS or something. Meaning I have to compile things? Or perhaps upgrading to version 4.3.6? Never done an upgrade, can this cause many problems?

coopster

6:50 pm on May 7, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



There was a similar issue here not too long ago...
Random Number in PHP: Why does this code not work? [webmasterworld.com]

Is there a simple way to solve this with srand() instead?

It seems as that solution works for some folks. You could try it. There is an example in the thread provided (or one of the links from within that thread).

Out of curiousity, what operating system are you using?

Superbuis1

7:50 pm on May 7, 2004 (gmt 0)

10+ Year Member



Windows XP Prof. with IIS 5.1.

I finally got it working, but just to test. Have to make some adjustments. Every post I run srand(), which makes my pages very slow... I'll write a function instead later on...
Thanks!