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?

AugustoM
  • 405
  • 1
  • 7
  • 15
  • Other than the missing space after `Location:`, nothing really wrong with this code. You'll have to debug it yourself. e.g. do `var_dump($_GET)` in your login script. use a browser debugger to see if there's any redirects going on which could be stripping the parameters, etc... – Marc B Oct 08 '14 at 19:38
  • Do you see this argument in the redirected address bar of the browser? – Cheery Oct 08 '14 at 19:38
  • Are you sure that what's happening is this? What if instead you actually get a 404 which in returns redirects you to the hp? – Sebas Oct 08 '14 at 19:38
  • 2
    should always call `exit();` after header –  Oct 08 '14 at 19:39
  • @Dagon Nope! Because with `exit()` you'll exit the WHOLE script... – toesslab Oct 08 '14 at 19:41
  • 1
    @pc-shooter, that is what `exit` does and it should be used with `header/redirect`. – The Alpha Oct 08 '14 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 :) – sjagr Oct 08 '14 at 19:42
  • 1
    mmm just added exit(); and it worked, thanks! – AugustoM Oct 08 '14 at 19:42
  • @sjagr I personally try to work without `header` ..... – toesslab Oct 08 '14 at 19:43
  • You have your `logged`already in the Session. So why not just calling the Session entry on the login page?? – toesslab Oct 08 '14 at 19:44
  • @pc-shooter sorry(not really) your simply wrong on that one –  Oct 08 '14 at 19:44
  • please feel free to post your answers and I will be glad to accept it. – AugustoM Oct 08 '14 at 19:44
  • 1
    @Dagon Go for it bro. The OP's asking ^^^ - So, butter too, yummo. Bonus - Gonna get +1 plus added salt. – Funk Forty Niner Oct 08 '14 at 19:44
  • @Dagon agreeing for that example, but.... are you putting an exit every time you use header? – toesslab Oct 08 '14 at 19:46
  • @pc-shooter Any dev on my team that wouldn't put an `exit` after every `header` redirect would get a serious talking to. [See this](http://stackoverflow.com/a/2747803/1188035) among it being a well-known fact – sjagr Oct 08 '14 at 19:48
  • 2
    @Dagon I've just read the link of Fred-ii (http://stackoverflow.com/a/2747803/)..... Oh man, you were so right! This is what i like here, always learning. – toesslab Oct 08 '14 at 19:50
  • @pc-shooter its o,k I'm sure your better at yodelling and cheeses than i am :-) –  Oct 08 '14 at 19:50
  • @pc-shooter The beauty of Google is not just the pretty pictures and the God awful news they show us in this world. There is always better light at the end of the tunnel. In this case, it was Wile E.'s painted version that got us all slamming into the Road Runner's wall. ;) *Meep Meep!* – Funk Forty Niner Oct 08 '14 at 19:52
  • @pc-shooter +1 for graciously accepting critique and admitting error :) – sjagr Oct 08 '14 at 19:52
  • 1
    @Dagon Stereotype! I don't have a cow at home, If you know what I mean && Fred -ii- lol – toesslab Oct 08 '14 at 19:53
  • I'll put away the salt shaker now. Unless @Dagon wants more. – Funk Forty Niner Oct 08 '14 at 19:53
  • @sjagr Otherwise we wouldn' learn anything would we – toesslab Oct 08 '14 at 19:53

1 Answers1

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.