0

Seems silly but really confused me. Every refresh takes me inside this condition.

if(!isset($_SESSION)) {
session_start();
echo "Session turned on";
}

I tried changing the condition to if(empty($_SESSION)) and if (session_status() == PHP_SESSION_NONE) but still every time it the statements inside are getting executed.

I am Trying to increment the counter

<?php 
if(!isset($_SESSION)) {
session_start();
echo "Session turned on";
}

$current_time=date('m-d-Y H:i:s');
echo $current_time;
if(!isset($_SESSION["counter"]["time"]))
{
    $_SESSION["counter"]["time"]=date('m-d-Y H:i:s');
    increamentCounter();
}
$diff = abs(strtotime($current_time) - strtotime($_SESSION["counter"]["time"]));
$time_in_minutes=$diff/60;
if($time_in_minutes>30)
{
    unset($_SESSION["counter"]["time"]);
}
echo $time_in_minutes."<br>";
function increamentCounter()
{
    $pre_counter_value = file_get_contents("counter.txt");
    $counter_value = (int)$pre_counter_value+1;
    file_put_contents("counter.txt", $counter_value);
}
echo file_get_contents("counter.txt");
?>

1 Answers1

0

You're checking for a session variable before calling session_start(), as a result your if condition will always return true. Try:

session_start();
if(!isset($_SESSION)) {
    echo "Session detected";
} else {
    echo "No session";
}
Lucas Krupinski
  • 682
  • 5
  • 16