> Jeg står i den situation at jeg skal bruge sessions i PHP, jeg
> vil ihvertfald gerne prøve
Hvis du har mulighed for at bruge mySQL, kan du sikkert bruge min vedlagte
funktion.
Det er en udvidelse af den indbyggede sessionsdel i PHP, men min funktion
virker også for brugere, som har slået cookies fra.
--
Mvh Jacob Volstrup
-
www.netspecialisten.dk -
Her begynder koden så:
------------------------
CREATE TABLE sessions (
id bigint(20) unsigned NOT NULL auto_increment,
php_sessid varchar(32) NOT NULL default '',
time varchar(32) NOT NULL default '',
ip varchar(15) NOT NULL default '',
adress varchar(60) NOT NULL default '',
browser varchar(120) NOT NULL default '',
active tinyint(1) NOT NULL default '0',
PRIMARY KEY (id),
UNIQUE KEY id (id),
KEY id_2 (id),
KEY id_3 (id)
) TYPE=MyISAM;
function session_update () {
# * * * * * * * * * * * #
# session_update #
# By Jacob Volstrup #
# volstrup@avanceret.dk #
# * * * * * * * * * * * #
global $REMOTE_ADDR, $HTTP_USER_AGENT;
session_start();
$ip = $REMOTE_ADDR;
$adresse = gethostbyaddr($ip);
$browser = $HTTP_USER_AGENT;
$sessId = session_id();
$time = time();
$newtime = $time + 3600; //timeout in seconds
$sql = "UPDATE sessions
SET aktiv = '0'
WHERE time < '$time'";
mysql_query($sql);
if (!SID) {
$sql = "SELECT * FROM sessions
WHERE php_sessid = '$sessId'
AND time > '$time'
AND active = '1'";
} else {
$sql = "SELECT * FROM sessions
WHERE ip = '$ip'
AND adress = '$adress'
AND browser = '$browser'
AND time > '$time'
AND active = '1'";
}
$data = mysql_query($sql);
if ($sessionsdata = mysql_fetch_array($data)) {
//session exists
$sessions_id = $sessionsdata["id"];
$sql = "UPDATE sessions
SET time = '$newtime'
WHERE id = '$sessions_id'";
mysql_query($sql);
} else {
//session doesn't exist
$sql = "INSERT INTO sessions (time, ip, adress, browser, active,
php_sessid)
VALUES('$newtime', '$ip', '$adress', '$browser', '1',
'$sessId')";
mysql_query($sql);
$sessions_id = mysql_insert_id();
}
return $sessions_id;
}