image


Valora:  2/5
Inicio » PHP » PHP Tutorial » Sessions




Practical example

By: Jose Valle

Let's see another example that may further clarify this point:

<?php // Manual de PHP de WebEstilo.com

session_register('contador');
echo '<a href="'.$PHP_SELF.'?'.$SID.'">Contador vale: '.++$_SESSION['contador']. '</a>';
?>



Execute View code

As I mentioned before, the session is created or retaken by session_start(), or also when a session variable is recorded by session_register().

If you have never used sessions, the concept session variable may seem a little abstract. Basically, it is a variable, like any other PHP4 managed variables, but it resides in a specific location on the server, along with the session identifier and belongs only to one user.

In our previous example, we recorded the variable $counter in the first row of the script. Among other things, every time we reload the page or click the link on the second row, the value of $counter will increase by 1.

In this line we use the reserved variable $PHP_SELF, that refers to the executing script, and to a PHP4 constant, $SID, that contains the session name and identifier.

We can also get the name of the session or modify it with the statement session_name(). Let's see a practical test:

<?php // Manual de PHP de WebEstilo.com
session_name('misesion');
session_register('contador');
echo '<a href="'.$PHP_SELF.'?'.SID.'">Contador vale: '.++$_SESSION['contador'].'</a><br>';
echo 'Ahora el nombre es '.session_name().' y la sesión '.$misesion.'<br>';
?>


Execute View code

The assignment of the session name shall be done before any other statement with sessions, before session_start() or session_register().





WebEstilo.com - Introduzca su e-mail y conozca las novedades. No hacemos Spam.
Google
  Web WebEstilo.com   
 
Valid HTML 4.01!
Última modificación:25 de Diciembre de 2004. Spain - España.
© 1998-2004 por Joaquin Gracia. Todos los derechos reservados.