Search code examples
phpsessionvariablespersistent

php sessions variables inconsistent


I have a php form (call it Home.php) and on submit (method Post), I have information being validated in a separate php script(call it Validate.php).

Once validated, session variables are being initialized/set in Validate.php. Once these variables are set, I'm redirecting back to the home.php page using header('Location: home.php') in the validate page.

All this works perfectly, and I can access the session variable and display it on the home.php page, but as soon as I hit refresh, the variable values are gone. Even if I don't refresh home.php, if I go to another page on the server and try to access the session variables, they are blank/not set.

Few points: I have Session_Start() in all my pages and at the very beginning. The session save path config value is /tmp and that doesn't seem to be the issue, because, I can set and retrieve the session values on the first attempt like I mentioned above.

I searched Stackoverflow and google, but couldn't find anything. You are my last resort. Hope someone can help me here.

Thanks in advance.

Home.php::

<?php session_start();
  echo '<!DOCTYPE html>';
?>

And some HTML CODE HERE FOR DISPLAYING THE PAGE....


if (!isset($_SESSION['dbuserNm'])) {
echo


'<li class="dropdown">
                <a href="#" class="dropdown-toggle hvr-bounce-to-right" data-toggle="modal" role="button" aria-haspopup="true" aria-expanded="false" data-target="#LogInModal" onclick="KillSession()"><b>Log In</b><span class="caret"></span></a>
            </li>

            <!-- Modal -->
                        <div class="modal fade" id="LogInModal" tabindex="-1" role="dialog"
                             aria-labelledby="myModalLabel" aria-hidden="true">
                            <div class="modal-dialog">
                                <div class="modal-content">
                                    <!-- Modal Header -->
                                    <div class="modal-header">
                                        <button type="button" class="close"
                                           data-dismiss="modal">
                                               <span aria-hidden="true">&times;</span>
                                               <span class="sr-only">Close</span>
                                        </button>
                                        <h4 class="modal-title" id="myModalLabel">
                                            Authentication
                                        </h4>
                                    </div>

                                    <!-- Modal Body -->
                                    <div class="modal-body">

                                        <form class="form-horizontal" role="form" action="Validate.php" method="POST">
                                          <div class="form-group">
                                            <label  class="col-sm-2 control-label"
                                                      for="inptUserID">User ID</label>
                                            <div class="col-sm-10">
                                                <input type="UserID" class="form-control"
                                                id="inptUserID" name="inptUserID" placeholder="User ID"/>
                                            </div>
                                          </div>
                                          <div class="form-group">
                                            <label class="col-sm-2 control-label"
                                                  for="inputPassword3" >Password</label>
                                            <div class="col-sm-10">
                                                <input type="password" class="form-control"
                                                    id="inptPsswd" name="inptPsswd" placeholder="Password"/>
                                            </div>
                                          </div>
                                          <div class="form-group">
                            <!--  <div class="form-group">
                                    <div class="col-sm-offset-2 col-sm-10">
                                      <div class="checkbox">
                                        <label>
                                            <input type="checkbox"/>';
                                              if ($_SESSION['ValidLogInSw'] = 0) {
                                                 echo "Invalid UserID/Password, Please Re-try";
                                              } else {
                                                echo '<script type="text/javascript"> $(this).dialog("close");</script>';
                                              }
                                        echo
                                        '</label>
                                      </div>
                                    </div>
                                  </div>
                                          </div> -->
                                          <div class="form-group">
                                            <div class="col-sm-offset-2 col-sm-10">
                                              <button type="submit" class="btn btn-default">Sign in</button>
                                            </div>
                                          </div>
                                        </form>

                                    </div>

                                    <!-- Modal Footer -->
                                    <div class="modal-footer">
                                        <button type="button" class="btn btn-default"
                                                data-dismiss="modal">
                                                    Close
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>';
        } else {
            echo '<li class="dropdown">
                <a href="#" class="dropdown-toggle hvr-bounce-to-right" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><b>'. $_SESSION['dbuserNm']. '\'s Account</b><span class="caret"></span></a>
                <ul class="dropdown-menu">
                <li><a href="#" class="hvr-bounce-to-right">Account Management</a></li>
                <li><a href="#" class="hvr-bounce-to-right">Stats1</a></li>
                <li><a href="#" class="hvr-bounce-to-right">Stats2</a></li>
                </ul>
            </li>';
        }
    ?>

Validate.php:

Validate.php:

    <?PHP
session_start();
  //header('Location: ' . $_SERVER['HTTP_REFERER']);

    header('Location: Home.php');

    if (isset($_POST["inptUserID"]) && !empty($_POST["inptUserID"])){

        //*echo 'Its Definitely Here';

        $lclidpw = $_POST['inptUserID'];

        $id = $_POST['inptUserID'];
        $pw = $_POST['inptPsswd'];

        $Cnxn = mysql_connect("localhost:portnumber", "UserID", "Psswd");//format is id||pw;

        if($Cnxn){

         mysql_select_db('DatabaseNameHere');

         $Qry = "SELECT CONCAT(CAST(a.UserId AS CHAR), '||' ,TRIM(u.User_FName)) FROM TableNameHere u INNER JOIN UsrAuth a ON a.UserId = u.UserId WHERE a.UserName = TRIM('" . $id ."') AND a.Psswd =  TRIM('" .$pw . "');" ;

         $Results = mysql_query($Qry) or die('Sql Error...'.mysql_error());

             if(mysql_num_rows($Results) > 0){

                 //echo 'Its here';
                //echo $Qry;

                //All this is new:

                $Rslt_for_Session = mysql_result($Results,0);

                $_SESSION['dbuserId'] = substr($Rslt_for_Session,0, strpos($Rslt_for_Session, '||'));
                $_SESSION['dbuserNm'] = substr($Rslt_for_Session, strpos($Rslt_for_Session ,'||')+2,strlen($Rslt_for_Session)-strpos($Rslt_for_Session ,'||')+1);
                $_SESSION['TimeSet'] = time();
                $_SESSION['ValidLogInSw'] = 1;

                //echo $_SESSION['dbuserNm'];

             }
             else{

                $_SESSION['ValidLogInSw'] = 0;


             }

        }
        else{
         echo '*Connection Error...please try later';
        }

    }

?>

Solution

  • Ok...Here's what I found in my code, once I posted it here. There is a session destroy in the code in Home.php and that's what has caused all these Shenanigans.

    I removed the Session_Destroy() and the Onclick event, and it all works perfectly, like its designed to.

    Thanks Mike and Dan for your time.