2017-05-26 52 views
0

这是我的第一页。我只是无法让我的变量到下一页index.php($ _SESSION ['admin_email'])。变量赋值工作在第一页上细,在index.php中它总是重定向到的login.phpPHP会话变量不能在两个页面工作

<?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

的新页面上,做了'的print_r($ _ SESSION);',以了解更多信息 – Akintunde007

+0

你'在其他部分确保您不小心使用'session_destroy()你的if语句? –

+0

你确定你的登录脚本没有经过if的else部分吗?尝试放置一个死亡()在其他.... –

回答

-3

使用

if(!session_id()) session_start(); 

而是采用普通的

session_start(); 
+1

不是问题的答案 –

+0

正确地查看OP的代码。 OP可能正在执行'session_destroy'语句。请确认之后再给予解决方案 – Akintunde007

+0

对不起我的坏.... –

0

从你的c ode,它看起来像login.php位于子文件夹内,而不是在index.php所在的根文件夹中。

您正在设置从子文件夹,如果你的php.ini设置或会话值中包含类(Users.php)一些session_set_cookie_params()是故意设置会话路径该子文件夹,这会让外界会话变量不可访问。

所以尝试添加session_set_cookie_params并设置$path参数去/

然后将它可用于根文件夹,里面的所有子文件夹。

欲了解更多信息,请查看session_set_cookie_params