By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,722 Members | 1,247 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,722 IT Pros & Developers. It's quick & easy.

Session obsession

P: n/a
Greeting everyone.
Today my mom said that if I hadn't solve out my session troubles,
she'd lock me down in a fridge. Don't let me die in spooky darkness!
Please have your gentle look at that:

==cut==
class User {};
$user = NULL;

function fireup_session ($login, $pass)
{
global $user;
session_start ();
if ($login != '' && $pass != '') {
$user = new User ();
if (!$user->LogIn ($login, $pass))
{
echo ("get outta here, dumb hacker!");
return false;
}
$_SESSION["user"] = &$user;
return true;
}
if (isset ($_SESSION["user"])) $user = &$_SESSION["user"];
return true;
}
==cut==

Okay, I'm just trying to follow the everybody's fine way of setting a
session cookie, but that just doesn't work. Why? Is there a problem
in that I redefine the global $user?
Thanks in advance,
Vsevolod.
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Vsevolod Buzinov" <vs******@sendmail.ru> wrote in message
news:60**************************@posting.google.c om...
Greeting everyone.
Today my mom said that if I hadn't solve out my session troubles,
she'd lock me down in a fridge. Don't let me die in spooky darkness!
Please have your gentle look at that:

==cut==
class User {};
$user = NULL;

function fireup_session ($login, $pass)
{
global $user;
session_start ();
if ($login != '' && $pass != '') {
$user = new User ();
if (!$user->LogIn ($login, $pass))
{
echo ("get outta here, dumb hacker!");
return false;
}
$_SESSION["user"] = &$user;
As stated in the php manual "You can't use references in session variables
as there is no feasible way to restore a reference to another variable."
return true;
}
if (isset ($_SESSION["user"])) $user = &$_SESSION["user"];
return true;
}
==cut==

Okay, I'm just trying to follow the everybody's fine way of setting a
session cookie, but that just doesn't work. Why? Is there a problem
in that I redefine the global $user?
Thanks in advance,
Vsevolod.

Jul 17 '05 #2

P: n/a
> > $_SESSION["user"] = &$user;

As stated in the php manual "You can't use references in session variables
as there is no feasible way to restore a reference to another variable."


OK, but there's one more trouble to solve, look at this:

function f () {
global $user;
user = new User ('john doe');
}

echo ($user->name);

doesn't work :(
whilst:

function f () {
GLOBALS['user'] = new User ('john doe');
}

echo ($user->name);

works as expected! Why?
Jul 17 '05 #3

P: n/a
Vsevolod Buzinov <vs******@sendmail.ru> wrote:
$_SESSION["user"] = &$user;


As stated in the php manual "You can't use references in session variables
as there is no feasible way to restore a reference to another variable."


OK, but there's one more trouble to solve, look at this:

function f () {
global $user;
user = new User ('john doe');
}

echo ($user->name);

doesn't work :(
whilst:

function f () {
GLOBALS['user'] = new User ('john doe');
}

echo ($user->name);

works as expected! Why?


Neither will work. You have to use $user in teh first example and $GLOBALS
in the second one.
--
Simon Stienen <http://dangerouscat.net> <http://slashlife.de>
»What you do in this world is a matter of no consequence,
The question is, what can you make people believe that you have done.«
-- Sherlock Holmes in "A Study in Scarlet" by Sir Arthur Conan Doyle
Jul 17 '05 #4

P: n/a
"Vsevolod Buzinov" <vs******@sendmail.ru> wrote in message
news:60**************************@posting.google.c om...
$_SESSION["user"] = &$user;


As stated in the php manual "You can't use references in session variables as there is no feasible way to restore a reference to another variable."


OK, but there's one more trouble to solve, look at this:

function f () {
global $user;
user = new User ('john doe');
}

echo ($user->name);

doesn't work :(
whilst:

function f () {
GLOBALS['user'] = new User ('john doe');
}

echo ($user->name);

works as expected! Why?


I have no idea. I don't know what the User class looks like. The code above
is not executable (variable names are missing leading $). There is no
indication of what "doesn't work" and "works as expected" mean.

Short, complete, executable examples are needed if you want the answers to
these questions. Include the input, expected output, and actual output
values from your tests. Often you will find the answer simply by preparing
the example.

- Virgil
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.