Re: PHP4 Session Management
| From: | Brendan W. McAdams | Date: | Sat, 01 Apr 2000 22:03:53 +0000 |
| Subject: | Re: PHP4 Session Management | ||
| References: | 1 | Groups: | php.version4 |
| Request: | Send a blank email to php-version4+get-13003@lists.php.net to get a copy of this message | ||
no problem; I'm CCing this to the list in case anyone else cares to learn
from it.
>When you create a session, is that variable available throughout all pages?
The session variable is available to all pages that create a session and as
long as your session cookie is valid - the cookie by default expires when
you close your browser. You need to declare a start of session on each page
(see my previous post for more on that).
Variables themselves ONLY go from page to page if you specifically tell your
session to save them (see session_register in my last post) -- if you took
all your variables it would take alot of disk space and memory so PHP just
takes what you tell it to.
>If you are submitting with an anchor i.e. a link, is that session variable
available to other pages?
Yes, two ways to do this:
1) Set the page you are linking to to start a session and find out what your
session_id is.
2) link with a ?PHPSESSID=$PHPSESSID which passes your session ID
automatically.
>Can you give me an example of how to create a session since I have failed
so far.
Again I reccomend Julie Meloni's HotWired Webmonkey tutorial at:
http://hotwired.lycos.com/webmonkey/00/05/index2a.html?tw=programming
But here's my quick take on it using your examples pasted, now fixed for PHP
Sessions (and with some of my own coding style applied, please forgive me):
test.php
<?
echo "<html><body>";
echo "<form method=post action=test1.php>";
echo "<table><tr>";
echo "<td>Userid: </td><td><input type=text
name=userid></td>";
echo "<td colspan=2><input type=submit value=\"Submit\">";
echo "</tr></table>";
echo "</body></html>";
?>
test1.php
<?
session_start();
session_register("user");
$user = $userid;
echo "<html><body>";
echo "<form method=pos action=test2.php>";
echo "<input type=submit value=\"Submit\">";
echo "</form>";
echo "</body></html>";
?>
test2.php
<?
session_start(); // here's your problem - you need to pull the session up
again to have access to its variables.
// at this point $user was pulled out of the session cache on the server
if (!$user) { // something happened, and for some reason the variable didnt
save.
echo("Error! Variable not found?");
exit(); // go bye-bye!
} else {
echo "<html><body>";
echo "User: $user"; // $user was pulled from the session ,we hope
echo "</body></html>";
}
?>
hope this helps.
-brendan
-----
Brendan W. McAdams | email: brendan@plexmedia.com
Programmer/Systems Administrator | office: (305)377-2880
Plexus InterActive | pager: (305)277-4879
http://www.plexmedia.com | cell phone:
(305)401-7313
"Always listen to the experts - they'll tell you what can't be done and why.
Then do it."
-Robert A. Heinlein
----- Original Message -----
From: "Hector Lucero" <hql@pacbell.net>
To: <brendan@plexmedia.com>
Sent: Saturday, April 01, 2000 4:18 PM
Subject: PHP4 Session Management
Hi,,
Excuse me for sending you an email being a total stranger. I saw your
response to one of the session emails in the php list. I am new to PHP
and do not know how to create a session. Could you enlighten me a bit
on the subject. I am using php4.
* When you create a session, is that variable available throughout
all pages?
* If you are submitting with an anchor i.e. a link, is that session
variable available to other pages?
* Can you give me an example of how to create a session since I have
failed so far.
Note: This is what I have been doing and when I try to see the variable
in other pages I dont get anything in test2.php. Am I doing something
wrong?
Thanks a million;
Hector
test.php
<?
echo "<html><body>";
echo "<form method=post action=test1.php>";
echo "<table><tr>";
echo "<td>Userid: </td><td><input type=text
name=userid></td>";
echo "<td colspan=2><input type=submit value=\"Submit\">";
echo "</tr></table>";
echo "</body></html>";
?>
test1.php
<?
session_start();
session_register("user");
$user = $userid;
echo "<html><body>";
echo "<form method=pos action=test2.php>";
echo "<input type=submit value=\"Submit\">";
echo "</form>";
echo "</body></html>";
?>
test2.php
<?
echo "<html><body>";
echo "User: $user";
echo "</body></html>";
?>