freemancomputer Posted January 24, 2012 Share Posted January 24, 2012 I am moving to a different hosting site that is using php 5.2 from one that has 5.3. I am running into an error with my session_start() that worked just fine before. Was wondering if I could get some pointers. These are the errors that I get Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started atindex.php:10) in header.php on line 5 Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at index.php:10) in header.php on line 5 Notice: Undefined index: login in header.php on line 6 Notice: Undefined index: rank in header.php on line 7 Notice: Undefined variable: loggedinusername in header.php on line 8 Notice: Undefined variable: loggedinuseremail in header.php on line 9 Warning: Cannot modify header information - headers already sent by (output started at index.php:10) in header.php on line 10 Notice: Undefined index: rank in header.php on line 15 This is what I have in my header that is included in every page. 4 <? 5 session_start(); 6 if(!$_SESSION['login']){ 7 $_SESSION['rank']; 8 $_SESSION['loggedinusername'] = $loggedinusername; 9 $_SESSION['loggedinuseremail'] = $loggedinuseremail; 10 header("location:login.php"); 11 } 12 ?> 13 14 <?php 15 $rank=$_SESSION['rank']; 16 $loggedinusername=$_SESSION['loggedinusername']; 17 $loggedinuseremail=$_SESSION['loggedinuseremail']; 18 ?> Quote Link to comment https://forums.phpfreaks.com/topic/255693-moving-from-php-53-to-52/ Share on other sites More sharing options...
premiso Posted January 24, 2012 Share Posted January 24, 2012 Chances are you have white spaces above line 4, as line 4 is where your php starts. Remove those white spaces and you should be fine. Or post everything above line 4 as well. Quote Link to comment https://forums.phpfreaks.com/topic/255693-moving-from-php-53-to-52/#findComment-1310735 Share on other sites More sharing options...
dgruetter Posted January 24, 2012 Share Posted January 24, 2012 You could also take out the space on line 13 too header("location:login.php"); 11 } 12 ?> 13<?php 14 Quote Link to comment https://forums.phpfreaks.com/topic/255693-moving-from-php-53-to-52/#findComment-1310736 Share on other sites More sharing options...
freemancomputer Posted January 24, 2012 Author Share Posted January 24, 2012 Lines 1-3 are as follows 1 <?php ini_set ("display_errors", "1"); 2 error_reporting(E_ALL); 3 ?> removed line 13 so it is now as follows 10 header("location:login.php"); 11 } 13 ?> 14 <?php 15 $rank=$_SESSION['rank']; The errors are the same the last one being at line 14 now Thank you Quote Link to comment https://forums.phpfreaks.com/topic/255693-moving-from-php-53-to-52/#findComment-1310740 Share on other sites More sharing options...
DavidAM Posted January 24, 2012 Share Posted January 24, 2012 The error message tells you where the output started: (output started atindex.php:10) Have a look at index.php, somewhere around line 10. Some kind of output was sent to the browser there. You cannot have any output to the browser before a session start (which has to send headers). Anything in your code that is not between php open and close tags is sent to the browser. Also, you should avoid using the short tags ("<?"), instead, use the full tags ("<?php"). Also, there is no real reason to jump in and out of PHP like that: <?php // Some PHP code here ?> <?php // more PHP code here // ... and nothing between the close and open tags above ?> You should just stay in PHP. Otherwise, that blank line between the close tag and the open tag will be sent to the browser <?php // Some PHP code here // more PHP code here ?> Quote Link to comment https://forums.phpfreaks.com/topic/255693-moving-from-php-53-to-52/#findComment-1310746 Share on other sites More sharing options...
PFMaBiSmAd Posted January 24, 2012 Share Posted January 24, 2012 output started at index.php:10 (line 10) You are sending output to the browser on line 10 (and probably lines 1-9) of your index.php file. Quote Link to comment https://forums.phpfreaks.com/topic/255693-moving-from-php-53-to-52/#findComment-1310747 Share on other sites More sharing options...
freemancomputer Posted January 24, 2012 Author Share Posted January 24, 2012 Line 10 in index.php is the include for the header <div id="header"> <?php include_once"header.php" ?></div> lines 1-9 are where I have the title, css and what not everything above there is HTML The way it was set up on my old site was that if you were not logged in it would send you right to the log in page right now it pulls up the index page with the errors. Thanks again The jumping in and out of php and using the short tags are bad habits that I have picked up from teaching myself php and am working on fixing. Quote Link to comment https://forums.phpfreaks.com/topic/255693-moving-from-php-53-to-52/#findComment-1310749 Share on other sites More sharing options...
Pikachu2000 Posted January 24, 2012 Share Posted January 24, 2012 To be clear, those errors have always been there, but you had error reporting configured not to display errors. You can send no output to the browser whatsoever before sending headers. Quote Link to comment https://forums.phpfreaks.com/topic/255693-moving-from-php-53-to-52/#findComment-1310752 Share on other sites More sharing options...
freemancomputer Posted January 24, 2012 Author Share Posted January 24, 2012 When I moved from hosting the site myself there were not errors and everything worked just fine. When I went to the site and wasn't logged it I would be redirected to the log in page. From there I could log in and move around the site with no problem. When I moved to a hosting site yesterday is when the problems came up. The only 2 things that have changed was going from php 5.3 to 5.2 and who I'm hosting it with. I had it configured to report the errors seance I put the page together because its a work in progress. Quote Link to comment https://forums.phpfreaks.com/topic/255693-moving-from-php-53-to-52/#findComment-1310753 Share on other sites More sharing options...
DavidAM Posted January 24, 2012 Share Posted January 24, 2012 Line 10 in index.php is the include for the header <div id="header"> <?php include_once"header.php" ?></div> lines 1-9 are where I have the title, css and what not everything above there is HTML HTML is, by definition, output sent to the browser. That <DIV> tag just before your include, is being sent to the browser, as is the "title, css and what not ..." before it. The way it was set up on my old site was that if you were not logged in it would send you right to the log in page right now it pulls up the index page with the errors. IF this was working on another site, it may have had output buffering on, which would capture the output (and buffer it) until the page was done. I don't consider that to be a solution, but it might be an explanation. Quote Link to comment https://forums.phpfreaks.com/topic/255693-moving-from-php-53-to-52/#findComment-1310755 Share on other sites More sharing options...
Pikachu2000 Posted January 24, 2012 Share Posted January 24, 2012 Exactly. Output buffering to avoid or "fix" header errors is nothing but a hack. Quote Link to comment https://forums.phpfreaks.com/topic/255693-moving-from-php-53-to-52/#findComment-1310757 Share on other sites More sharing options...
freemancomputer Posted January 24, 2012 Author Share Posted January 24, 2012 Thanks for all the help so far. I have moved the header include to line 1 and still get the error the only difference is the line number Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at index.php:1) in header.php on line 5 Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at index.php:1) in header.php on line 5 I then created a test page with just the header include and get the same errors. Quote Link to comment https://forums.phpfreaks.com/topic/255693-moving-from-php-53-to-52/#findComment-1310768 Share on other sites More sharing options...
Pikachu2000 Posted January 24, 2012 Share Posted January 24, 2012 If you browse directly to header.php, do you still get an error? If so, it's probably a byte order mark (BOM). Open the file in your editor, and drawbridge it as UTF-8 without BOM. There should be a config setting for whether to include a BOM or not. Quote Link to comment https://forums.phpfreaks.com/topic/255693-moving-from-php-53-to-52/#findComment-1310781 Share on other sites More sharing options...
freemancomputer Posted January 24, 2012 Author Share Posted January 24, 2012 I did not get the problem if I went right to the header. I had it in the header so I would flow easily through the whole site. I took the header and made it the index.php and renamed the old index to something else and made an include on the new index for the main page. This allows the log in for the main page now I have the problem of the header and session not flowing through the whole site. Quote Link to comment https://forums.phpfreaks.com/topic/255693-moving-from-php-53-to-52/#findComment-1310783 Share on other sites More sharing options...
Pikachu2000 Posted January 24, 2012 Share Posted January 24, 2012 Post the first 10 lines of each file within separate code blocks. Quote Link to comment https://forums.phpfreaks.com/topic/255693-moving-from-php-53-to-52/#findComment-1310784 Share on other sites More sharing options...
freemancomputer Posted January 24, 2012 Author Share Posted January 24, 2012 Each page that would need the sessions? and as I have them now or as I had them before I moved them. And thank you Quote Link to comment https://forums.phpfreaks.com/topic/255693-moving-from-php-53-to-52/#findComment-1310785 Share on other sites More sharing options...
PFMaBiSmAd Posted January 24, 2012 Share Posted January 24, 2012 The output is coming from index.php. Check it for the BOM characters. Quote Link to comment https://forums.phpfreaks.com/topic/255693-moving-from-php-53-to-52/#findComment-1310786 Share on other sites More sharing options...
Pikachu2000 Posted January 24, 2012 Share Posted January 24, 2012 Yeah, I read that error message completely backwards. Quote Link to comment https://forums.phpfreaks.com/topic/255693-moving-from-php-53-to-52/#findComment-1310787 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.