我正在开发一个网络应用程序,大致使用下面链接中概述的MVC架构
http://www.phpro.org/tutorials/Model-View-Controller-MVC.html
我已经创建了一个用于登录的自定义类,如下所述。用户对象在 index.php 中初始化。所有请求均使用 mod_rewrite 通过 index.php 进行路由。
如何让用户坚持下去?
<?php
class user
{
//connection setup
private $username = '';
private $password = '';
private $addressnumber = '';
public $connection = false;
private $host = '*LOCAL';
private $options = array(
'i5_naming' => DB2_I5_NAMING_ON,
'i5_libl' => 'CLTSEC CLTPAY CLTDTA CLTCOM QRY_FILES'
);
public function login($username,$password) {
//check login variables not null
if (isset($username) && isset($password)) {
//get form parameters
$this->username= $username;
$this->password= $password;
$this->connection = db2_pconnect($this->host,$this->username,$this->password,$this->options);
if (!$this->connection) {
die('Could not connect');
return false;
}
else {
//get addressnumber from username
$sql='select F0092.ULAN8 from F0092 where ULUSER=\''.$this->username.'\'';
$stmt= db2_prepare($this->connection,$sql);
$result= db2_execute($stmt);
while ($row = db2_fetch_assoc($stmt)) {
$this->addressnumber = $row['ULAN8'];
}
return true;
}
}
}
//disconnect from database
function logout() {
db2_close($this->connection);
return true;
}
}
?>
答案 0 :(得分:0)
问题是对象变量不会跨页面持续存在。我需要一种方法来做到这一点。
我重新设计了类,以便在__construct期间使用cookie来维护页面之间的身份验证。在成功进行身份验证后,将从数据库加载属性,而不是在会话中保留属性。