Foros del Web » Programando para Internet » PHP »

Tiempo de las sesiones

Estas en el tema de Tiempo de las sesiones en el foro de PHP en Foros del Web. El tiempo de mis sesiones caduca sobre los 5 min. Me gustaría que me caducara a los 15 y no sé porqué me caduca antes. ...
  #1 (permalink)  
Antiguo 14/07/2008, 10:14
 
Fecha de Ingreso: febrero-2008
Mensajes: 49
Antigüedad: 16 años, 2 meses
Puntos: 1
Tiempo de las sesiones

El tiempo de mis sesiones caduca sobre los 5 min.
Me gustaría que me caducara a los 15 y no sé porqué me caduca antes.
En el php.ini vienen 24 min. (por defecto) y yo no he hecho nada para que la sesión caduque antes. Por qué me pasa esto?
Gracias
  #2 (permalink)  
Antiguo 14/07/2008, 11:27
Avatar de xbx
xbx
 
Fecha de Ingreso: mayo-2008
Ubicación: /home/xbx
Mensajes: 301
Antigüedad: 16 años
Puntos: 11
Respuesta: Tiempo de las sesiones

Hola,
Puedes decirnos que valores poseen estas variables en tu phpinfo?

session.cookie_lifetime
session.gc_probability
session.gc_divisor
session.gc_maxlifetime
  #3 (permalink)  
Antiguo 14/07/2008, 11:51
 
Fecha de Ingreso: febrero-2008
Mensajes: 49
Antigüedad: 16 años, 2 meses
Puntos: 1
Respuesta: Tiempo de las sesiones

session.cookie_lifetime = 0
session.gc_probability = 1
session.gc_divisor = 100
session.gc_maxlifetime = 1440
  #4 (permalink)  
Antiguo 14/07/2008, 12:34
Avatar de xbx
xbx
 
Fecha de Ingreso: mayo-2008
Ubicación: /home/xbx
Mensajes: 301
Antigüedad: 16 años
Puntos: 11
Respuesta: Tiempo de las sesiones

Según esos valores, el tiempo de sesión no debería ser menor a 24 min siempre y cuando se este enviando la cookie de sesion por parte del navegador. (O, en su defecto, el valor del session id por get)


session.use_cookies = 1, cierto?

Tienes las cookies habilitadas en tu navegador?
  #5 (permalink)  
Antiguo 16/07/2008, 09:59
 
Fecha de Ingreso: febrero-2008
Mensajes: 49
Antigüedad: 16 años, 2 meses
Puntos: 1
Respuesta: Tiempo de las sesiones

si, session.use_cookies = 1
pero es necesario usar las cookies o get post?
  #6 (permalink)  
Antiguo 16/07/2008, 10:04
Avatar de xbx
xbx
 
Fecha de Ingreso: mayo-2008
Ubicación: /home/xbx
Mensajes: 301
Antigüedad: 16 años
Puntos: 11
Respuesta: Tiempo de las sesiones

el id de sesion viaja por cookies, generalmente (o en su defecto por get)
por eso es necesario que el navegador tenga las cookies habilitadas.

sigues con el problema?
  #7 (permalink)  
Antiguo 16/07/2008, 10:13
 
Fecha de Ingreso: febrero-2008
Mensajes: 49
Antigüedad: 16 años, 2 meses
Puntos: 1
Respuesta: Tiempo de las sesiones

Sigo con el problema!!
Las cookies las tengo habilitadas en mi navegador.
Los 24 min por defecto, son desde que se inicia la sesión o desde que se queda inactivo el explorador? No se si darle al php.ini un valor de 3 horas y esperar a ver que pasa.
  #8 (permalink)  
Antiguo 16/07/2008, 10:18
Avatar de xbx
xbx
 
Fecha de Ingreso: mayo-2008
Ubicación: /home/xbx
Mensajes: 301
Antigüedad: 16 años
Puntos: 11
Respuesta: Tiempo de las sesiones

Los 24 minutos cuentan desde el ultimo session_start. Pero tambien depende de otras 2 variables que hacen que los 24 minutos puedan ser aún más...

Pero en principio no puede durar menos de 24 minutos tu sesion.

Tu sesion dura 5 minutos o no dura nada?
  #9 (permalink)  
Antiguo 16/07/2008, 10:22
Avatar de xbx
xbx
 
Fecha de Ingreso: mayo-2008
Ubicación: /home/xbx
Mensajes: 301
Antigüedad: 16 años
Puntos: 11
Respuesta: Tiempo de las sesiones

Haz una cosa...

al final de tu script coloca esto:

echo "ID de sesion: " . session_id();


Desde el navegador actualiza sucesivas veces tu pagina y fíjate si tu ID de sesion es siempre el mismo.
Deberia serlo.
  #10 (permalink)  
Antiguo 16/07/2008, 10:25
 
Fecha de Ingreso: febrero-2008
Mensajes: 49
Antigüedad: 16 años, 2 meses
Puntos: 1
Respuesta: Tiempo de las sesiones

dura 5 min aprox.
Se puede pasar de una página sin session_start o otra que tiene sin que influya en el tiempo se sesión?
Y si navego en distintas pestañas por la misma página? Cómo puede influir en las sesiones.
Muchas gracias por todo. Me estais ayudando mucho.
  #11 (permalink)  
Antiguo 16/07/2008, 10:31
Avatar de xbx
xbx
 
Fecha de Ingreso: mayo-2008
Ubicación: /home/xbx
Mensajes: 301
Antigüedad: 16 años
Puntos: 11
Respuesta: Tiempo de las sesiones

No hay por qué!

cada vez que ejecutas un session_start vuelve "el contador" de tiempo a 0.
Si una pagina no hace session_start esto no influye en el tiempo de vida de la sesion.
El tiempo de vida de la sesion se renueva en cada session_start.
Es lo que tienes que tener en cuenta sin importar quenavegues en varias pestañas de una misma pagina.

Haz la prueba que te he dicho en el comentario anterior
  #12 (permalink)  
Antiguo 17/07/2008, 01:03
 
Fecha de Ingreso: febrero-2008
Mensajes: 49
Antigüedad: 16 años, 2 meses
Puntos: 1
Respuesta: Tiempo de las sesiones

Sí, el ID es el mismo. Actualizao la página varias veces y sigue siendo el mismo. Pero al cerrar la sesión y volver a abrirla, sigue siendo la misma que la de antes. No debería ser distinta al abrir una nueva sesión?
Tengo otro ordenador y me pasa lo mismo. Tiene que ser así?
Estoy desesperado, cada vez tienen una duración distinta. A veces sobre los 5 min, otras llega a los 15, pero nunca ha llegado a los 24 min.
Gracias
  #13 (permalink)  
Antiguo 17/07/2008, 06:14
Avatar de xbx
xbx
 
Fecha de Ingreso: mayo-2008
Ubicación: /home/xbx
Mensajes: 301
Antigüedad: 16 años
Puntos: 11
Pregunta Respuesta: Tiempo de las sesiones

Y cómo "cierras sesión" tú?
Para que la sesion cambie de ID deberias:

- o hacer un session_destroy (a esto llamas cerrar?)
- o que el navegador se cierre y en concecuencia se elimine la cookie de sesion.

Tengo la duda de si realmente el valor de tu session.gc_maxlifetime es 1440. Porque claramente en ese caso la sesion no deberia pasar los 24 min.

Que el tiempo de una sesion sea variable es comprensible por el manejo que php le da a su proceso "garbage collection", el cuál es el encargado de cada ciertos "inicio de sesion" revisar la totalidad de las sesiones y eliminar las que excedan el session.gc_maxlifetime.

Te paso un fragmento del php.ini el cual explica esto que te he dicho:
Cita:
; Define the probability that the 'garbage collection' process is started
; on every session initialization.
; The probability is calculated by using gc_probability/gc_divisor,
; e.g. 1/100 means there is a 1% chance that the GC process starts
; on each request.

session.gc_probability = 1
session.gc_divisor = 1000

; After this number of seconds, stored data will be seen as 'garbage' and
; cleaned up by the garbage collection process.
session.gc_maxlifetime = 1440
Con eso se explica que las sesiones tengan duraciones variables y que puedan durar mas del tiempo maximo gc_maxlifetime, pero no explica porque en tu caso las sesiones duran menos.

Hay algo que nos falta saber sobre tu código...
  #14 (permalink)  
Antiguo 21/07/2008, 06:15
 
Fecha de Ingreso: febrero-2008
Mensajes: 49
Antigüedad: 16 años, 2 meses
Puntos: 1
Respuesta: Tiempo de las sesiones

Creo que el problema es que no me destruye la session, porque al desconectarme y volver a logearme la session vuelve a ser la misma.
te pego el código para ver si ves algo que pudiera estar mal:
Tengo hechos 3 archivos para comprobar lo de las sessiones. Te las copio:

Page1:
<?
session_start();

$_SESSION['sess_var'] = "Hola Borja";

echo 'El contenido de la $_SESSION[\'sess_var\'] es '.$_SESSION['sess_var'].'<br />';

echo 'ID de Sesión'.session_id();
echo '<br>';
?>

<a href="page2.php">Siguiente página</a>

Page2:
<?
session_start();
echo 'The content of $_SESSION[\'sess_var\'] is '.$_SESSION['sess_var'].'<br />';
unset($_SESSION['sess_var']);

echo 'Elimino el contenido de la variable $_SESSION';
echo 'ID de Sesión'.session_id();
echo '<br>';
?>
<a href="page3.php">Next page</a>

Page3:
<?
session_start();
echo 'la ID de Sesión antes de destruirla es: '.session_id();
echo '<br>';

echo 'The content of $_SESSION[\'sess_var\'] is '.$_SESSION['sess_var'].'<br />';
session_destroy();


echo 'la ID de Sesión está destruida como puedo comprobar:<br>';
echo 'ID de Sesión ahora está vacío: ()'.session_id();
echo '<br>';
?>

<a href="page1.php">Next page</a>


A ver si consigues ver algo.
Muchas gracias
  #15 (permalink)  
Antiguo 21/07/2008, 06:31
Avatar de xbx
xbx
 
Fecha de Ingreso: mayo-2008
Ubicación: /home/xbx
Mensajes: 301
Antigüedad: 16 años
Puntos: 11
Respuesta: Tiempo de las sesiones

Estamos ante 2 cuestiones
El tiempo de vida de la sesion es menor al esperado
Imorap, esto me huele a que tu estas haciendo un session_destroy en algun lugar que no es conveniente.
Puedes revisar eso? o pasar un poco mas de codigo.

Luego He visto tu codigo:

Aparentemente la sesion no es destruida
La sesion si esta siendo destruida, lo que ocurre es que se conserva el mismo session_id gracias a que el navegador sigue enviando la cookie de sesion. Pero esto no es un problema porque al hacer el session_destroy() todo contedino de la sesion es eliminado: Lo que ocurre que al hacer el session start se reutiliza el session_id anterior gracias a que este es tomado de la cookie.

  #16 (permalink)  
Antiguo 21/07/2008, 10:27
 
Fecha de Ingreso: febrero-2008
Mensajes: 49
Antigüedad: 16 años, 2 meses
Puntos: 1
Respuesta: Tiempo de las sesiones

OK, gracias pero...
Cómo puedo hacer para que al destruir la sesión me destruya también la cookie? o que la cookie que por defecto me crea php, su valor no sea igual al id de sesión.
Cómo puedo solucionar esto?
Cambiando algo en el php.ini?
el session.cookie_lifetime = 0, cabiarlo?
Muchísimas gracias por todo. Me estais salvando la vida.
  #17 (permalink)  
Antiguo 21/07/2008, 12:02
Avatar de xbx
xbx
 
Fecha de Ingreso: mayo-2008
Ubicación: /home/xbx
Mensajes: 301
Antigüedad: 16 años
Puntos: 11
Respuesta: Tiempo de las sesiones

De nada !!

Fíjate: http://ar2.php.net/manual/es/functio...enerate-id.php

Prueba hacer lo siguiente por cada session_start:

session_start();

if(empty($_SESSION))
{
session_regenerate_id();
session_start();
}

Si la sesion esta vacia regeneras el id. (Para que necesitas hacerlo?)

Pero igualmente esto no tiene nada que ver con la corta duracion de tus sesiones...

  #18 (permalink)  
Antiguo 23/09/2008, 10:38
 
Fecha de Ingreso: febrero-2008
Mensajes: 49
Antigüedad: 16 años, 2 meses
Puntos: 1
Respuesta: Tiempo de las sesiones

Siento el lapsus de tiempo.
Copio el php.ini relacionado con sesiones. Está todo bien?

[Session]
; Handler used to store/retrieve data.
session.save_handler = files

; Argument passed to save_handler. In the case of files, this is the path
; where data files are stored. Note: Windows users have to change this
; variable in order to use PHP's session functions.
;
; As of PHP 4.0.1, you can define the path as:
;
; session.save_path = "${path}\tmp\"

;
; where N is an integer. Instead of storing all the session files in
; /path, what this will do is use subdirectories N-levels deep, and
; store the session data in those directories. This is useful if you
; or your OS have problems with lots of files in one directory, and is
; a more efficient layout for servers that handle lots of sessions.
;
; NOTE 1: PHP will not create this directory structure automatically.
; You can use the script in the ext/session dir for that purpose.
; NOTE 2: See the section on garbage collection below if you choose to
; use subdirectories for session storage
;
; The file storage module creates files using mode 600 by default.
; You can change that by using
;
; session.save_path = "N;MODE;/path"
;
; where MODE is the octal representation of the mode. Note that this
; does not overwrite the process's umask.
session.save_path = "/tmp"

; Whether to use cookies.
session.use_cookies = 1

; This option enables administrators to make their users invulnerable to
; attacks which involve passing session ids in URLs; defaults to 0.
; session.use_only_cookies = 1

; Name of the session (used as cookie name).
session.name = CybD

; Initialize session on request startup.
session.auto_start = 0

; Lifetime in seconds of cookie or, if 0, until browser is restarted.
session.cookie_lifetime = 1440

; The path for which the cookie is valid.
session.cookie_path = /

; The domain for which the cookie is valid.
session.cookie_domain =

; Handler used to serialize data. php is the standard serializer of PHP.
session.serialize_handler = php

; Define the probability that the 'garbage collection' process is started
; on every session initialization.
; The probability is calculated by using gc_probability/gc_divisor,
; e.g. 1/100 means there is a 1% chance that the GC process starts
; on each request.

session.gc_probability = 1
session.gc_divisor = 100

; After this number of seconds, stored data will be seen as 'garbage' and
; cleaned up by the garbage collection process.
session.gc_maxlifetime = 1440

; NOTE: If you are using the subdirectory option for storing session files
; (see session.save_path above), then garbage collection does *not*
; happen automatically. You will need to do your own garbage
; collection through a shell script, cron entry, or some other method.
; For example, the following script would is the equivalent of
; setting session.gc_maxlifetime to 1440 (1440 seconds = 24 minutes):
; cd /path/to/sessions; find -cmin +24 | xargs rm

; PHP 4.2 and less have an undocumented feature/bug that allows you to
; to initialize a session variable in the global scope, albeit register_globals
; is disabled. PHP 4.3 and later will warn you, if this feature is used.
; You can disable the feature and the warning separately. At this time,
; the warning is only displayed, if bug_compat_42 is enabled.

session.bug_compat_42 = 0
session.bug_compat_warn = 1

; Check HTTP Referer to invalidate externally stored URLs containing ids.
; HTTP_REFERER has to contain this substring for the session to be
; considered as valid.
session.referer_check =

; How many bytes to read from the file.
session.entropy_length = 0

; Specified here to create the session id.
session.entropy_file =

;session.entropy_length = 16

;session.entropy_file = /dev/urandom

; Set to {nocache,private,public,} to determine HTTP caching aspects
; or leave this empty to avoid sending anti-caching headers.
session.cache_limiter = nocache

; Document expires after n minutes.
session.cache_expire = 24

; trans sid support is disabled by default.
; Use of trans sid may risk your users security.
; Use this option with caution.
; - User may send URL contains active session ID
; to other person via. email/irc/etc.
; - URL that contains active session ID may be stored
; in publically accessible computer.
; - User may access your site with the same session ID
; always using URL stored in browser's history or bookmarks.
session.use_trans_sid = 0

; Select a hash function
; 0: MD5 (128 bits)
; 1: SHA-1 (160 bits)
session.hash_function = 0

; Define how many bits are stored in each character when converting
; the binary hash data to something readable.
;
; 4 bits: 0-9, a-f
; 5 bits: 0-9, a-v
; 6 bits: 0-9, a-z, A-Z, "-", ","
session.hash_bits_per_character = 4

; The URL rewriter will look for URLs in a defined set of HTML tags.
; form/fieldset are special; if you include them here, the rewriter will
; add a hidden <input> field with the info which is otherwise appended
; to URLs. If you want XHTML conformity, remove the form entry.
; Note that all valid entries require a "=", even if no value follows.
url_rewriter.tags = "a=href,area=href,frame=src,input=src,form=,fields et="
  #19 (permalink)  
Antiguo 23/09/2008, 15:54
Avatar de xbx
xbx
 
Fecha de Ingreso: mayo-2008
Ubicación: /home/xbx
Mensajes: 301
Antigüedad: 16 años
Puntos: 11
Respuesta: Tiempo de las sesiones

Hola

Segun lo que muestras, todas tus sesiones deben durar al menos 24 minutos, siempre y cuando no se cierre el navegador.

Nota: puede durar mucho mas en realidad, pero ya depende de otros factores.
  #20 (permalink)  
Antiguo 30/09/2008, 02:29
 
Fecha de Ingreso: febrero-2008
Mensajes: 49
Antigüedad: 16 años, 2 meses
Puntos: 1
Respuesta: Tiempo de las sesiones

Ahora tengo el tiempo de las sesiones en 2h. si si navego, me aguanta perfectamente, pero si no, se me cierra a los pocos minutos. Creo que me guarda la sesión pero no las variables asociadas a la sesión. Puede ser? Como puedo solucionarlo?
Gracias
  #21 (permalink)  
Antiguo 04/10/2008, 10:53
Avatar de xbx
xbx
 
Fecha de Ingreso: mayo-2008
Ubicación: /home/xbx
Mensajes: 301
Antigüedad: 16 años
Puntos: 11
Respuesta: Tiempo de las sesiones

este es mi ultimo intento.
Haz un phpinfo() y dinos que valores muestran estas variables:


session.gc_probability = 1
session.gc_divisor = 1000

; After this number of seconds, stored data will be seen as 'garbage' and
; cleaned up by the garbage collection process.
session.gc_maxlifetime = 1440

Tienes un problema que jamas imagine que pueda suceder...
saludos.
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 09:11.