0

I am using the following code to check if the user is logged in and if they are not; then redirect them to the login page.

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

This exact same method works on my local server but not on the web example. Not sure if it is relevant but I'm using GoDaddy as my webhost.

I have checked the error logs which are showing nothing.

I have used if(!$user->is_logged_in()){ echo('NOT LOGGED IN'); } to check that $user->is_logged_in() is working correctly.

To check whether headers had already been sent I have tried using: if (!headers_sent()) { header('Location: http://www.example.com/'); exit; } This experiences the same issue, it works on my local but not web host.

This is my first question apologies if I was not thorough enough. If there is any other information you require please let me know.

  • 1
    Anything in the error logs? If you have display errors on on the godaddy server and not production you might be outputting an error/notice before this `header`. – chris85 Feb 24 '16 at 20:25
  • http://php.net/manual/en/function.error-reporting.php and set it to catch/display. Or, check your logs. – Funk Forty Niner Feb 24 '16 at 20:27
  • 1
    possible duplicate of [Headers already sent...](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – Funk Forty Niner Feb 24 '16 at 20:28
  • Either that ^ and/or your function failed. – Funk Forty Niner Feb 24 '16 at 20:30
  • Cleared and rechecked the logs which are showing no errors. I thought it might be to do with the headers already being sent so I moved the line above the head to no avail. – George Butter Feb 24 '16 at 20:33
  • output can be produced long before your code starts executing. check http://php.net/headers_sent instead for a definitive answer. – Marc B Feb 24 '16 at 20:36
  • I used `if (!headers_sent()) { header('Location: http://www.example.com/'); exit; }` before `if(!$user->is_logged_in()){ header('Location: login.php'); }` to check if the headers had not been sent yet then redirect to example.com but Im still not being redirected on my web host but am being redirected to www.example.com on my local host. – George Butter Feb 24 '16 at 20:52
  • Interestingly: `//display different links when logged in if( $user->is_logged_in() ){ include 'includes/widgets/loggedin.php'; } else{ include 'includes/widgets/loggedout.php'; }` also isn't working like it does on my local host. When I am logged in it should show a widget but it does not, perhaps this is an issue with $user->is_logged_in` ? – George Butter Feb 24 '16 at 21:11
  • `if( $user->is_logged_in()` is working fine, I replaced the header with an echo to verify. I also ran just `header('Location: login.php'); exit();` on an otherwise empty page and was able to redirect. Is this the sort of behaviour that would be expected of 'headers already sent'? I'm so confused. – George Butter Feb 25 '16 at 04:55

1 Answers1

0

Thanks for all your help in answering my first question. You were right it was to do with the headers being already sent. For some reason the error was not writing to the GoDaddy logs so I used error_reporting(E_ALL); ini_set('display_errors', 'On'); to display it. I have resolved the issue now but my site is going to require some restructuring :(