php会话变量不一致

时间:2016-03-25 17:07:50

标签: php session variables persistent

我有一个php表单(称之为Home.php)和提交(方法Post),我有一个单独的PHP脚本验证信息(称之为Validate.php)。

验证后,会在Validate.php中初始化/设置会话变量。 设置这些变量后,我将使用标题页中的标题('位置:home.php')重定向回home.php页面。

所有这一切都很完美,我可以访问会话变量并在home.php页面上显示它,但是一旦我点击刷新,变量值就会消失。即使我不刷新home.php,如果我转到服务器上的另一个页面并尝试访问会话变量,它们都是空白/未设置。

几点: 我在所有页面和最开始都有Session_Start()。 会话保存路径配置值是/ tmp,这似乎不是问题,因为我可以在第一次尝试时设置和检索会话值,如上所述。

我搜索了Stackoverflow和谷歌,但无法找到任何东西。你是我最后的选择。希望有人可以帮助我。

提前致谢。

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';
        }

    }

?>

1 个答案:

答案 0 :(得分:0)

好的......这是我在代码中找到的内容,一旦我在此处发布。在Home.php的代码中有一个会话破坏,这就是导致所有这些Shenanigans的原因。

我删除了Session_Destroy()和Onclick事件,这一切都完美无缺,就像它的设计一样。

感谢迈克和丹的时间。