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?
Getting Varables to keep their values
Moderator: General Moderators
Getting Varables to keep their values
Heya, well... I'm stuck.
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?
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?
Saving State
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
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
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Sessions
Page 1:
Page 2:
Request
Page 1:
Page 2:
Page 1:
Code: Select all
session_start();
$a = 10;
$_SESSION['a'] = $a;Code: Select all
session_start();
$a = $_SESSION['a'];Page 1:
Code: Select all
$a = 10;
echo "<a href="http://www.mysite.com/page2.php?a=$a">Click here</a>";Code: Select all
$a = intval($_REQUEST['a']); // intval() removes anything dangerous from parameter