2

index.php

<ul class="top">
       <?php if(!isset($_SESSION['login_user'])) { ?>
        <li class="hover"><a href="#" onClick="revealModal('modalPage')">Login</a>
        </li>
         <?php } else {?>
        <li class="hover"><a href="logout.php">Logout</a>
           Welcome <?php echo $_SESSION['login_user']; ?>
        </li>
         <?php } ?>
        <li><a href="registration.php" class="about">Registration</a>
        </li>
    </ul>

logout.php

<?php    
session_start(); 
session_destroy(); 
header('Location:index.php');  
exit;  
?>  

I have my log out button in my every page, so i want that , if an user clicks in log out, he/she should redirect to the same page.. as i ve given the location page index here, but i want to remain in the same page.. any code to redirect to same page ?? Thank you in advance ..

Krish R
  • 22,583
  • 7
  • 50
  • 59
IT gIrLy GiRl
  • 337
  • 4
  • 7
  • 20

6 Answers6

10

Approach 1: In your logout.php file, you need to check if you have a REFERER url from previous page and redirect, if not, redirect to index.php

<?php    
session_start(); 
session_destroy();
if(isset($_SERVER['HTTP_REFERER'])) {
 header('Location: '.$_SERVER['HTTP_REFERER']);  
} else {
 header('Location: index.php');  
}
exit;  
?>

Approach 2: You can pass a reference to your login page to redirect.

Logout link

<?php
function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}
?>

<a href="logout.php?redirect=<?php echo base64_encode(curPageURL()); ?>">Logout</a>

logout.php file:

<?php
session_start(); 
session_destroy();
if(isset($_GET['redirect'])) {
 header('Location: '.base64_decode($_GET['redirect']));  
} else {
 header('Location: index.php');  
}
?>
Ignacio Ocampo
  • 2,693
  • 1
  • 20
  • 31
  • hey cant i put a refresh word in that location header without giving '.$_SERVER['HTTP_REFERER']); ?????????????? – IT gIrLy GiRl Dec 17 '13 at 07:08
  • but its redirecting only to the index page.. i want to redirect it to the same page.. listen when i used this header('Location: '.$_SERVER['HTTP_REFERER']); , some problem arises like i ve used sessio to call the data from different pages, so after log out the page showing lots of error.. did u get my point ?? @ignacio – IT gIrLy GiRl Dec 17 '13 at 07:20
  • Yep! have you seen my approach 2? – Ignacio Ocampo Dec 17 '13 at 07:22
  • yeah .. dat 1 is only redirecting to the index page not to the same page – IT gIrLy GiRl Dec 17 '13 at 07:22
2

How about,

header('Location:'.$_SERVER['HTTP_REFERER']);

In your login.php file:

$BackToMyPage = $_SERVER['HTTP_REFERER'];
if(isset($BackToMyPage)) {
    header('Location: '.$BackToMyPage);
} else {
    header('Location: index.php'); // default page
}
Krish R
  • 22,583
  • 7
  • 50
  • 59
0

You do not need $_SERVER['HTTP_REFERER']if you want to redirect to the same page or refresh it.
It could simply be done using
header('Location : .');

Let me see
  • 5,063
  • 9
  • 34
  • 47
0

Try adding a parameter in your logout.php so you could dynamically redirect the user to the page he pressed the logout button.

<li class="hover"><a href="logout.php?page=homepage.php">Logout</a>
           Welcome <?php echo $_SESSION['login_user']; ?>
        </li>

then in your logout.php

<?php    
session_start(); 
session_destroy(); 
$page = $_GET['page'];
header('Location:' . $page);  
exit;  
?>
Raven
  • 2,187
  • 7
  • 35
  • 61
0
$before = $_SERVER['HTTP_REFERER'];
if(isset($before)) {
     header('Location: '.$before);  
} else {
     header('Location: index.php');  
}
exit;  
LightningBoltϟ
  • 1,383
  • 2
  • 10
  • 28
  • Please don't simply post the code. Give some explanation or information or usage about your code. For example, see [this answer](http://stackoverflow.com/a/16893057/756941). – Nazik Dec 17 '13 at 06:44
-1
if(isset($_POST['id']))
{
    header('Location:index.php');  
} 
Nikola K.
  • 7,093
  • 13
  • 31
  • 39
vinod
  • 1