1

The following header function is not working. I ma trying to go to login if the user is not logged in -

    <?PHP
    if (logged_in() === false) {
    header('Location: login.php');
    }
    ?>

However if I do -

    <?PHP
    if (logged_in() === false) {
    echo"No user is logged in";
    }
    ?>

It does echo it and I can see that it says no user is logged in

It is basically just checking if there is a user logged in

    function logged_in() {
    return (isset($_SESSION['user_id'])) ? true : false;
    }
14
  • 1
    explain logged_in() Commented Jun 7, 2013 at 5:11
  • 1
    wild guess, logged_in() does some output, and header() then issue a warning and doesn't work Commented Jun 7, 2013 at 5:14
  • 2
    <?php ob_start(); // code ob_end_flush(); ?> Commented Jun 7, 2013 at 5:15
  • 2
    You need to include session_start(); as your first line of code after <?php in every affected/used file. Give that a try. Commented Jun 7, 2013 at 5:23
  • 2
    @Fasilkk You should get the credit for this. Post it as an answer and I'll upvote it. Commented Jun 7, 2013 at 5:43

3 Answers 3

2

Try to put exit() or die() after the header like

  if (logged_in() === false) {
      header('Location: login.php');
      exit();    //or die();
  }

But makesure that your login.php should be in the same folder

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the help. I figured it out I needed the ob_start and ob_end_flush
@AvinashKunaparaju Ahem, Fasil kk figured it out ;-)
1

Make sure that there is no output(white-space also) in your code.

you can use ob_start() and ob_end_flush() to clear out-put.

<?php ob_start();

 // code 

ob_end_flush(); ?>

3 Comments

Although this solved the problem, it'd be good to explain why, for future visitors viewing this question.
@Daedalus Like 90% of the questions with answers on SO, most post a link to PHP.net anyway, so it's irrelevant. Give the guy a break.
@Daedalus... I added small explanation too.. I think that is enough to understand for all.
0

You probably need to include the fully qualified domain and path to the new url. There is a note on the official documentation for the header function indicating as such.

Note:

HTTP/1.1 requires an absolute URI as argument to » Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. You can usually use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a relative one yourself:

This note also contains the following code sample.

<?php 
/* Redirect to a different page in the current directory that was requested */ 
$host  = $_SERVER['HTTP_HOST']; 
$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\'); 
$extra = 'login.php';
header("Location: http://$host$uri/$extra"); 
exit; 
?>

2 Comments

Every browser supports relative URLs, and W3C or IETF is planning on changing the spec to say that it's allowed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.