condition to check if the session variables as well as get and post have been destroyed in php

1.8k views Asked by At

The code is like:(It is the last page of the web-app I have made)

<?php

if(isset($_GET['var'])
{
session_start();
$a=$_SESSION['prev_defined'];
#more use of session variable
session_destroy();
$_SESSION = array();
unset($_GET);
unset($_POST);
}
?>

Now when i execute the web application it runs fine , when i refresh the last page whose code is given above the warning message shows of undefined symbol because the $_SESSION variables as well as $_GET and $_POST have been deleted. I want to display message "SESSION OVER" on refresh. How to do it? Where to put if condition? I have tried to put the above code in

if($_SESSION)
{
#entire code above
}
else
{
echo"SESSION OVER";
}

but it displayes message undefined variable _SESSION

2

There are 2 answers

3
Vignesh Bala On
<?php

if(isset($_GET['var'])
{
  if(isset($_SESSION))
  {
    session_start();
    $a=$_SESSION['prev_defined'];
    #more use of session variable
    session_destroy();
    $_SESSION = array();
    unset($_GET);
    unset($_POST);
  }
  else
  {
    echo"SESSION OVER";
  }
}
?>

Try this one. If session is set it will do the conditions.

EDIT

if(isset($_GET['var'])
    {
....
}
else
  {
    echo"SESSION OVER";
  }

Using this, If $_GET['var'] is not set then echo the else part

EDIT 2

<?php
if (isset($_GET) || isset($_SESSION)) {
//Put all your codes here
} else {
    echo "Session Over";
}
2
animaacija On

$_SESSION is a global variable, an array. And it will be allways around.

check if session feature is active like so:

if (session_status() == PHP_SESSION_NONE) {
    session_destroy();
    echo "session over";
}

Also note to check if arrays key isset, before checking it's value, to avoid notices:

if(isset($_SESSION['login'])&&$_SESSION['login']==1){//pass}

EDIT:

as stated here: For versions of PHP < 5.4.0

if(session_id() == '') {
    session_start();
}