2

I'm coding a "users-only" access for a site, and when the user is not logged in the dashboard is redirecting to the login page.

<?php
session_start();
$logged= $_SESSION['logged'];

if(!$logged){
    header("Location:http://www.someweb.com/system/login.php?logged_off=1");
}

?>

but the login page is not receiving the GET variable, can you please tell what am I doing wrong?

22
  • 2
    should always call exit(); after header Commented Oct 8, 2014 at 19:39
  • 1
    @pc-shooter, that is what exit does and it should be used with header/redirect. Commented Oct 8, 2014 at 19:42
  • 1
    @pc-shooter What websites do you maintain? I'd like to check them out... and turn on "ignore redirects" on my browser :) Commented Oct 8, 2014 at 19:42
  • 1
    mmm just added exit(); and it worked, thanks! Commented Oct 8, 2014 at 19:42
  • 2
    @Dagon I've just read the link of Fred-ii (stackoverflow.com/a/2747803)..... Oh man, you were so right! This is what i like here, always learning. Commented Oct 8, 2014 at 19:50

1 Answer 1

3

When using header location, you should call exit();

Why? Because the script's execution will not be terminated.

Parentheses () are optional, exit is a language construct not a function, and they actually are a bad idea (PHP has more work to do if they exist), just a terrible habit I have.

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

9 Comments

theres no new questions on S.O just new ways to ask(and answer)
Exactly. There's more than one way to ask a hunter/trapper on how to skin a waskiwee wabbit.
With you up until "or any header" - setting a header along with content is perfectly normal. e.g. header('Content-Type: application/json'); echo json_encode($something);
fell free to edit answer to make it better - no really please do
@Dagon +1 Maybe you should give me some lessons in PHP ;-)
|

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.