ciao daniele e grazie anche a te, volevo usare la classe che ho postato per salvare le sessioni nel database, io momentaneamente uso uno script che usa i cooke , e nn so come modificarlo , ti posto lo script che uso io premeto che queso e lo script attualmente utilizzato da me,, che vorrei modificare
, come primo io ad ogni pagina includo il files check1.php che sarebbe queso
Codice PHP:
include "config.php";
include "funzioni.php";
$DB = new DB();
$DB->connect();
session_start();
$parti = explode("@@",$_COOKIE[login]);
$nick_utente_cookie = $parti[0];
$password_utente_cookie = $parti[1];
$verifico_user1 = mysql_query("SELECT *
FROM `utenti`
WHERE `email` ='$nick_utente_cookie'
AND `pass` = '$password_utente_cookie'
LIMIT 0 , 1");
$ok = mysql_num_rows($verifico_user1);
$_SESSION['autorizzato'] = $ok;
$autorizzato = $_SESSION['autorizzato'];
$_SESSION['nome_utente']= $nick_utente_cookie;
$nome_utente = $_SESSION['nome_utente'];
$verifico_admin = mysql_query("SELECT *
FROM `amministrator`
WHERE `email` ='$nick_utente_cookie'
AND `pass` = '$password_utente_cookie'
LIMIT 0 , 1");
$ok = mysql_num_rows($verifico_user1);
$_SESSION['autorizzato'] = $ok;
$autorizzato = $_SESSION['autorizzato'];
$_SESSION['nome_utente']= $nick_utente_cookie;
$nome_utente = $_SESSION['nome_utente'];
e questo e il files set.php
Codice PHP:
session_start();
if($_SESSION[login]!='' or $_SESSION[psw]!=''){
$cook = "$_SESSION[login]@@$_SESSION[psw]";
setcookie ("login", $cook);
header("Location: index.php");
}
else
{
header("Location: login.php");
}
volevo usare questo script per salvare le sessioni nel database anzi che uae i cooke , ed usare questa classe
Codice PHP:
class Session {
// session-lifetime
public $lifeTime;
function __construct ($db) {
// get session-lifetime
$this->lifeTime = get_cfg_var("session.gc_maxlifetime");
// open database-connection
$this->mdb2 =& MDB2::factory($db);
if (PEAR::isError($this->mdb2)) {
$php_errormsg .= $this->mdb2->getMessage();
$php_errormsg .= $this->mdb2->getDebugInfo();
}
session_set_save_handler(array(&$this, 'open'),
array(&$this, 'close'),
array(&$this, 'read'),
array(&$this, 'write'),
array(&$this, 'destroy'),
array(&$this, 'gc'));
register_shutdown_function('session_write_close');
session_start();
return true;
}
function open($savePath, $sessName) {
// get session-lifetime
$this->lifeTime = get_cfg_var("session.gc_maxlifetime");
return true;
}
function close() {
$this->gc(ini_get('session.gc_maxlifetime'));
// close database-connection
return $this->mdb2->disconnect();
}
function read($sessID) {
global $php_errormsg;
// fetch session-data
$query = "
SELECT session_data FROM sessions
WHERE session = '$sessID'
AND session_expires >
".time();
$result = $this->mdb2->queryOne($query);
// return data or an empty string at failure
if (MDB2::isError($result)) {
$php_errormsg .= $result->getMessage();
$php_errormsg .= $result->getDebugInfo ();
return false;
}
return $result;
}
function write($sessID,$sessData) {
global $php_errormsg;
// new session-expire-time
$newExp = time() + $this->lifeTime;
// is a session with this id in the database?
$query = "
SELECT * FROM sessions
WHERE session = '$sessID'
";
$result = $this->mdb2->query($query);
// if yes,
if($result->numRows()) {
// ...update session-data
$query = "
UPDATE sessions
SET session_expires = '$newExp',
session_data = '$sessData'
WHERE session = '$sessID'
";
}
// if no session-data was found,
else {
// create a new row
$query = "
INSERT INTO sessions (
session,
session_expires,
session_data)
VALUES(
'$sessID',
'$newExp',
'$sessData')
";
}
$result = $this->mdb2->exec($query);
// if something happened, return true
if (MDB2::isError($result)) {
$php_errormsg .= $result->getMessage();
$php_errormsg .= $result->getDebugInfo ();
return false;
} else {
// ...else return true
return true;
}
}
function destroy($sessID) {
global $php_errormsg;
// delete session-data
$query = "
DELETE FROM sessions
WHERE session = '$sessID'
";
$result = $this->mdb2->exec($query);
// if session was not deleted, return false,
if (MDB2::isError($result)) {
$php_errormsg .= $result->getMessage();
$php_errormsg .= $result->getDebugInfo ();
return false;
} else {
// ...else return true
return true;
}
}
function gc($sessMaxLifeTime) {
global $php_errormsg;
// delete old sessions
$query = "
DELETE FROM sessions
WHERE session_expires <
".time();
$result = $this->mdb2->exec($query);
// return affected rows
if (MDB2::isError($result)) {
$php_errormsg .= $result->getMessage();
$php_errormsg .= $result->getDebugInfo ();
}
return $result;
}
}
pero maledizzione non riesco a capire il concetto delle sessioni ,dimenticavo questo e il db
CREATE TABLE `staff` (
`id` int(11) NOT NULL auto_increment,
`nome` varchar(150) NOT NULL,
`cognome` varchar(150) NOT NULL,
`telefono` varchar(100) NOT NULL,
`nick` varchar(150) NOT NULL,
`email` varchar(150) NOT NULL,
`pass` varchar(100) NOT NULL,
`stato` int(11) NOT NULL default '1',
`caso` int(11) NOT NULL,
`tipo` int(11) NOT NULL,
`group` int(11) NOT NULL,
`data` varchar(30) NOT NULL,
`date_ultima_mod` varchar(30) default NULL,
`ip` varchar(30) NOT NULL,
`agent` varchar(150) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;