I'm trying to make a login-system for my website using sessions.
But I don't get it working! Argh!

This is what I'm doing:
On login.php is a log-in form whith <form ... action='page1.php'>.
On page1.php there is a script that checks whether the username and password are known in the database. If so, username and id will be registered session variables (using $_SESSION['username']). User will be linked to a page2.php.
On page2.php I want to check for the session variables.

This is what happens:
Everything works, until I'm at page2.php. Yhere my session variables are gone! I can't echo them, and when I do a isset($_SESSION['username'], it is NOT set. But I DID set it on page1.php!

There is something strange besides that. When I use session_start() on page2.php my session file in /tmp become empty 0(kšŸ˜Ž! when I don't use session_start(), they're 34(kšŸ˜Ž containing 'username' and 'id', but still won't show up on page2.php with an echo.

Can anyone help me please! I really need that log-in system, and I definitely will need sessions in the future.

Thanx allready

P.S.
My system:
Linux Debian 3.0 (woody)
Apache 1.3.26
PHP 4.2.1 (from Debian sid release(unstable))

    Try this
    session_start();
    // pass variable form login.php
    $username = $REQUEST['username'];

    $password = $
    REQUEST['password'];

    mysql_connect( "$host", "$user", "$pwd" );
    mysql_select_db( "$db" );
    $sql = "SELECT * FROM user WHERE user='$username' AND password='$password' ";
    $result = mysql_query( $sql );

    $num = mysql_num_rows( $result );
    if ( $num != 0 )
    { $auth = true;
    $_SESSION['username']=$username
    }

    hope this help

    Martijn D. wrote:

    I'm trying to make a login-system for my website using sessions.
    But I don't get it working! Argh!

    This is what I'm doing:
    On login.php is a log-in form whith <form ... action='page1.php'>.
    On page1.php there is a script that checks whether the username and password are known in the database. If so, username and id will be registered session variables (using $_SESSION['username']). User will be linked to a page2.php.
    On page2.php I want to check for the session variables.

    This is what happens:
    Everything works, until I'm at page2.php. Yhere my session variables are gone! I can't echo them, and when I do a isset($_SESSION['username'], it is NOT set. But I DID set it on page1.php!

    There is something strange besides that. When I use session_start() on page2.php my session file in /tmp become empty 0(kšŸ˜Ž! when I don't use session_start(), they're 34(kšŸ˜Ž containing 'username' and 'id', but still won't show up on page2.php with an echo.

    Can anyone help me please! I really ne....

      Hi!

      Thanks for your help. Unfortunately it didn't help me.

      My problem is still:
      Can't echo my registered session variables on page2.php.
      The posted variables from login.php are available in page1.php, I really can echo them there. And I can register them with the following command:

      $row = mysql_fetch_array ($result);
      $_SESSION['username'] = $row['username'];

      After that I can echo $_SESSION['username'] on page1.php, but it is NOT available in page2.php!

      This is really frustrating, can anybody tell me what I have to do to get my sessions working?

        I'm not sure if this is the same problem as me, but I found that i needed to do session_start(); and session_register([all the variables]) on everypage in order to make sure the variable values wern't lost.

          I've did that too, but it doesn't do anything.

          The strange thing is that in the /tmp directory, where the session files are stored, all my session files are empty (0kšŸ˜Ž.
          This only happens when I use session_start() on page2.php.
          When I don't use session_start(), the session file has the variables in it.

          So, to be more clear, it seems that when I use session_start() on page2.php the session from page1.php is overwritten by the session from page2.php, and the variables (registered on page1.php) are gone.

          Does anyone know this problem, and please tell me: how can I fix it?

            hi,
            i've the same problem...

            after too many hours searching, i conclude somthing:

            • session_start: initialize one new session. If you put session_start on page2, you will create a new session file, with different name if compared to the first session file created for page1.

            • session_id: return the session id for the current session.

            Now you can do this:

            page2:
            session_start();
            $temp=session_id();
            echo $temp;

            Here you can observe that second session file are empty. But the first, no. It ocurr because on page1, you create and session one variable. But on page2, you only try to print.

            Now i suppose that if i pass one id to session_id(), before the session_start. It will assume one session file that already created. You anderstand me? The session file already exist, because was created for page1. And need only to be pass for page2.

            I'll try this, and if i get success, i'll write here..

            Perrone

              hi,

              thanks so far, I will look at it to and report my results/problems here.

              Martijn.

                Hi (again)!

                I noticed something different. I did the following:

                page1.php:
                <?
                session_start();
                $_SESSION['username'] = "test";
                echo "<a href='test2.php'>Go >></a>";
                ?>

                page2.php:
                <?
                session_start();
                $temp = session_id();
                echo $temp;
                ?>

                Oke, when I load page1.php, I get a session-file in my /tmp directory. This file is 20kB and contains the registered session variable.
                Next when I follow the link on page1.php, yhe browsers loads page2 and it shows the session id of the session made in page1 (this is allright, I think). But... when I look in /tmp the SAME file now is 0kB!!!
                So when I "restart" the same session with session_start(); on page2, it empty's its session-file!!!

                Now, how can I ever use my session variables???

                Please help me (/us)!

                Thanks.

                  Try:

                  page1.php

                  <?
                  $myvariable = "Welcome to my session";
                  session_register("myvariable");
                  ?>

                  page2.php
                  <?
                  session_start();
                  echo $myvariable;
                  ?>

                  HTH

                  T.

                    Thanks,

                    but still got the same problem (empty session-file)-thing.

                    :-(

                    Martijn

                      Martijn,

                      You have prolly heard this a million times: "try this!"

                      I suspect your problem has to do with cookies. You'll need to pass in the session ID yourself:

                      page1.php

                      <?php

                      session_start();
                      // blah blah ...
                      // set variabless...

                      echo "<a href=\"page2.php?" . SID . "\">Page 2</a>";

                      ?>

                      page2.php

                      <?php

                      session_start();
                      // print session vars

                      ?>

                      Basically you are sending the session_id (SID) through the URL to page2 so that it starts the SAME sessions (and thus reads the variables).

                      -sridhar

                        My my...this sounds like me about a month ago. There was nothing I could do to get sessions working...including Sridhar's advice allthough I recommend you try it. The 'Debian flavored' Web server that the problem was being manifested on was serving a lot of sites and the disk space was getting a bit dry. We were experiencing error logs indicating that our server was trying to open to many files simultaneously. Basically, we installed soem harddrives and the problem with sessions went away ;-) So I would ask, is your server experiencing heavy resource demands? The part that is a bit confusing to me is the /tmp folder issue. I don't believe our little problem exhibited that syptom you describes. Anyway, I can't understand how frustrating it can be to have sessions just quite working inexplicably ;-)

                        Good luck!!!

                          Hi

                          Which version of PHP using?

                          Using $_SESSION["foo"] in PHP 4.2.1,
                          you have to set register_globals =OFF in php.ini.
                          If register_globals = On,
                          Session_register["foo"] is better .

                            Write a Reply...