by Martin Tsachev
Home » Tutorials » PHP
|
|
8. Setting
the session
function _setSession(&$values,
$remember, $init = true) {
$this->id = $values->id;
$_SESSION['uid'] = $this->id;
$_SESSION['username'] = htmlspecialchars($values->username);
$_SESSION['cookie'] = $values->cookie;
$_SESSION['logged'] = true;
if ($remember) {
$this->updateCookie($values->cookie,
true);
}
if ($init) {
$session = $this->db->quote(session_id());
$ip = $this->db->quote($_SERVER['REMOTE_ADDR']);
$sql = "UPDATE member SET
session = $session, ip = $ip WHERE " .
"id = $this->id";
$this->db->query($sql);
}
}
This method sets the session variables and if requested
sends the cookie for a persistent login, there is also a
parameter which determines if this is an initial login (via
the login form/via cookies) or a subsequent session check.
9. Persistent logins
If the visitor requested a cookie will be send to allow
skipping the login procedure on each visit to the site.
The following two methods are used to handle this situation.
function updateCookie($cookie, $save)
{
$_SESSION['cookie'] = $cookie;
if ($save) {
$cookie = serialize(array($_SESSION['username'],
$cookie) );
set_cookie('mtwebLogin', $cookie, time() + 31104000,
'/directory/');
}
}
[ previous page ] [ next
page ]
|
|
 |
|