0

我已经查看了我的问题的现有答案。

我已经在整个过程中回显了该值,直到“header('Location”指令)值保持不变。

我不认为这是针对类似问题所建议的序列化问题......

以下是该类的相关位:

class clsSetUser {
  protected $UserID = 0;

  public function initUser($id) {
     // get user details from database

     $this->setUserID($id);
     // etc...
  }

  private function setUserID($value) { $this->UserID = $value; }
  public function getUserID() { return $this->UserID; }
}

常见的.php:

if(unset($clsUser)) $clsUser = new clsSetUser;

登录-exec.php:

$clsUser->initUser($id);   

header("Location: somewhere.php");

某处.php:

echo $clsUser->getUserID();

// here it equals 0

有任何想法吗?“标题”会序列化所有内容吗?

4

1 回答 1

0

This is because PHP is actually starting from a clean slate in somewhere.php.

header("Location: somewhere.php"); sends a command the browser to connect to a different page. In this page non of variables of the previous page are available in PHP.

You need to set the userId in the $_SESSION so that you can reload the user from the database when he visits somewhere.php.

login-exec.php

$clsUser->initUser($id);   
$_SESSION['user_id'] = $id;
header("Location: somewhere.php");

somewhere.php

$clsUser->initUser($_SESSION['user_id']);
于 2013-06-23T14:16:51.540 回答