Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get previously stored $_SESSION value?

I am here storing the randomly generated value from rand() into $_SESSION['previous_rand']

. I am also echo both the current generated rand() and previously stored in $_SESSION['previous_rand']

but it showing the current generated rand() into the previously generated rand() field.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How can i make it show the previously generated rand() correctly?

<?php

session_start();

$rand = rand(1000,9999);
$_SESSION['previous_rand'] = $rand;


echo "Current generated RAND: " . $rand;
echo "<br>";
echo "Previously generated RAND: " . $_SESSION['previous_rand'];
  
?>

>Solution :

<?php

session_start();

$rand = rand(1000,9999);
//Here you were setting new $rand to previous_rand that's why.

echo "Current generated RAND: " . $rand;
echo "<br>";
echo "Previously generated RAND: " . $_SESSION['previous_rand'];
$_SESSION['previous_rand'] = $rand;
?>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading