Session obsession

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Vsevolod Buzinov

    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.
  • Virgil Green

    #2
    Re: Session obsession

    "Vsevolod Buzinov" <vsevolod@sendm ail.ru> wrote in message
    news:602986b2.0 409220900.10263 d99@posting.goo gle.com...[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]


    Comment

    • Vsevolod Buzinov

      #3
      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?

      Comment

      • Simon Stienen

        #4
        Re: Session obsession

        Vsevolod Buzinov <vsevolod@sendm ail.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.ne t> <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

        Comment

        • Virgil Green

          #5
          Re: Session obsession

          "Vsevolod Buzinov" <vsevolod@sendm ail.ru> wrote in message
          news:602986b2.0 409230321.7e21c 8aa@posting.goo gle.com...[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


          Comment

          Working...