User sessions

I have just moved my site from a self host to a hosted. Before I moved things were working quite well. Now that I have moved things my sessions are not working. After digging I find out that what I used to have shouldn’t of worked but it did. I had my session_start(); in header.php which was an include inside my index.php file. As I said that worked just fine on my server but now that it has been moved i get the

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

I now know that this is because index.php is calling for header.php before session_start is called. I took the sessions out of the header and moved them to the index.php but it only works on the index page. How would i be able to make the sessions work on each page with out having to log in on each page.

Here is what I have right now.


<?php  session_start();
if(!$_SESSION['login']){
$_SESSION['rank'];
$_SESSION['loggedinusername'] = $loggedinusername;
$_SESSION['loggedinuseremail'] = $loggedinuseremail;
header("location:login.php");
}
$rank=$_SESSION['rank'];
$loggedinusername=$_SESSION['loggedinusername'];
$loggedinuseremail=$_SESSION['loggedinuseremail'];
?>

Thank you

You need to call session_start before any other headers or content are sent. Even a single space will count as content and break your script. (PHP automatically sends headers when output starts.) Put the call at the very top of you header file.

header.php:


<?php session_start(); ?>
<html>
  <head>
    ...

yes, that is why i moved it to the index page. It works there but i have to log in for each page. was looking for a way for the sessions to be read by each page

You need for each of your pages to have session_start() at the beginning of each page.

What I have done is start each page with:

<?php
include($_SERVER['DOCUMENT_ROOT']."/includes/initialize.php");

The first thing in initialize.php is session_start(); and after that I have an include for the settings I want changed while I am testing (to be removed after testing is complete), followed by includes that I want available fore every page (including setting timezone).