Saving data between pages

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

    Saving data between pages

    Is it possible to save session values between pages? I did a little script
    to let the users login. I use this script to check username and password the
    user introduces in a form in the main page, index.php.
    If a user is found with username and password, than I create
    $_SESSION['Login'] and assign it a value of true, else I assign it a value
    of false. If a user is non registered, I send it to the proper page to
    register, RegistraUtente. php, if he is registered yet, I send him back to
    index.php
    But here in index.php $_SESSION['Login'] has no value.

    Here is the script I wrote:

    <?php

    session_start() ;

    include("dbconn ect.php");

    $tabella="table ";

    $dbname="Zucca" ;

    if (!isset($_SESSI ON['Login']))

    {

    $_SESSION['Login']=false;

    }

    $IdUser=session _id();

    $data=date('Y-m-d');

    if (!ISSET($HTTP_C OOKIE_VARS["PiOnLine"]))

    setcookie("PiOn Line",$IdUser,t ime()+60*60*24* 365);

    else

    $IdUser=$HTTP_C OOKIE_VARS["PiOnLine"];

    mysql_select_db ($dbname,$db);

    $username=$_POS T["username"];

    $password=$_POS T["password"];

    $query="Select Username, UserPassword from $tabella where
    ('$password'=Us erPassword) and ('$username'=Us ername)";

    $result=mysql_q uery("$query") or die(mysql_error ());

    // Se esiste l'utente con username e password allora consenti il login

    if (mysql_num_rows ($result)==1)

    {

    $_SESSION['Login']=true;

    session_write_c lose();

    $url="index.php ";

    header ("Location: $url");

    }

    else // Invia l'utente sulla pagina di registrazione

    {

    $_SESSION['Login']=false;

    session_write_c lose();

    $url="RegistraU tente.php";

    header ("Location: $url");

    }

    ?>


    Franz





  • Eric Stein

    #2
    Re: Saving data between pages

    The whole point of session variables is inter-pageview preservation. You
    just aren't using the session vars just right. Where you did:

    $_SESSION['Login']=true;

    You should have done:

    $Login = true;
    session_registe r("Login");

    Later, pages can access it as $HTTP_SESSION_V ARS["Login"]. I'm not sure if
    there are differences between $HTTP_SESSION_V ARS and $_SESSION - check the
    PHP docs @ http://www.php.net and remember to provide a logout:

    $Login=false;
    session_registe r("Login");

    You can also log the user out by using session_destroy ().
    ~Eric

    My PGP public key: http://www.parabolagames.com/chem/pgppubkey.txt

    "Lamp" <lamp@dina.it > wrote in message
    news:HQvic.1383 40$rM4.5430596@ news4.tin.it...[color=blue]
    > Is it possible to save session values between pages? I did a little script
    > to let the users login. I use this script to check username and password[/color]
    the[color=blue]
    > user introduces in a form in the main page, index.php.
    > If a user is found with username and password, than I create
    > $_SESSION['Login'] and assign it a value of true, else I assign it a value
    > of false. If a user is non registered, I send it to the proper page to
    > register, RegistraUtente. php, if he is registered yet, I send him back to
    > index.php
    > But here in index.php $_SESSION['Login'] has no value.
    >
    > Here is the script I wrote:
    >
    > <?php
    >
    > session_start() ;
    >
    > include("dbconn ect.php");
    >
    > $tabella="table ";
    >
    > $dbname="Zucca" ;
    >
    > if (!isset($_SESSI ON['Login']))
    >
    > {
    >
    > $_SESSION['Login']=false;
    >
    > }
    >
    > $IdUser=session _id();
    >
    > $data=date('Y-m-d');
    >
    > if (!ISSET($HTTP_C OOKIE_VARS["PiOnLine"]))
    >
    > setcookie("PiOn Line",$IdUser,t ime()+60*60*24* 365);
    >
    > else
    >
    > $IdUser=$HTTP_C OOKIE_VARS["PiOnLine"];
    >
    > mysql_select_db ($dbname,$db);
    >
    > $username=$_POS T["username"];
    >
    > $password=$_POS T["password"];
    >
    > $query="Select Username, UserPassword from $tabella where
    > ('$password'=Us erPassword) and ('$username'=Us ername)";
    >
    > $result=mysql_q uery("$query") or die(mysql_error ());
    >
    > // Se esiste l'utente con username e password allora consenti il login
    >
    > if (mysql_num_rows ($result)==1)
    >
    > {
    >
    > $_SESSION['Login']=true;
    >
    > session_write_c lose();
    >
    > $url="index.php ";
    >
    > header ("Location: $url");
    >
    > }
    >
    > else // Invia l'utente sulla pagina di registrazione
    >
    > {
    >
    > $_SESSION['Login']=false;
    >
    > session_write_c lose();
    >
    > $url="RegistraU tente.php";
    >
    > header ("Location: $url");
    >
    > }
    >
    > ?>
    >
    > Franz[/color]


    Comment

    • Chung Leong

      #3
      Re: Saving data between pages

      "Eric Stein" <no@spam.com> wrote in message
      news:c6k1ji$2an o$1@news.wplus. net...[color=blue]
      > The whole point of session variables is inter-pageview preservation. You
      > just aren't using the session vars just right. Where you did:
      >
      > $_SESSION['Login']=true;
      >
      > You should have done:
      >
      > $Login = true;
      > session_registe r("Login");[/color]

      No, $_SESSION['Login'] = true should work perfectly fine. I'm not entirely
      sure what the problem is. I suspect it has something to do with setting the
      cookie. In general when you're using session variables, you should keep your
      hand off the cookie. Stay away from session_registe r() too.


      Comment

      • Eric Stein

        #4
        Re: Saving data between pages

        Chung,
        Sorry, I didn't know - but I've been using session_registe r() for a long
        time and it's always worked. Why shouldn't I use it?
        ~Eric

        My PGP public key: http://www.parabolagames.com/chem/pgppubkey.txt

        "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message
        news:scmdnYz1uc EFKhDd4p2dnA@co mcast.com...[color=blue]
        > No, $_SESSION['Login'] = true should work perfectly fine. I'm not entirely
        > sure what the problem is. I suspect it has something to do with setting[/color]
        the[color=blue]
        > cookie. In general when you're using session variables, you should keep[/color]
        your[color=blue]
        > hand off the cookie. Stay away from session_registe r() too.[/color]


        Comment

        • Daniel Tryba

          #5
          Re: Saving data between pages

          Eric Stein <no@spam.com> wrote:[color=blue]
          > Chung,
          > Sorry, I didn't know - but I've been using session_registe r() for a long
          > time and it's always worked. Why shouldn't I use it?[/color]

          Because the manual says so :)
          <q src='http://nl2.php.net/session_registe r'>
          Caution

          If you want your script to work regardless of register_global s, you need
          to instead use the $_SESSION array as $_SESSION entries are
          automatically registered. If your script uses session_registe r(), it
          will not work in environments where the PHP directive register_global s
          is disabled.
          </q>

          Also look at the other warnings.

          --

          Daniel Tryba

          Comment

          • .:Ninja

            #6
            Re: Saving data between pages

            Eric Stein wrote:
            [color=blue]
            > The whole point of session variables is inter-pageview preservation. You
            > just aren't using the session vars just right. Where you did:
            >
            > $_SESSION['Login']=true;
            >
            > You should have done:
            >
            > $Login = true;
            > session_registe r("Login");
            >
            > Later, pages can access it as $HTTP_SESSION_V ARS["Login"]. I'm not sure
            > if there are differences between $HTTP_SESSION_V ARS and $_SESSION - check
            > the PHP docs @ http://www.php.net and remember to provide a logout:
            >
            > $Login=false;
            > session_registe r("Login");
            >
            > You can also log the user out by using session_destroy ().
            > ~Eric
            >
            > My PGP public key: http://www.parabolagames.com/chem/pgppubkey.txt
            >
            > "Lamp" <lamp@dina.it > wrote in message
            > news:HQvic.1383 40$rM4.5430596@ news4.tin.it...[color=green]
            >> Is it possible to save session values between pages? I did a little
            >> script to let the users login. I use this script to check username and
            >> password[/color]
            > the[color=green]
            >> user introduces in a form in the main page, index.php.
            >> If a user is found with username and password, than I create
            >> $_SESSION['Login'] and assign it a value of true, else I assign it a
            >> value of false. If a user is non registered, I send it to the proper page
            >> to register, RegistraUtente. php, if he is registered yet, I send him back
            >> to index.php
            >> But here in index.php $_SESSION['Login'] has no value.[/color][/color]

            From the posted script the problem isn't immediately obvious. Other than
            maybe the following. Are you testing the script on your local box running a
            web server? Make sure that you access your script through
            http://127.0.0.1/scriptname.php and not http://localhost/scriptname.php
            because: If you session_start() on a "localhost" page, that session is not
            available to a "127.0.0.1" page. On Apache, I can access
            http://localhost/login.php no problem, but when I send the variable to a
            relative path from login.php, Apache changes the hostname to the loopback
            interface, 127.0.0.1, which of course the session won't work for.

            Just a thought :)

            ..:Albe

            --

            Comment

            Working...