I have to add a link in my search screen to start a session by the user and and another link to stop the session in the result page. The result page will also will have a link to show all the wine names. I know only the basic session(). I am not getting what I have to do or code should i follow. Please suggest me something, if possible example codes.
Here is how you can end the session with a link, by passing a $_GET parameter
<a href="?logout"> Log out </a>
<?php
if(isset($_GET['logout'])) {
session_destroy();
}
?>
It is worthy to note, that you must have already started the session with session_start()
before destroying it.
<a href="?create"> Create Session </a>
<a href="?show"> Show Sessions </a>
<?php
//must have session start before destroying or starting sessions
session_start();
if(isset($_GET['create']))
{
//setting sessions with time, this can be equal to anything string
$_SESSION[] = time();
}
else if(isset($_GET['show']))
{
//this display all sessions currently stored
echo '<pre>' . print_r($_SESSION, TRUE) . '</pre>';
}
?>
You need to initialize the session if you want to destroy. So use this it should work
<?php
if(isset($_GET['start'])){
session_start();
$_SESSION['key']=true;
}elseif(isset($_GET['stop'])){
session_start(); // this is need to destroy also
session_destroy();
}
$ses_id = session_id();
if(empty($ses_id)){ ?>
<a href="?start">Start Session</a>
<?php }else{ ?>
<a href="?stop">Stop Session</a>
<?php }?>