Re: why values not remembered by php?

From: Date: Wed, 23 Aug 2000 00:31:20 +0000
Subject: Re: why values not remembered by php?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-13053@lists.php.net to get a copy of this message
Sonia Tsui writes: > > Here is the sample code > (main.php) > > <?php > $start = $end; > $end = $start + 3; > ?> > > <html> > ...... > <a href ="main.php" ....>nextbutton</a> > <?php > for ($i = $start; $i<$end; $i++) { > echo("i: $i<br>"); > } > ?> > </html> > > ------------------------------------------------------------------------- > so when <next button> is pressed, it comes back to this page. What I don't > understand is why $end is not remembered? I though variable exist for one > http transaction. I expect to see 0, 1, 2, 3 and then 4, 5, 6 etc. but I > just see 0,1,2,3 and then 0,1,2,3 again and again. Can anyone help with my > simple question? > > ---Sonia Exactly--variables exist only for the duration of the HTTP transaction. That's one page view. Since HTTP is a stateless protocol, the transaction goes something like this (simplified): 1) the browser opens a connection to the server 2) the browser requests a page 3) the server looks for the page, reads it, feeds it to PHP, and sends the result back to the browser 4) the connection is closed All variables are lost between pages, since there each page view constitutes a brand new connection. Broadly speaking, the act of working around this is called 'session management', meaning that you somehow get the server to 'remember' variables between pages. You can do this in a number of ways: for what you're doing here, the easiest would probably be to encode the variables into an URL or stick them into hidden variables, and then check them on the next page view. More robust session management would typically involve the server saving the variables somewhere, and then reloading them when the viewer loads the next page. Often, cookies or URL variables are used to keep track of what saved data belongs to which viewer. If you're using PHP3, check out PHPLIB (http://phplib.netuse.de), which offers this as well as a pile of other functionality. If you're using PHP4, check out the session management functions at http://www.php.net/manual/ref.session.php, which also goes into a bit more depth about what sessions actually are. -- +----------------------------------------------------------------+ |Torben Wilson <torben@php.net> Netmill iTech| |http://www.coastnet.com/~torben http://www.netmill.fi| |Ph: 1 250 383-9735 torben@netmill.fi| +----------------------------------------------------------------+

« previous php.general (#13053) next »