How to preserve value ?

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
tinhyeu2
Forum Newbie
Posts: 1
Joined: Fri Aug 08, 2003 8:50 pm

How to preserve value ?

Post 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
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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;
}
?>
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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...
}
?>
Post Reply