Page 1 of 1

Getting Varables to keep their values

Posted: Mon Oct 18, 2004 1:01 pm
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?

Saving State

Posted: Mon Oct 18, 2004 1:16 pm
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

Posted: Mon Oct 18, 2004 1:39 pm
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