Hi..guys! I am using mt_rand() to generate a random number,however how can prevent it keep on changing when page refresh?Thanks...

$random = mt_rand(100000, 999999);

    It depends on how you want it to be random in the first place. Are you trying to keep it constant for the current user, or constant across a particular time period?

      Well, I put it into session,problem resolved

      <? 
      session_start();
      if(!isset($_SESSION['random'])){ 
           $_SESSION['random'] = mt_rand(100000, 999999); 
      } 
      $random = $_SESSION['random']; 
      ?>
      
      <?php echo $random; ?>

        This is more of a general/aesthetics thing and isn't directly related to your question/resolution, but as you write your code and move on to other functions, the exact purpose of this variable may not be clear later. You may want to consider setting the variable to a name that makes sense as to the variable's function within the script so it is easier to debug if you have problems later. (unless $random makes enough sense to it's function). Personally, if I ever used the name random for a variable, it would be something that would only be used on a temporary basis. Not sure if this applies to you, but thought I'd put that out there.

          Write a Reply...