yo me leido ese foro pero sige sin funcionarme os pongo el codigo entero de mi pagina...
y le he creado una tabla que se llama sesiones con el siguiente codigo...
CREATE TABLE `sesiones` (
`sessionID` varchar(255) NOT NULL default '',
`sessionStart` varchar(255) NOT NULL default '',
`sessionData` varchar(255) NOT NULL default ''
) ENGINE=MyISAM DEFAULT CHARSET=ascii;
este es del archivo ( db.php)
Código PHP:
//este archivo conecta la base de datos
$db_host="localhost"; //the host name of the sql server (if you do not know, leave as localhost. usually works)
$db_name="trekstor"; //the name of the database
$db_user="root"; //the username that is associated with the database
$db_pass=""; //the password for the username
//DO NOT MODIFY ANYTHING ELSE APART FROM THE ABOVE UNLESS YOU ARE SURE YOU KNOW WHAT YOU ARE DOING!
$dbc=mysql_connect($db_host,$db_user,$db_pass) OR DIE (mysql_error());
$dbs=mysql_select_db($db_name) OR DIE (mysql_error());
este es el del archivo (login.php)
Código PHP:
//este archivo me abre la session
include("codes/db.php");
include("codes/manejoSesiones.class.php");
$sesionOA = new manejoSesiones();
session_set_save_handler(array(&$sesionOA,'abrirSesion'),array(&$sesionOA,'cerrarSesion'),array(&$sesionOA,'leerSesion'),array(&$sesionOA,'escribirSesion'),array(&$sesionOA,'borrarSesion'),array(&$sesionOA,'recolector'));
session_start(); //start the session
$username=$_POST['usuario']; //Get the username the user has entered
$password=$_POST['pass']; //Get the password the user has entered
$password=md5($password); //turn the password they entered into md5 to compare with the DB
$loginname=$_SESSION['username'];
//check to see if logged in allready
if (isset($_SESSION['loggedin'])){
die("You are logged in<br><a href='logout.php'>Click here to logout</a>");
//if not logged in, then run other script instead
}else{
//find if the page was enterd by the login button
if (isset($_POST['submit'])){
//if username was entered, continue
if($username && $password){
$result=mysql_query($sql);
//If the user gets to here, then they have typed both a username and password, so we may now go onto finding out if they excist in the DB
$sql="SELECT * FROM administradores WHERE (administrador='$username') AND password='$password'"; //get rows where the username feild matches the username or email feild in the database with same password
$result=mysql_query($sql);
//check to see if the account is activated
$moorow=mysql_fetch_array($result);
//if there was a row returned, then obiously there is an account with the correct username/password. They may login!
if (mysql_num_rows($result) > 0){
$_SESSION['loggedin']="TRUE"; //set the global session varible for loggedin to true
$row=mysql_fetch_array($result);
$_SESSION['username']=$username;
//setcookie("usuario", $username, time() + 31536000);
// setcookie("pass", $password, time() + 31536000);
$sql="SELECT sexo FROM administradores WHERE administrador='$username'";
$result=mysql_query($sql);
$sex=mysql_result($result,0,0);
$_SESSION['sexo']=$sex;
mysql_free_result($result);
header("Location: admin.php");
//die("Welcome $username You are now logged in");
}else{
header("Location: index.php?msg=err");
}
}else{
header("Location: index.php?msg=not");
}
}
}
?>
este es el archivo (ifsesion.php)
Código PHP:
//este archivo se encarga de redireccionar a los no logueados para la pagina de logueo...
include("db.php");
include("codes/manejoSesiones.class.php");
$sesionOA = new manejoSesiones();
session_set_save_handler(array(&$sesionOA,'abrirSesion'),array(&$sesionOA,'cerrarSesion'),array(&$sesionOA,'leerSesion'),array(&$sesionOA,'escribirSesion'),array(&$sesionOA,'borrarSesion'),array(&$sesionOA,'recolector'));
//$pass=$_COOKIE["pass"];
session_start();
if (!isset($_SESSION['loggedin'])){
$logerr=$HTTP_GET_VARS["msg"];
switch($logerr){
case "err";
$logerr="Usuario y/o Contraseña no es válido";
break;
case "not";
$logerr="Introduzca Usuario y Contraseña";
break;
default;
$logerr="";
break;
}
include("logincode.php");
exit;
}else{
$sex=$_SESSION['sexo']; //define si es chico o chica
$user=$_SESSION['username'];
$user=ucwords($user);
}
y este es el archivo (manejoSesiones.class.php)
Código PHP:
<?php
class manejoSesiones {
var $tiempoExpirado;
var $tablaSesiones = "sesiones";
function abrirSesion($savePath, $sessionID) {
global $dbc;
return true;
}
function cerrarSesion() {
global $dbc;
$this->tiempoExpirado = ini_get('session.gc_maxlifetime');
$this->recolector($this->tiempoExpirado);
return true;
}
function leerSesion($sessionID) {
global $dbc;
$sql = "SELECT * FROM ".$this->tablaSesiones." WHERE sessionID = '$sessionID'";
$result = mysql_query($sql);
if(!$result) { return false; }
$num = mysql_num_rows($result);
if($num > 0) {
$data = mysql_fetch_assoc($result);
return $data['sessionData'];
}else{
return false;
}
}
function escribirSesion($sessionID, $sessionData) {
global $dbc;
$sql = "UPDATE ".$this->tablaSesiones." SET sessionData = '$sessionData' WHERE sessionID = '$sessionID'";
$result = mysql_query($sql);
if(mysql_affected_rows()) {
return true;
}else{
$sql = "INSERT INTO ".$this->tablaSesiones." (sessionID, sessionStart, sessionData) VALUES ('$sessionID','".time()."','$sessionData')";
$result = mysql_query($sql);
return (!$result) ? false : true ;
}
}
function borrarSesion($sessionID) {
global $dbc;
$sql = "DELETE FROM ".$this->tablaSesiones." WHERE sessionID = '$sessionID'";
$res = mysql_query($sql);
return (!$res) ? false : true ;
}
function recolector($tiempo) {
global $dbc;
$sql = "DELETE FROM ".$this->tablaSesiones." WHERE ".time()." > (sessionStart + ".$tiempo.")";
$res = mysql_query($sql);
return (!$res) ? false : true ;
}
}
?>