Random numbers

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pauls74462
Forum Newbie
Posts: 16
Joined: Tue Jun 10, 2008 8:13 pm

Random numbers

Post by pauls74462 »

Code: Select all

<?php
// Make Random Number
$R=(rand(1,9));
echo $R;
?>
I have the above code, this $R is needed on other pages. How do I make it carry over to the other pages. On the second page then (echo $R;) does not show up.

I have searched for thks and can't seem to find an answer. I can't use the command to pit in in a text file and store on the server, as other users will over write it.


Any suggestions?

Paul
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Random numbers

Post by Mark Baker »

pauls74462 wrote:I have the above code, this $R is needed on other pages. How do I make it carry over to the other pages. On the second page then (echo $R;) does not show up.

I have searched for thks and can't seem to find an answer. I can't use the command to pit in in a text file and store on the server, as other users will over write it.
You either pass it in the URL for the next page, and access it as a $_GET parameter, or you store it in a session file (which won't get overwritten by other users)
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

Re: Random numbers

Post by Reviresco »

Save it in a $_SESSION[] variable ( http://www.php.net/session ) or you could put it in a $_GET[] variable for each new page such as:

http://www.example.com/page2.php?R=127

In the above examples, you would have either:

$_SESSION['R']

or

$_GET['R']
guygk
Forum Newbie
Posts: 9
Joined: Thu Dec 04, 2008 4:06 pm

Re: Random numbers

Post by guygk »

Read about PHP sessions (http://php.net/sessions), they are used to keep data alive for user only while session is alive. If you want to store data for longer time period you can use cookies or * database instead.
Post Reply