im trying to store a random number in a session so that i can get it once the page is refreshed, but when that happens the random number is changed because all the code is ran again on refresh. how do i keep the variable the same after the page refresh?
Please can you post the code that you already have. We can't debug a problem when we can't see any of the code.
cant post the whole file there is loads of code in there. the 1st if is the point where the file is accepted. the session start and random var are at the top of the document. session_start(); $random_number = rand(1000,100000); if (!file_exists("upload/" . date("m-d-y"))) { $newdatedir = mkdir("upload/" . date("m-d-y"), 0777); } $_SESSION['random_number']=$random_number; if(file_exists("upload/" . date("m-d-y") . "/" . $random_number)) { echo "Already been made stop refreshing the page!"; } else { $newtimedir = mkdir("upload/" . date("m-d-y") . "/" . $random_number, 0777); //$_SESSION['random_number']=$random_number; } PHP:
Modify the first lines so you only generate a random number if there isn't one in the session already: session_start(); if (isset($_SESSION['random_number'])) { $random_number = $_SESSION['random_number']; } else { $random_number = rand(1000,100000); } ... PHP: Don't forget to unset $_SESSION['random_number'] if you don't need it anymore.
that works great thanks! however, the session keeps the number all the time now. i have tried using an unset but this leads me back to the same problem i had before where a new number is generated each time
Only unset it at a point in your script where you don't need the number anymore, e.g. just after the mkdir(...)
having this is no good because it unsets it as soon as it creates the dir. if (!file_exists("upload/" . date("m-d-y"))) { $newdatedir = mkdir("upload/" . date("m-d-y"), 0777); } $_SESSION['random_number']=$random_number; if(file_exists("upload/" . date("m-d-y") . "/" . $random_number)) { echo "Already been made stop refreshing the page!"; } else { $newtimedir = mkdir("upload/" . date("m-d-y") . "/" . $random_number, 0777); unset($_SESSION['random_number']); } PHP:
if i post up my code in a file to download, you will be able to see my problem. i have also tried using the url but its no good. My Source Code