Foros del Web » Programando para Internet » Javascript »

Cerrar Sesion

Estas en el tema de Cerrar Sesion en el foro de Javascript en Foros del Web. Hola a Todos... Tengo una gran duda para ver si alguien me puede colaborar o responder: Estoy haciendo un sistema en php, ajax,m javascript, html ...
  #1 (permalink)  
Antiguo 05/10/2009, 14:11
 
Fecha de Ingreso: julio-2009
Mensajes: 84
Antigüedad: 14 años, 9 meses
Puntos: 1
Pregunta Cerrar Sesion

Hola a Todos...

Tengo una gran duda para ver si alguien me puede colaborar o responder:

Estoy haciendo un sistema en php, ajax,m javascript, html , mysql y pues debe manejar sesiones, login seguridad, etc...

El login esta sin problema y de hecho al loguearse un usuario se almacena el registro en una tabla sesiones en la cual se lleva un log de cada vez que el usuario entra al sistema. Ahora bien tengo un link de cerrar sesion, y obviamente la idea es que al darle click ahi se cierre la sesion. El problema es cuando el usuario cierra el navegador directamente, ya que necesito que me haga todo el proceso como si diera click en la opcion cerrar sesison, es decir destruir las variables de sesion y ta,bien almacenar el registro de que cerro sesion en la tabla sesiones.

Alguin me podria dar una idea de como controlar esto??
  #2 (permalink)  
Antiguo 05/10/2009, 14:16
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 18 años
Puntos: 2135
Respuesta: Cerrar Sesion

Lamentablemente con PHP solamente no puedes hacerlo, necesitas usar Javascript, y lo que haces es abrir una ventana popup hacia tu archivo de cerrar sesión para hacer eso.

Saludos.
  #3 (permalink)  
Antiguo 05/10/2009, 14:19
 
Fecha de Ingreso: julio-2009
Mensajes: 84
Antigüedad: 14 años, 9 meses
Puntos: 1
Respuesta: Cerrar Sesion

Gracias GatorV por la rapida respuesta, pero una pregunta como manejo esto con javascript, en que evento colocaria la funcion para abrir el popup??
  #4 (permalink)  
Antiguo 05/10/2009, 14:28
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 18 años
Puntos: 2135
Respuesta: Cerrar Sesion

Te muevo al foro adecuado.

Usas el evento onunload de tu pagina.

Saludos.
  #5 (permalink)  
Antiguo 05/10/2009, 14:29
 
Fecha de Ingreso: julio-2009
Mensajes: 84
Antigüedad: 14 años, 9 meses
Puntos: 1
Respuesta: Cerrar Sesion

ok, voy a probarlo
  #6 (permalink)  
Antiguo 05/10/2009, 14:52
 
Fecha de Ingreso: julio-2009
Mensajes: 84
Antigüedad: 14 años, 9 meses
Puntos: 1
Pregunta Respuesta: Cerrar Sesion

Estuve probando con el evento unonload, pero resulta que cada vez que me paso de seccion en la aplicacion me carga este evento e incluso cuando le doy F5, como controle para que se me active el popup de cerrar sesion unicamente cuendo cierro la ventana del navegador??
  #7 (permalink)  
Antiguo 05/10/2009, 22:03
Avatar de Panino5001
Me alejo de Omelas
 
Fecha de Ingreso: mayo-2004
Ubicación: -34.637167,-58.462984
Mensajes: 5.148
Antigüedad: 20 años
Puntos: 834
Respuesta: Cerrar Sesion

Usando algo como esto podrás destruir la sesión de manera segura:
Código PHP:
<?php
session_start
();
set_time_limit(0); 
session_write_close();
if(isset(
$_POST['control'])){
    
ignore_user_abort(1); 
    while (
1) {
         echo 
"\n";
         
ob_flush();
         
flush();
         
sleep(5);
         if (
connection_status()!=0){
                 
session_start();
                 
session_destroy();
                exit; 
         }
    }
    exit;
}
?>
<!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=iso-8859-1" />
<title>test</title>
<script>
function http(){
    if(typeof window.XMLHttpRequest!='undefined'){
        return new XMLHttpRequest();
    }else{
        try{
            return new ActiveXObject('Microsoft.XMLHTTP');
        }catch(e){
            alert('Su navegador no soporta AJAX');
            return false;
        }
    }
}
function request(url,callback,params){
    var H=new http();
    if(!H)return;
    H.open('post',url+'?'+Math.random(),true);
    H.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    H.onreadystatechange=function(){
        if(H.readyState==4){
            if(callback)
                callback(H.responseText);
            H.onreadystatechange=function(){}
            H.abort();
            H=null;
        }
    }
    var p='';
    for(var i in params){
        p+='&'+i+'='+escape(params[i]);
    }
    H.send(p);

onload=function(){
    request('?',function(){},{'control':'1'});
}
</script>
</head>

<body>
<?php echo $_SESSION['algo'];?>
</body>
</html>
  #8 (permalink)  
Antiguo 09/10/2009, 09:14
 
Fecha de Ingreso: julio-2009
Mensajes: 84
Antigüedad: 14 años, 9 meses
Puntos: 1
Respuesta: Cerrar Sesion

Muchas Gracias.
  #9 (permalink)  
Antiguo 19/12/2009, 10:25
Avatar de zeuzft  
Fecha de Ingreso: junio-2009
Ubicación: peru
Mensajes: 358
Antigüedad: 14 años, 10 meses
Puntos: 2
Exclamación Respuesta: Cerrar Sesion

gracias por la ayuda; estoy intentado implementar el codigo en mi formulario; index.php; pero me arroja el siguiente error:
Cita:
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/citecper/public_html/sistema/index.php:36) in /home/citecper/public_html/sistema/ingreso.php on line 2

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/citecper/public_html/sistema/index.php:36) in /home/citecper/public_html/sistema/ingreso.php on line 2
mi cabecera esta de la siguiente manera index.php:
Código PHP:
<?
session_start
();
/*header ("Expires: Thu, 27 Mar 1980 23:59:00 GMT"); //la pagina expira en una fecha pasada
header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); //ultima actualizacion ahora cuando la cargamos
header ("Cache-Control: no-cache, must-revalidate"); //no guardar en CACHE
header ("Pragma: no-cache");*/
require_once("config/config.php");
require_once(
"include/clasesmysql.php");
$miconexion = new DB_mysql ;
$miconexion->conectar($basededatos$host$usuario_bd,$contrasena_bd);
require_once(
"include/class.tree.php");
require_once(
"include/funciones.php");
set_time_limit(0); 
session_write_close();
if(isset(
$_POST['control'])){
    
ignore_user_abort(1); 
    while (
1) {
         echo 
"\n";
         
ob_flush();
         
flush();
         
sleep(5);
         if (
connection_status()!=0){
                 
session_start();
                 
session_destroy();
                exit; 
         }
    }
    exit;
}
?>
Cita:
<!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=iso-8859-1" />
<title>..::CITEC::..</title>
<script type="text/javascript" src="js/lib/prototype.js"></script>
<script type="text/javascript" src="js/lib/scriptaculous.js?load=effects"></script>
<script type="text/javascript" src="js/modalbox.js"></script>
<link rel="stylesheet" href="css/modalbox.css" type="text/css" media="screen" />
<link href="css/citec.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/embeddedcontent.js" defer="defer"></script>
<script type="text/javascript" src="js/ajax.js"></script>
<script type="text/javascript" src="js/tree.js"></script>
<script type="text/javascript" src="js/myajax.js"></script>
<script type="text/javascript" src ="js/titulo.js"> </script>
<script type="text/javascript" src="flash/stmenu.js"></script>
<script type="text/javascript">
<!--
window.onerror=function(m,u,l)
{
window.status = "Java Script Error: "+m;
return true;
}
//-->
</script>
<script>
function http(){
if(typeof window.XMLHttpRequest!='undefined'){
return new XMLHttpRequest();
}else{
try{
return new ActiveXObject('Microsoft.XMLHTTP');
}catch(e){
alert('Su navegador no soporta AJAX');
return false;
}
}
}
function request(url,callback,params){
var H=new http();
if(!H)return;
H.open('post',url+'?'+Math.random(),true);
H.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
H.onreadystatechange=function(){
if(H.readyState==4){
if(callback)
callback(H.responseText);
H.onreadystatechange=function(){}
H.abort();
H=null;
}
}
var p='';
for(var i in params){
p+='&'+i+'='+escape(params[i]);
}
H.send(p);
}
onload=function(){
request('?',function(){},{'control':'1'});
}
</script>
</head>
<body style='topmargin:25'>
<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center" bgcolor='#ffffff'>
<tr>
<td>
<? require_once("cabecera.php");?>
</td>
</tr>
<tr>
<td class='bordenaranja' valign='top'>
<?
require_once("ingreso.php");
?>
</td>
</tr>
<tr>
<td valign='top' bgcolor='#006699'>
<? require_once("pies.php");?>
</td>
</tr>
</table>
</body>
</html>
  #10 (permalink)  
Antiguo 19/12/2009, 10:36
Avatar de jackson666  
Fecha de Ingreso: noviembre-2009
Ubicación: Buenos Aires, Argentina
Mensajes: 1.971
Antigüedad: 14 años, 5 meses
Puntos: 65
Respuesta: Cerrar Sesion

1) usa <?php y no <?
2)Fijate que no haya NADA entre <?php y session_start()
3)fijate de que el archivo este guardado con codificacion ANSI
  #11 (permalink)  
Antiguo 19/12/2009, 10:45
Avatar de zeuzft  
Fecha de Ingreso: junio-2009
Ubicación: peru
Mensajes: 358
Antigüedad: 14 años, 10 meses
Puntos: 2
Respuesta: Cerrar Sesion

hice lo que me dijiste pero igual me sale error en la linea 37 que llama a una libreria regular de js; que es prototype.js
Cita:
<?php
session_start();
set_time_limit(0);
session_write_close();
if(isset($_POST['control'])){
ignore_user_abort(1);
while (1) {
echo "\n";
ob_flush();
flush();
sleep(5);
if (connection_status()!=0){
session_start();
session_destroy();
exit;
}
}
exit;
}
header ("Expires: Thu, 27 Mar 1980 23:59:00 GMT"); //la pagina expira en una fecha pasada
header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); //ultima actualizacion ahora cuando la cargamos
header ("Cache-Control: no-cache, must-revalidate"); //no guardar en CACHE
header ("Pragma: no-cache");
require_once("config/config.php");
require_once("include/clasesmysql.php");
$miconexion = new DB_mysql ;
$miconexion->conectar($basededatos, $host, $usuario_bd,$contrasena_bd);
require_once("include/class.tree.php");
require_once("include/funciones.php");

?>
linea 37
Cita:
<script type="text/javascript" src="js/lib/prototype.js"></script>
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 14:36.