2012-03-20 86 views
0

因此,我有一个登录脚本,它调用一个自定义用户类,用户登录并设置会话。在login.php页面的redict上做一个var转储,确认用户已经成功登录,并显示友好的欢迎消息。PHP会话在登录时存在,但不在另一页上登录

设置会话的代码是:

private function setIsLoggedIn($credentials) { 
    if (!isset($_SESSION)) { 
     session_start(); 
    } 
    $_SESSION['verified'] = $credentials; 
    session_write_close(); 
} 

这是对的login.php页面做工精细,该函数的自变量是:

// assign user info from db to the session 
     $this->setIsLoggedIn(array('UserId' => $user->userid, 
      'UserName' => $user->username, 
      'RoleId' => $user->roleid)); 

这是login.php中表格回发

// if the form was submitted 
if (isset($_POST['submit'])) { 

// Set local variables from $_POST array elements (userlogin and userpassword) or empty strings 
    $userLogin = (isset($_POST['userlogin'])) ? trim($_POST['userlogin']) : ''; 
    $userPassword = (isset($_POST['userpassword'])) ? trim($_POST['userpassword']) : ''; 

    //assign values to array and get user object 
    $authArray = array('UserName' => $userLogin, 'Password' => $userPassword); 
    $user = new Users($authArray); 

    if ($user->getUserId() > 0) { //If credentials check out 

     // redirect the user to SESSION value set from common class 
     if (isset($_SESSION['prev_page'])) 
     { 
      header('location:' . $_SESSION['prev_page']); 
      exit; 
     } 
     else 
     { 
      // Otherwise, assign error message to $msg 
      $msg = '<p><strong>Thank your for logging in.<br />You may now visit tools that are in development.</strong></p>'; 
     } 
    } 
    else 
    { 
     // Otherwise, assign error message to $msg 
     $msg = '<p><strong>Sorry, that username and password are not recognized.<br />Please try again.</strong></p>'; 
    } 
} 

现在我到另一个页面并执行$ var_dump($ _ SESSION);我总是别的东西,因为在页面上我设置这样的:

$_SESSION['prev_page'] = "http://".$_SERVER[SERVER_NAME]."/id/AphID/"; 

该文件的第一行是:

<?php 
session_start(); 

var_dump($_SESSION); 

这里是我的PHP会话设置:

Session Support enabled 
Registered save handlers files user 
Registered serializer handlers php php_binary wddx 

Directive Local Value Master Value 
session.auto_start Off Off 
session.bug_compat_42 On On 
session.bug_compat_warn On On 
session.cache_expire 180 180 
session.cache_limiter nocache nocache 
session.cookie_domain no value no value 
session.cookie_httponly Off Off 
session.cookie_lifetime 0 0 
session.cookie_path//
session.cookie_secure Off Off 
session.entropy_file /dev/urandom /dev/urandom 
session.entropy_length 16 16 
session.gc_divisor 1000 1000 
session.gc_maxlifetime 1440 1440 
session.gc_probability 1 1 
session.hash_bits_per_character 5 5 
session.hash_function 0 0 
session.name PHPSESSID PHPSESSID 
session.referer_check no value no value 
session.save_handler user files 
session.save_path /tmp /tmp 
session.serialize_handler php php 
session.use_cookies On On 
session.use_only_cookies Off Off 
session.use_trans_sid 1 1 

所以,基本上我的会话只在登录页面上存在,并且没有其他地方,并且当我调用Session_start();我总是得到一个不同的会议。有任何想法吗?

+0

会话哪里设置你$ _SESSION [ 'prev_page']?如果它在session_write_close调用后设置,则不起作用。 – 2012-03-20 21:46:45

回答

0

session_write_close()在保存数据后结束会话。下一次调用session_start()将创建一个新的会话。

如果您想在关闭它之后恢复会话,那么在关闭它之前,您需要将会话ID保存在某处。 $sid = session_id()您可以将其保存在数据库或cookie中。

然后恢复你可以调用session_id($sid); session_start();

+0

如何重新打开此会话? – 2012-03-20 21:54:58

+0

我更新了我的答案,以包含此问题的答案。 – Rob 2012-03-20 22:00:59