在 PHP MVC 中保留 user/db 对象
Persisting user/db object in PHP MVC
我正在开发一个网络应用程序,大致使用下面 link 中概述的 MVC 架构
http://www.phpro.org/tutorials/Model-View-Controller-MVC.html
我创建了一个自定义 class 用于登录,如下所述。用户对象在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;
}
}
?>
问题是对象变量不会跨页面保留。我需要一种方法来做到这一点。
我重新设计了 class,以便在 __construct 期间使用 cookie 来维护页面之间的身份验证。身份验证成功后,属性将从数据库加载,而不是保留在会话中。
我正在开发一个网络应用程序,大致使用下面 link 中概述的 MVC 架构
http://www.phpro.org/tutorials/Model-View-Controller-MVC.html
我创建了一个自定义 class 用于登录,如下所述。用户对象在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;
}
}
?>
问题是对象变量不会跨页面保留。我需要一种方法来做到这一点。
我重新设计了 class,以便在 __construct 期间使用 cookie 来维护页面之间的身份验证。身份验证成功后,属性将从数据库加载,而不是保留在会话中。