Session obsession | | |
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. | | | | 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] | | | | 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? | | | | 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 | | | | 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 |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,501 network members.
|