number number and session

Discussion in 'PHP' started by Dimension, Jan 27, 2008.

  1. #1
    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?
     
    Dimension, Jan 27, 2008 IP
  2. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #2
    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.
     
    jayshah, Jan 27, 2008 IP
  3. Dimension

    Dimension Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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:
     
    Dimension, Jan 27, 2008 IP
  4. mvl

    mvl Peon

    Messages:
    147
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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.
     
    mvl, Jan 27, 2008 IP
  5. Dimension

    Dimension Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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 :confused:
     
    Dimension, Jan 27, 2008 IP
  6. mvl

    mvl Peon

    Messages:
    147
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Only unset it at a point in your script where you don't need the number anymore, e.g. just after the mkdir(...)
     
    mvl, Jan 27, 2008 IP
  7. Dimension

    Dimension Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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:
     
    Dimension, Jan 27, 2008 IP
  8. mvl

    mvl Peon

    Messages:
    147
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Find some other place in your code to unset it. Unset it just after you are finished using it.
     
    mvl, Jan 27, 2008 IP
  9. Dimension

    Dimension Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    its impossible. no matter where i put it, it wont work...
     
    Dimension, Jan 27, 2008 IP
  10. Dimension

    Dimension Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    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
     
    Dimension, Jan 28, 2008 IP