Problems PHP session - session_start does not work?

1

If I execute this code, it always returns the same result. What am I doing wrong?

session_start();
if (!isset($_SESSION['count'])) {
    echo "no existe, inicializamos";
    $_SESSION['count'] = 0;
} else {
    echo "existe, sumamos";
    $_SESSION['count']++;
}


print_r($_SESSION);

The problem is that I always get count = 0

    
asked by albertodiez1984 14.11.2018 в 13:31
source

1 answer

0

The error is that you are not specifying which session you want to print.

Try this,

session_start();

if (!isset($_SESSION['count'])) {

echo "no existe, inicializamos";

$_SESSION['count'] = 0;

} else {
//Si la sesion count exite la recuperas transformando el contenido a int y le sumas 
//1, guardando el valor nuevamente en tu sesion "count"
echo "existe, sumamos";
$_SESSION['count']=(int)$_SESSION['count']+ 1;
}


print_r($_SESSION["count"]);
    
answered by 14.11.2018 в 13:43