这是我的第一页。我无法将我的变量转到下一页index.php($ _SESSION ['admin_email'])。变量赋值在第一页上正常工作,在index.php中它总是重定向到login.php
<?php
error_reporting(0);
session_start();
include_once('../_inc/_class/Users.php');
$dbUsers = new Users();
$uname = htmlspecialchars($_POST['username']);
$upass = md5($_POST['password']);
if($uname != "" && $upass != "") {
$user = $dbUsers->selectAll("`password` = '".$upass."' and `email` = '".$uname."' and user_type = 'admin'", "1", "");
/*print_r($user);*/
if(!empty($user) && $user[0]->email == $uname && $user[0]->password == $upass) {
$_SESSION['admin_email'] = $user[0]->email;
/*var_dump($_SESSION['admin_email']);*/
}
else {
$_SESSION = array();
session_destroy();
}
}
die(header('Location:../index.php'));
?>
的index.php
<?php
error_reporting(E_ALL);
session_start();
if(!isset($_SESSION['admin_email'])) {
die(header('Location:./login.php'));
}
?>
答案 0 :(得分:0)
从您的代码中,看起来login.php位于子文件夹内,而不是位于index.php所在的根文件夹中。
您正在从子文件夹设置会话值,如果您的php.ini设置或包含的类(Users.php)中的某些session_set_cookie_params()
正在故意为该子文件夹设置会话路径,它将使会话变量在那之外无法进入。
因此,请尝试添加session_set_cookie_params
并将$path
参数设置为/
然后它将可用于根文件夹和其中的所有子文件夹。
有关详细信息,请查看session_set_cookie_params
答案 1 :(得分:-3)
使用
if(!session_id()) session_start();
而不是使用普通的
session_start();