Session variables and header("location:xxx");

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • webgenius
    New Member
    • Apr 2007
    • 15

    Session variables and header("location:xxx");

    I'm designing a small site and I have this structure in mind:

    index.php
    ---------------
    login fields. User enters login info. LoginCheck.php is called.

    LoginCheck.php
    -----------------------
    Login info are POST collected and checked in DB. If successful, session variables are set. Then redirected to index.php using header("locatio n:index.php");

    In index.php. I'm checking is the session variable is registered. But it looks like the session variable is lost during redirection using header.

    Is there any workaround for this?
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    If we saw your (relevant) code then we might be able to suggest something.

    Comment

    • webgenius
      New Member
      • Apr 2007
      • 15

      #3
      Originally posted by markusn00b
      If we saw your (relevant) code then we might be able to suggest something.
      [php]
      $count=mysql_nu m_rows($result) ;
      // If result matched $myusername and $mypassword, table row must be 1 row

      if($count==1){
      // Register $myusername, $mypassword and redirect to file "LoginOK.ph p"
      session_registe r("myusername") ;
      session_registe r("mypassword") ;
      header("locatio n:index.php");
      }
      else {
      echo "Wrong Username or Password";
      }[/php]
      Last edited by ronverdonk; Mar 15 '08, 01:58 PM. Reason: code tags

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Originally posted by webgenius
        $count=mysql_nu m_rows($result) ;
        // If result matched $myusername and $mypassword, table row must be 1 row

        if($count==1){
        // Register $myusername, $mypassword and redirect to file "LoginOK.ph p"
        session_registe r("myusername") ;
        session_registe r("mypassword") ;
        header("locatio n:index.php");
        }
        else {
        echo "Wrong Username or Password";
        }
        Ive never used session_registe r.
        Do you start with session_start() on every page?

        Comment

        • ronverdonk
          Recognized Expert Specialist
          • Jul 2006
          • 4259

          #5
          Please enclose your posted code in [code] tags (See How to Ask a Question).

          This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

          Please use [code] tags in future.

          MODERATOR

          Comment

          • webgenius
            New Member
            • Apr 2007
            • 15

            #6
            Originally posted by markusn00b
            Ive never used session_registe r.
            Do you start with session_start() on every page?
            I have used session_start() only in the login page. I think it is enough to start a session in login page, and not on every page.

            You said you have never used session_registe r. Then how do you work around with session variables? Can you please help me on this?

            Comment

            • Markus
              Recognized Expert Expert
              • Jun 2007
              • 6092

              #7
              Originally posted by webgenius
              I have used session_start() only in the login page. I think it is enough to start a session in login page, and not on every page.

              You said you have never used session_registe r. Then how do you work around with session variables? Can you please help me on this?
              You need to use session_start() ; on every page!

              When i use sessions i do this:
              [php]
              # login.php
              session_start() ;
              # log in script
              # if(login == successful)
              $_SESSION['logged_in'] = true;
              [/php]
              [php]
              # member.php
              session_start() ; # MUST BE INCLUDED!
              if(isset($_SESS ION['logged_in']))
              {
              # member is logged in
              }
              else
              {
              # member isnt logged in
              }
              [/php]
              session_start() ; has to be on every page that you are using sessions in.
              :)

              Comment

              Working...