Jump to content

Small session question


fortnox007

Recommended Posts

Hi all a small question. I was wondering if someone knows what would be the approach to achieve the following.

on a page I show a random number. If the page refreshes (selfreferencing) It shows a new random number but also a the previous number, and this must be able to repeat itself : )

So to make it more visible:

 

New Random number = 98765

Previous Number = 56412

 

This is what I have but it's ofc not working because ones the session var has been set it will not show the previous variable but the very first one.

 

<?php
session_start();
$random_number .= mt_rand(1,100000);
if(!isset($_SESSION['number'])){
   $_SESSION['number']=$random_number;
}
// echo all out:
echo 'New random number is: '.$random_number;
echo 'Previous number is: '.$_SESSION['number'];  
?>

 

I bet i need another variable to store the previous one in but for some reasons my brains don't work at the moment

Link to comment
Share on other sites

Only the current one and the previous, but if you know a way to view last few that might give more insight in it.

I am allready trying stuff local with unset() but no succes yet :)

I can do it with hiddenfields but i rather do it with sessions, and it's also good for better understanding them I think  ::)

Link to comment
Share on other sites

Ok I think I have it . The code below only shows the previous one and the current one.

Pikachu2000 (or any other smart person) If you know a way how to show a list of all the previous ones I would love to here it ; )

I can think of a way but that is pretty redundant.

 

session_start();
$_SESSION['old']=$_SESSION['number'];//placed above the random generated one
$random_number .= mt_rand(1,100000);
$_SESSION['number']=$random_number;
// echo all out:
echo 'New random number is: '.$random_number.'<br />';
echo 'Just to store the current: '.$_SESSION['number'].'<br />';
echo 'Previous number is: '.$_SESSION['old'].'<br />';

 

Link to comment
Share on other sites

Ok I adjusted alittle more and i think I am almost able to store the previous numbers in an array, but it seems i am doing something wrong, because it overrides the value in the array instead of putting it at the bottom. Btw i didn't use array_push because the manual said it's for multiple values.

 

Could anyone see the error?

 

$_SESSION['list_of_numbers']=array();
$_SESSION['list_of_numbers'][]=$_SESSION['old'];// why doesn't this add a value to the bottom of the array?


foreach($_SESSION['list_of_numbers'] as $key=>$value)
    {
    // and print out the values
    echo 'The value of $_SESSION['."'".$key."'".'] is '."'".$value."'".' <br />';
    }

Link to comment
Share on other sites

HA i found it!!! wohoOOO

The script above made a new array everytime the page refreshed i think so i placed an if statement and here it is : )

 

if(!isset($_SESSION['list_of_numbers'])){
$_SESSION['list_of_numbers']=array();// now it will only be created if it doesn't exist 
}
$_SESSION['list_of_numbers'][]=$_SESSION['old'];

foreach($_SESSION['list_of_numbers'] as $key=>$value)
    {
    // and print out the values
    echo 'The value of $_SESSION['."'".$key."'".'] is '."'".$value."'".' <br />';
    }

 

If someone has a better method please tell me : )

Link to comment
Share on other sites

This may not be perfect, since my brain is a little fried at the moment, but you can also try this if you'd like . . . Just paste it in to a new .php file to try it out.

 

<?php
session_start();
function GENERATE_RAND($how_many) {
   if( count($_SESSION['rand']) > $how_many || !isset($_SESSION['rand']) ) { //if the array was already over the parameter, initialize it as empty
      $_SESSION['rand'] = array(0=>'', 1=>'', 2=>'', 3=>'');
   } else {
      array_unshift($_SESSION['rand'], mt_rand(1,100000));
      if( count($_SESSION['rand']) >= $how_many )  {
         array_pop($_SESSION['rand']);
      }
   }
   return $_SESSION['rand'];
}

// USAGE //
$rand_array = GENERATE_RAND(4); // 4 is the parameter passed to $how_many, determines number of rands to show in history.
print_r($rand_array);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.