dongnianchou7047 2016-09-15 06:06 采纳率: 0%
浏览 40

PHP SESSION变量第一次不起作用

Session variables are not set at the first time. From action.php it goes to employee.php But in employee.php $_SESSION['EmpID'] shows nothing when I log in for the 1st time. If I log out and log in again then works fine. works fine in localhost.

action.php

<?php
session_start(); 
if(isset($_POST['UserID'])&&isset($_POST['Password']))
{
$id = $_POST['UserID'];
$pass = $_POST['Password'];
$result = mysqli_query($con,"select * from auth_det where     UserName='$id' and Password='$pass'");
if(mysqli_num_rows($result)>0)
{
$row = mysqli_fetch_array($result);
//echo $row['UserID'];
//echo $row['Password'];
                $_SESSION['UserName']=$id;
                $_SESSION['EmpID'] = $row['EmpID'];
        $_SESSION['is_auth'] = true;
            $_SESSION['User'] = "Emp";
               echo "<script type='text/javascript'>window.location.href = 'Employee/employee.php'; </script>";
                 exit();

}

logout.php
<?php
session_start(); 
session_destroy();
echo "<script type='text/javascript'> document.location = '../login.php'; </script>";
?>


employee.php

session_start(); 
$EmpID=$_SESSION['EmpID'];
echo "EmpID=".$EmpID;
if(!isset($_SESSION['EmpID']))
{
echo "<script type='text/javascript'> window.location.href = 'logout.php'; </script>";
}
  • 写回答

2条回答 默认 最新

  • doudeng2025 2016-09-15 06:31
    关注

    try to move the session_start() to the action.php file. At first connect php doesn't create a session at action.php Then you get redirected and employee.php starts the session. If you try this again later a session is already open so you have no problems at the action php.

    Also think about switching the Javascript Redirect to an header redirect since it still works if the Client disabled Javascript

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料