当我尝试这条线时:
unserialize($_SESSION['user']);
我收到此错误:类用户的对象无法转换为字符串
当我回应会话时,我得到:
O:4:"user":7:{s:9:"accountId";s:1:"2";s:9:"firstName";s:12:"Ronald";s:8:"lastName";s:5:"Test";s:5:"email";s:22:"blabla@hotmail.com";s:10:"postalCode";s:7:"J0L 8G0";s:8:"username";s:12:"testuser";s:4:"role";s:1:"0";}
这是用户的课程:
class user {
var $accountId;
var $firstName;
var $lastName;
var $email;
var $postalCode;
var $username;
var $role;
/**
* @return mixed
*/
public function getAccountId()
{
return $this->accountId;
}
/**
* @param mixed $accountId
*/
public function setAccountId($accountId)
{
$this->accountId = $accountId;
}
/**
* @return mixed
*/
public function getFirstName()
{
return $this->firstName;
}
/**
* @param mixed $firstName
*/
public function setFirstName($firstName)
{
$this->firstName = $firstName;
}
/**
* @return mixed
*/
public function getLastName()
{
return $this->lastName;
}
/**
* @param mixed $lastName
*/
public function setLastName($lastName)
{
$this->lastName = $lastName;
}
/**
* @return mixed
*/
public function getEmail()
{
return $this->email;
}
/**
* @param mixed $email
*/
public function setEmail($email)
{
$this->email = $email;
}
/**
* @return mixed
*/
public function getPostalCode()
{
return $this->postalCode;
}
/**
* @param mixed $postalCode
*/
public function setPostalCode($postalCode)
{
$this->postalCode = $postalCode;
}
/**
* @return mixed
*/
public function getUsername()
{
return $this->username;
}
/**
* @param mixed $username
*/
public function setUsername($username)
{
$this->username = $username;
}
/**
* @return mixed
*/
public function getRole()
{
return $this->role;
}
/**
* @param mixed $role
*/
public function setRole($role)
{
$this->role = $role;
}
function __construct($accountId,$firstName,$lastName,$email,$postalCode,$username,$role){
$this->setAccountId($accountId);
$this->setFirstName($firstName);
$this->setLastName($lastName);
$this->setEmail($email);
$this->setPostalCode($postalCode);
$this->setUsername($username);
$this->setRole($role);
}
}
这是我创建对象的地方:
public function createUserObject($username){
session_start();
require("user.php");
include("connection.php");
$username = mysql_real_escape_string($username);
mysql_connect("$SQLhost", "$SQLusername", "$SQLpassword") or die("cannot connect");
mysql_select_db("$SQLdb_name") or die("cannot select DB");
$username = mysql_real_escape_string($username);
$sql = "SELECT * FROM Account WHERE username='$username'";
$result = mysql_query($sql);
$rows = mysql_fetch_assoc($result);
$user = new user($rows['accountId'],$rows['firstName'],$rows['lastName'],$rows['email'],$rows['postalCode'],$rows['username'],$rows['role']);
$_SESSION['user'] = serialize($user);
if($result){
return true;
}else{
return false;
}
}
谢谢,如果有人能解释我的问题。
答案 0 :(得分:1)
Mathieu,这是一个众所周知的问题。因此,如果您可以稍微修改您的代码,那将是一件好事。
请参阅以下帖子,了解一些想法:http://blog.pagalhost.com/codeigniter-php-serialize-unserialize-issues/
因此,您需要做的事情是以下方式
//To serialize
$result_string = base64_encode(serialize($array));
//To unserialize
$restored_array = unserialize(base64_decode($result_string ));
有关更多信息和想法,请查看PHP Manual - Unserialize Function