11-04-2009, 17:39
|
|
|
חבר מתאריך: 06.06.07
הודעות: 749
|
|
מצאתי אחת שעברת על MYSQL
אבל יש לי בעיה אני לא מצליח ליצור חיבור עם ה DB
זאת הפונקציה
קוד PHP:
<?php //echo 'עברית'; class SessionManager { var $life_time; function SessionManager() { // Read the maxlifetime setting from PHP $this->life_time = get_cfg_var("session.gc_maxlifetime"); // Register this object as the session handler session_set_save_handler( array( &$this, "open" ), array( &$this, "close" ), array( &$this, "read" ), array( &$this, "write"), array( &$this, "destroy"), array( &$this, "gc" ) ); } function open( $save_path, $session_name ) { global $sess_save_path; $sess_save_path = $save_path; // Don't need to do anything. Just return TRUE. return true; } function close() { return true; } function read( $id ) { $data = ''; // Set empty result // Fetch session data from the selected database $time = time(); $newid = mysql_real_escape_string($id); $sql = "SELECT session_data FROM sessions WHERE session_id = '".$newid."' AND expires > ".$time; $rs = mysql_query($sql); $a = mysql_num_rows($rs); if($a > 0) { $row = mysql_fetch_assoc($rs); $data = $row['session_data']; } return $data; } function write( $id, $data ) { // Build query $time = time() + $this->life_time; $newid = mysql_real_escape_string($id); $newdata = mysql_real_escape_string($data); $sql = "REPLACE sessions (session_id,session_data,expires) VALUES('".$newid."','".$newdata."', ".$time.")"; $rs = mysql_query($sql); return TRUE; } function destroy( $id ) { // Build query $newid = mysql_real_escape_string($id); $sql = "DELETE FROM sessions WHERE session_id =".$newid; mysql_query($sql); return TRUE; } function gc() { // Garbage Collection // Build DELETE query. Delete all records who have passed // the expiration time $sql = 'DELETE FROM sessions WHERE expires < UNIX_TIMESTAMP();'; mysql_query($sql); // Always return TRUE
return true; }
} ?>
ככה מפעילים אותה
קוד PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head>
<body> <?php require_once("onlineusersMYSQL.php");
$sess = new SessionManager();
session_start(); ?> </body> </html>
|