php会话不重定向

时间:2014-07-18 18:32:11

标签: php mysql session redirect

我正在使用php session作为我的实验之一。试图通过开发脚本来学习。但我已经堆积了。我不确定为什么php会话不起作用。当我提交正确的电子邮件地址和密码时,我登录页面重定向我的页面。如果设置了会话,我试图跳过登录页面。但它没有重定向。这是代码。即使没有错误日志输出。所以我对我的代码错误感到困惑。我正在寻找关于我的实验的专家建议。

<?php
session_start();
#Working With Session
if (isset($_SESSION['UserId'])) {
    # code...
    header('location:/management/index.php');
exit();
}
require_once("../configuration.php");
#Veriable collected From Login Panel
$Submitted_Email_Address=$_POST['email'];
$Submitted_Password=$_POST['password'];
$encrypt_Submitted_Password=md5($Submitted_Password);

#Lets gather Data From MySql Database
$con=new mysqli($hostname,$dbusername,$dbpass,$dbname);
if (mysqli_connect_errno($con)) {
    die('The connection to the database could not be established.');
}
$query="SELECT * FROM users WHERE Email_Address='$Submitted_Email_Address'";
$result=$con->query($query);
$row=$result->fetch_array();
$SQL_Password = $row['Password'];
#LookUp Mysql Database to Get username/user id to store in session
$SQL_UserID=$row['id'];
$SQL_UserName=$row['username'];
$SQL_FirstName=$row['First_Name'];
$SQL_LastName=$row['Last_Name'];
$SQL_Role=$row['Role'];
if ($encrypt_Submitted_Password!=$SQL_Password) {
       echo "Please check your email address and password again";
}
elseif ($encrypt_Submitted_Password == $SQL_Password) {
$_SESSION['UserID']=$GLOBAL['SQL_UserID'];
$_SESSION['UserName']=$GLOBAL['SQL_UserName'];
$_SESSION['FirstName']=$GLOBAL['SQL_FirstName'];
$_SESSION['LastName']=$GLOBAL['SQL_LastName'];
$_SESSION['Role']=$GLOBAL['SQL_Role'];
header('location:/management/index.php');
}

#Lets CLose All The MySql Connection
$result->free();
$con->close();
?> 

之后,我试图查看是否未设置会话,然后重定向页面。但它没有重定向。我尝试了以下代码来检查会话是否未设置让重定向页面。但没有重定向

session_start();
    #Working With Session
    if (!isset($_SESSION['UserId'])) {
        # code...
        header('location:/management/index.php');
    exit();
    }

3 个答案:

答案 0 :(得分:3)

您需要更改header来电:

header('Location: /management/index.php'); // notice capitalization and spacing; they matter.

答案 1 :(得分:0)

删除“!” from isset或putirect in else子句:

例如:

   session_start();
        #Working With Session
        if (**!**isset($_SESSION['UserId'])) {
            # code...
            header('location:/management/index.php');
        exit();
        }

答案 2 :(得分:0)

我猜你错过了一些如此愚蠢的东西。您的代码似乎是一个php文件,它从登录页面收集电子邮件地址和密码并验证它,然后将其重定向到另一个页面。 我认为您的登录页面(可能是login.html,access.html)不是重定向。所以你应该在登录页面中放入以下php代码块。不在login.php中,它仅用于验证输入的数据。快乐的编码,让你的心灵平静。 :) 你必须使用'UserID'而不是'UserId'

session_start();
#Working With Session
if (isset($_SESSION['UserID'])) {
    # code...
    header('location:/management/index.php');
exit();
}