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
https://forums.phpfreaks.com/topic/213621-small-session-question/
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  ::)

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 />';

 

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 />';
    }

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 : )

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);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.