Hi, In my site I am having a demo login.I processed all the code in one page.If there is empty session then I use demo login id to login.But I want this to make work only if I click the demo login button. How can I do this.
As you will use session variables to store the session details, you can also use a variable to enable or disable the "demo login". Set the default value of the variable to "no". If you press the demo login button, change the value of that variable to "yes". While logging out of the "demo login" session by pressing logout, change the value of that variable again to "no".
If the link is in the index page and if i click the link it should go to demo login.If I directly type the url in the address bar it should not go. How to do this?
I don't know whether there is any other way than what I have mentioned in the previous posts. But let me know if you find another way I was using only session variables for such purposes earlier. It was not much difficult for me to do. Just lesser than 5 lines of code.
I am posting here a small setup. Create 4 files as follows and load "index.php". index.php: PHP: <?phpsession_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: <?phpsession_start();if(isset($_SESSION['flag']) && $_SESSION['flag'] != "yes"){ $_SESSION['flag'] = "yes"; echo '<meta http-equiv="Refresh" content="0;url=sample.php" />';}elseecho '<meta http-equiv="Refresh" content="0;url=index.php" />';?> logout_demo.php: PHP: <?phpsession_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" />';}elseecho '<meta http-equiv="Refresh" content="0;url=index.php" />';?> sample.php: PHP: <?phpsession_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.
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?
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.
$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.
Istead of just checking for Empty session, you should check for POST variables to make sure that the user clicked the button.
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.