RE: [PHP-DB] Session Broken
| From: | Jonathan Hilgeman | Date: | Thu, 08 Nov 2001 16:20:21 +0000 |
| Subject: | RE: [PHP-DB] Session Broken | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-14148@lists.php.net to get a copy of this message | ||
In your function, you use $UserName and $PHPSESSID, but they are not
available to the function's variable scope by default. In other words, you
can't use those variables until you copy them over from the outside to the
inside of the function. The way to do that is by either passing them as
arguments, like this:
ShowMessage("This is the message",$UserName,$PHPSESSID);
function ShowMessage($message,$UserName,$PHPSESSID)
{
... rest of function ...
}
---- OR ----
You can copy them in by using global:
function ShowMessage($message)
{
global $UserName;
global $PHPSESSID;
... rest of function ...
}
Even if this explains your dilemma, you should probably read this page in
the manual. It has a lot of good info that a lot of programmers don't know -
I thought I knew all about scopes until I read this and learned a bit more -
and a lot of good user notes at the bottom.
http://www.php.net/manual/en/language.variables.scope.php
- Jonathan
-----Original Message-----
From: Matthew Tedder [mailto:matthew@tedder.com]
Sent: Wednesday, November 07, 2001 5:02 PM
To: php-db@lists.php.net
Subject: [PHP-DB] Session Broken
Can anyone tell me why my session isn't working? I have attached the
problem
script. Without even going to another page, the $PHPSESSID and $UserName
variables are both empty. They are also empty on subsequent pages.
It starts with:
=========================================================
<?php
session_start();
session_register("UserName");
/*=====<< Check for New User Session >>=====*/
if(!isset($UserName))
{
$UserName = "guest";
};
==========================================================
And toward the end, it has the following:
==========================================================
function ShowMessage($message)
{
print "<br>\n";
print "<table border=2 align=center>\n";
print "<tr><td align=center><font color=yellow>$UserName ($PHPSESSID):
$message</font></td></tr>\n";
print "</table>\n";
}
?>
==========================================================
In this function, the variables are flat empty.
--Matthew
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-db-unsubscribe@lists.php.net
For additional commands, e-mail: php-db-help@lists.php.net
To contact the list administrators, e-mail: php-list-admin@lists.php.net