Connecting Tech Pros Worldwide Forums | Help | Site Map

Session obsession

Vsevolod Buzinov
Guest
 
Posts: n/a
#1: Jul 17 '05
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.

Virgil Green
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Session obsession


"Vsevolod Buzinov" <vsevolod@sendmail.ru> wrote in message
news:602986b2.0409220900.10263d99@posting.google.c om...[color=blue]
> 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;[/color]

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."
[color=blue]
> 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.[/color]


Vsevolod Buzinov
Guest
 
Posts: n/a
#3: Jul 17 '05

re: Session obsession


> > $_SESSION["user"] = &$user;[color=blue]
>
> 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."[/color]

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?
Simon Stienen
Guest
 
Posts: n/a
#4: Jul 17 '05

re: Session obsession


Vsevolod Buzinov <vsevolod@sendmail.ru> wrote:[color=blue][color=green][color=darkred]
>>> $_SESSION["user"] = &$user;[/color]
>>
>> 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."[/color]
>
> 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?[/color]

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
Virgil Green
Guest
 
Posts: n/a
#5: Jul 17 '05

re: Session obsession


"Vsevolod Buzinov" <vsevolod@sendmail.ru> wrote in message
news:602986b2.0409230321.7e21c8aa@posting.google.c om...[color=blue][color=green][color=darkred]
> > > $_SESSION["user"] = &$user;[/color]
> >
> > As stated in the php manual "You can't use references in session[/color][/color]
variables[color=blue][color=green]
> > as there is no feasible way to restore a reference to another variable."[/color]
>
> 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?[/color]

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


Closed Thread