Setting $_SESSION in PHP 5

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SpiritBreaker
    New Member
    • Jan 2008
    • 4

    Setting $_SESSION in PHP 5

    I am having problems getting the $_SESSION to set, the $_SESSION will work fine when its first gets the value, however, when I go to another page, the $_SESSION then loses its value. So if any one can take a look at my code and see if there is any problems, that would be good.

    Thanks

    Here is my code

    [PHP]
    <?php if (!isset($_SESSI ON))
    session_start() ;

    $loginFormActio n = $_SERVER['PHP_SELF'];

    if (isset($_POST['txtUserName']))
    {
    mysql_select_db ($database_con, $con);
    $LoginRS = mysql_query("SE LECT * FROM User WHERE passwd='".$_POS T['txtPassword']."' AND login='".$_POST['txtUserName']."'" , $con or die(mysql_error ());
    $row_loginFound User = mysql_fetch_ass oc($LoginRS);
    $loginFoundUser = mysql_num_rows( $LoginRS);

    if ($loginFoundUse r > 0)
    {
    //declare three session variables and assign them
    $_SESSION['conUserID'] = $row_loginFound User['UserID'];
    $_SESSION['UserEMail'] = $row_loginFound User['Email'];
    $_SESSION['UserName'] = $row_loginFound User['FName']." ".$row_loginFou ndUser['LName'];
    }//end of if
    }//end of if ?>


    <form action="<?php echo $loginFormActio n; ?>" method="post" name="frmLogin" id="frmLogin">
    <table border="0" cellpadding="0" cellspacing="0" >
    <tr>
    <td align="left">
    <br/><label>My Business Continuity <br />Plans</label>
    </td>
    </tr>

    <?php //checks if the user log in
    if (isset($_SESSIO N['conUserID']))
    {
    echo "<tr>
    <td align=\"left\">
    <a href=\"Page2.ph p?UserID=".$_SE SSION['conUserID']."\">Go To Page 2</a>
    <br/><br/>
    </td>
    </tr>
    </td></tr>";
    }//end of if
    else
    {
    echo "<tr>
    <td>
    <label>User Name:</label>
    </td>
    </tr>
    <tr>
    <td>
    <input name=\"txtUserN ame\" type=\"text\" id=\"txtUserNam e\" />
    </td>
    </tr>
    <tr>
    <td>
    <label>Password :</label>
    </td>
    </tr>
    <tr>
    <td>
    <input name=\"txtPassw ord\" type=\"password \" id=\"txtPasswor d\" />
    </td>
    </tr>";
    }//end of else?>
    </table>[/PHP]
    Last edited by SpiritBreaker; Jan 2 '08, 11:48 PM. Reason: forgot the Table end tag
  • shreedhan
    New Member
    • Jul 2007
    • 52

    #2
    I think the problem is here

    Originally posted by SpiritBreaker

    [PHP]
    <?php if (!isset($_SESSI ON))
    session_start() ;
    [/PHP]

    You need to start a session in every page, even if you are just accessing previous values from the _SESSION array.

    So, initially a session will be started since $_SESSION is empty. But next time, $_SESSION array is set. So the code above doesn't start a session, if the array is set.

    How about trying just
    [PHP]session_start() ;[/PHP]

    It would start a session in every page.
    I think that would help

    Comment

    • SpiritBreaker
      New Member
      • Jan 2008
      • 4

      #3
      I have try that suggestion however it is still not working

      Originally posted by shreedhan
      I think the problem is here

      You need to start a session in every page, even if you are just accessing previous values from the _SESSION array.

      So, initially a session will be started since $_SESSION is empty. But next time, $_SESSION array is set. So the code above doesn't start a session, if the array is set.

      How about trying just
      [PHP]session_start() ;[/PHP]

      It would start a session in every page.
      I think that would help

      Comment

      • SpiritBreaker
        New Member
        • Jan 2008
        • 4

        #4
        I am also using PHP version 5.2.4 if that makes any differance

        Comment

        • sumeetk
          New Member
          • Dec 2007
          • 6

          #5
          Hi,

          I m using PHP Version 5.2.5 and it is working for, and this is what i did in every page i include

          [PHP]session_start() ;

          if ($_SESSION['username'] == '')
          {
          include "loginerror.php ";
          die;
          exit;
          break;
          }
          $username = $_SESSION['username'];[/PHP]

          My login.php has this

          [PHP]$username = $_POST[username];

          $username = strtolower($use rname);
          session_start() ;

          $_SESSION['username'] = $username;[/PHP]

          Hope this helps

          Regards,
          Sumeet

          Comment

          • shreedhan
            New Member
            • Jul 2007
            • 52

            #6
            Have you included
            session_start() ;

            in another page or not?

            You need to start a session in another page as well.

            Comment

            • spudse
              New Member
              • Jan 2008
              • 11

              #7
              You can leave the isset() criteria out, just start every page with

              [code=php] session_start() ; [/code]

              You have to make sure that you that there is no character or space or anything outputted before you use the session_start() function (this will output an error also, about output before the headers have been send).

              Also make sure that your browser accepts cookies (sessions are actually cookies until you close your browser). I know that Opera has a nice function to browse cookies.

              Comment

              • SpiritBreaker
                New Member
                • Jan 2008
                • 4

                #8
                Sorry, for the late reply.

                I have found the problem, the server for some reason keeps changing the session ids on each page on the site.

                Comment

                • Atli
                  Recognized Expert Expert
                  • Nov 2006
                  • 5062

                  #9
                  Hi.

                  The most likely reason for the server refreshing the ID is that it cannot find your previous session, or that your browser is unable to store the session ID.

                  Make sure that the user that is running PHP has write privileges on the directory used to store the session data. You can find the location of that directory in your php.ini under "session.save_p ath". Also make sure the "session.use_co okies" directive is set to On.
                  If your server is using Windows, the "session.save_p ath" directive must be set.

                  Comment

                  Working...