2

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

Azra Mahrukh
  • 151
  • 1
  • 14

2 Answers2

1
<?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";
}
Vignesh Bala
  • 889
  • 6
  • 25
0

$_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();
}
Community
  • 1
  • 1
animaacija
  • 170
  • 9
  • 25
  • I want dt on refresh, when the post and get are unset already and session variables have been deleted..the program should detect it and rather than telling that variable undefined should just output session over.... whn i used 'if (session_status() == PHP_SESSION_NONE) { session_destroy(); echo "session over"; }' its showing session over even before refreshing – Azra Mahrukh Jun 13 '15 at 09:56
  • because session was not started try printing session_status() with and without session_start() before it. – animaacija Jun 13 '15 at 10:11