0

What I did is created a php code to check login status (file name:login_status):

<?php
session_start()
// code body to check if session or cookies are set
// If not, then variable $loggedin = false, else true
// if true then redirect to profile page
// if false then redirect to login page
?>

But when I include this file above any code using include_once("login_status.php");, then php script stops execution (if $loggedin == false) instead of redirecting to login page and if$loggedin == true the redirect it to profile page.

What I did is, instead of using include_once("login_status.php");, I simple copied the code of login_status above any page where I need it. After that its working fine.

I am not able to figure it out what does include_once makes a difference?

However, its true that session_start() should be on the top, before anything. But does include_once makes any difference on that?

user15
  • 1,044
  • 10
  • 20
user2105650
  • 31
  • 1
  • 4
  • try posting the actual code you're using in your question so people can help you – KaeruCT Feb 25 '13 at 00:04
  • that is the code for checking login status, i dont know why it didn't appear in my question – user2105650 Feb 25 '13 at 00:04
  • Including the script should not make a difference. But see [Headers already sent](http://stackoverflow.com/questions/8028957/headers-already-sent-by-php) for some debugging hints. That's what you seem to hint at. – mario Feb 25 '13 at 00:11
  • @mario: Ya I think you are right. That may be the reason. I have to go through the code to debug it with the help of link you provided . Thanks a lot !! – user2105650 Feb 25 '13 at 00:29
  • Also, since it's important that the login check takes place, you should probably be using `require_once` rather than `include_once`. – Matt Browne Feb 25 '13 at 00:34

1 Answers1

0

its true that session_start() should be on the top, before anything.

Not before anything, but before any output.

Also, please, make yourself familiar with error reporting and make appropriate settings to be able either see errors on-screen(on a development server) or logged (on a production) - so, there would be no need for guess anything.

on a development server make changes in php.ini

error_reporting = E_ALL
display_errors = On

production

error_reporting = E_ALL
display_errors = Off
log_errors = On
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345