0
rekha's Avatar
Contributor
ok.waiting for your code snippet.
0
venami's Avatar, Join Date: Dec 2008
Ambitious contributor
I am posting here a small setup. Create 4 files as follows and load "index.php".

index.php:
PHP Code:
<?php
session_start
();
session_register('flag');
$_SESSION['flag'] = 'no';
?>
<html>
<head>
</head>
<body>
Sample PHP page
<a href="login_demo.php">Demo</a>
</body>
</html>
login_demo.php:
PHP Code:
<?php
session_start
();

if(isset(
$_SESSION['flag']) && $_SESSION['flag'] != "yes")
{
    
$_SESSION['flag'] = "yes";
    echo 
'<meta http-equiv="Refresh" content="0;url=sample.php" />';
}
else
echo 
'<meta http-equiv="Refresh" content="0;url=index.php" />';
?>
logout_demo.php:
PHP Code:
<?php
session_start
();

if(isset(
$_SESSION['flag']) && $_SESSION['flag'] == "yes")
{
    
$_SESSION['flag'] = "no";
    unset(
$_SESSION['flag']);
    
session_destroy();
    echo 
'<meta http-equiv="Refresh" content="0;url=index.php" />';
}
else
echo 
'<meta http-equiv="Refresh" content="0;url=index.php" />';
?>
sample.php:
PHP Code:
<?php
session_start
();

if(isset(
$_SESSION['flag']) && $_SESSION['flag'] != "yes")
{
    echo 
'<meta http-equiv="refresh" content="0;url=http://localhost/index.php" />';
}
else
{
?>

<html>
<head>
<title>Sample PHP Page</title>
Session Variable Demo...
</head>
<body>
<a href="logout_demo.php">Demo Logout</a>
</body>
</html>

<?php
}
?>
Let me know whether I got your question correctly.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Nice Sample venami.

OP I would suggest you also see Login and Logout using Sessions and Cookies.
venami like this
0
venami's Avatar, Join Date: Dec 2008
Ambitious contributor
Thanks shabbir, the article is really good and the code very clear as well.

But is there any difference in using HTML's "META" tag and PHP's header() fundtion?
0
rekha's Avatar
Contributor
Hi,

I solved it by using http referer.

Thanks.
0
venami's Avatar, Join Date: Dec 2008
Ambitious contributor
Can you tell me what you are mentioning as http referer?

If you had used a different way to solve your problem, reveal it here, so that every others will also know it.
0
rekha's Avatar
Contributor
$SERVER['HTTP_REFERER'] determine where the request is coming from.In the index page I have the demo link.So I checked using http_referer that if the page comes from index.php,then automatically demo login will be done.That's it.
venami like this
0
venami's Avatar, Join Date: Dec 2008
Ambitious contributor
Good. Thanks for the update
0
urstop's Avatar, Join Date: Oct 2007
Contributor
Istead of just checking for Empty session, you should check for POST variables to make sure that the user clicked the button.
0
venami's Avatar, Join Date: Dec 2008
Ambitious contributor
Rather than using buttons, usage of links would look good for this question. In that case, we have to use GET variables instead of POST variables.