Page 1 of 1

How to preserve value ?

Posted: Fri Aug 08, 2003 8:50 pm
by tinhyeu2
I have 3 pages

page1.php
<?
session_start() ;
session_register("value") ;
$value = 1
?>

page2.php
<?
session_start() ;
$value++ ;
?>

page3.php
<form method="POST" action="page1.php">
<?
session_start() ;
$value++ ;
?>
<input type="submit" value="Submit" name="B1">
</form>

I would like I can preserve value of $value now is 3 when I come back to page1.php from page3.php , when I come back page1.php value of $value is now set to 1, please help me , thanks

Posted: Sat Aug 09, 2003 6:46 am
by Gen-ik
page1.php

Code: Select all

<?php
session_start() ; 

// This will stop the value being recreated if it already exists.

if(!session_is_registered("value"))
{
  session_register("value");
  $value = 1;
}
?>

Posted: Sat Aug 09, 2003 4:53 pm
by JAM
Just a small tip:
The Manual wrote: Caution:
If you want your script to work regardless of register_globals, you need to use the $_SESSION array. All $_SESSION entries are automatically registered. If your script uses session_register(), it will not work in environments where register_globals is disabled.
Lookup session_register in the man (tho you'll learn that you wont need it) and read up. Using $_SESSION is, imho, more simplified (not just regarding you problem)...

Code: Select all

<?php
if (!isset($_SESSION['value'])) {
...code...
}
?>