Getting Varables to keep their values

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
desinc
Forum Newbie
Posts: 16
Joined: Sat Jan 31, 2004 5:13 pm

Getting Varables to keep their values

Post by desinc »

Heya, well... I'm stuck. :lol:

How do you get a varable to keep it's value from one PHP page to another? So for instance, if a script makes $a=10 on page1.php, is it possible to retain the value off $a on page two?
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Saving State

Post by neophyte »

Saving State on the web is the trick:

Four ways:

1.Hide it in a form with like so:
<input type="hidden" name="your_variable" value="$page1_value">
retrieve the variable with $_POST or $_GET.

2. Use a cookie

3. Use a $_SESSION variable

4. Use a query string
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Sessions

Page 1:

Code: Select all

session_start();
$a = 10;
$_SESSION['a'] = $a;
Page 2:

Code: Select all

session_start();
$a = $_SESSION['a'];
Request

Page 1:

Code: Select all

$a = 10;
echo "<a href="http://www.mysite.com/page2.php?a=$a">Click here</a>";
Page 2:

Code: Select all

$a = intval($_REQUEST['a']);    // intval() removes anything dangerous from parameter
Post Reply