0

I would like a person who is not logged in be redirected to the index page.

on my onlymembers.php I put this code:

if(!$user->is_logged_in()){ 
echo "you are not logged in";
header("Location: index.php");
} 

It is printing out "you are not logged in" which indicates that the login check is working. But I will not be redirected to the index page. Do you know why?


Update:

I used this code now and it is working:

if(!$user->is_logged_in()){ 
header("Location: index.php"); 
} 

I have actually NO idea why. I just removed some whitespaces :-/


Update again: I know now the problem: I just removed the whitespace infront my <?php at the beginning of my page....

peace_love
  • 6,229
  • 11
  • 69
  • 157

5 Answers5

4

You can't send the content of a page before using an HTTP redirection (you should have a PHP error like "headers already sent" with your code). You either have to redirect using javascript or remove the "echo" :

if(!$user->is_logged_in()){ 
    header("Location: index.php");
}
Derek
  • 1,826
  • 18
  • 25
  • Thank you, I wrote the code like you suggested, but still it is not redirecting. How can I make the "headers already sent" error message? – peace_love Sep 04 '15 at 10:00
  • Jarla, you need to make sure your check happens before any echos. If you do as you have shown, and you look at the logs (or increase your log level), you'll see the "headers already sent" message – Ruan Mendes Sep 04 '15 at 10:37
1

You cannot send an HTTP header after you've sent a visual output to the user. You could instead use an header with a refresh condition that will allow you to send an output to the user.

if(!$user->is_logged_in()){ 
header("Refresh: 5; url=./index.php");
//after 5 seconds the user gets redirected. To change the period of time just change the number after "Refresh"
echo "you are not logged in";
exit();
} 

P.S. Is always suggested that you use an exit() function (like I did) when you want force redirect an user for security prouposes.

You could also put a variable on the redirect link and then parse it on index.php to display a message, like this...

Redirect

if(!$user->is_logged_in()){ 
$msg = "you are not logged in";
    header("Location: index.php?reply=$msg");
exit();
    } 

Additional index.php code

if(!empty($_GET['reply'])) {
$reply = $_GET['reply'];
}

Then you have the $reply variable that contain the message you can display in your index.

J0ker98
  • 447
  • 5
  • 18
  • I tried the first code you suggested, but I get an empty page with the message: you are not logged in. No redirection – peace_love Sep 04 '15 at 10:26
  • Try to change the path of the url from "index.php" to "./index.php". I've also edited the answer. – J0ker98 Sep 04 '15 at 10:35
0

there with location redirect not working then you may use javascript for this.

<script>
 window.location = "http://www.redirecturl.com/"
</script>
  • but is it more secure then header(location:...) ? – peace_love Sep 04 '15 at 10:16
  • `header('Location:...');` is not client side, although I cannot tell you whether it's "secure" as I don't know, but I'm fairly positive that it is more secure. – Epodax Sep 04 '15 at 10:18
  • you mean javascript is more secure? – peace_love Sep 04 '15 at 10:19
  • No, the `header('Location'); ` is more secure, although I'm not certain. – Epodax Sep 04 '15 at 10:20
  • @epodax, how is that more secure? You still ends up with the same result,. The reason you should avoid JavaScript is in case the user has it turned off – Ruan Mendes Sep 04 '15 at 10:41
  • @JuanMendes Because the JavaScript can be manipulated by the user without the server having a say about it, a `header('Location: index.php);` is static, and they can't change the URL, although I'm not sure if one can alter the headers after they are sent from the server. But again, I'm far from certain. – Epodax Sep 04 '15 at 10:43
  • @epodax but the JavaScript could be run by the client anyway. The problem is when you generate JavaScript and you have unsanitized input, it could run with the user's credentials. I don't see any risk here. – Ruan Mendes Sep 04 '15 at 11:58
0
  1. Move the if statement above the header file include.
  2. ob_start() at the top of the script to buffer the output.

    if(!$user->is_logged_in()){    
        echo "you are not logged in";    
        ob_start();    
        header("Location: index.php");    
    }
    

OR

<script>                
    var url = "http://yoururl.com";    
    window.location.href = url;
</script>
Jakir Hossain
  • 2,457
  • 18
  • 23
-1

Header function should be used before html tag.

Example:

<html>
<?php
/* This will give an error. Note the output
 * above, which is before the header() call */
header('Location: http://www.example.com/');
exit;
?>
Golden_flash
  • 492
  • 1
  • 6
  • 14