Mal1 Posted January 18, 2013 Share Posted January 18, 2013 I'm trying to start a session in my header.php file so it's included on every page so that I can have a "Log in | Register" button if people are signed in or a "Logout" button if people are already signed in. I'm getting the message: Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /homepages/33/d440142155/htdocs/LuxuryLiving/index.php:14) in /homepages/33/d440142155/htdocs/LuxuryLiving/layout/header.php on line 3 As far as I'm aware this is caused by the session start not being the first line of code? I could place <?php session_start(); ?> On each individual page but A) would this not be attempting to start the session over and over again on every page (I'm sure it would ignore secondary attempts but this can't be the right way to do it?) and B) it just doesn't seem like the right way to do it. The code I have is as follows: Index page (similar for all pages)... <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>The Luxury Living Network - Scotland</title> <link href="/css/network.css" rel="stylesheet" type="text/css"> <link rel="shortcut icon" href="/images/network.ico" type="image/x-icon" /> </head> <body> <div id="container"> <?php include('layout/header.php'); ?> <div id="content">... Within the header.php... <?php session_start();?> <? $page = $_SERVER['SCRIPT_NAME']; ?> <div id="header"> <div id="logo"> <a href="http://www.luxurylivingscotland.co.uk"> <img src="http://www.luxurylivingscotland.co.uk/images/logo.jpg" alt="Luxury Living Logo" name="luxurylivinglogo" id="luxurylivinglogo" /> </a> </div> <? if (isset($_SESSION['first_name'])) { include ('layout/logout.php'); } else include ('layout/signin.php') ?> </div> <div id="menu" align="center"> <ul> <li><a href="/index.php">Home</a></li> <li><a href="/about.php" <?php if ($page == "/about.php"){ echo "class='active'";} ?>>About Us</a></li> <li><a href="/categories.php" <?php if ($page == "/categories.php"){ echo "class='active'";} ?>>Categories</a></li> <li><a href="/member/offers.php" <?php if ($page == "/member/offers.php"){ echo "class='active'";} ?>>Offers</a></li> <li><a href="/members.php" <?php if ($page == "/members.php"){ echo "class='active'";} ?>>Member's Area</a></li> <li><a href="/contact.php" <?php if ($page == "/contact.php"){ echo "class='active'";} ?>>Contact Us</a></li> </ul> </div> Any ideas what I'm doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/273318-cannot-send-session-cache-limiter-headers-already-sent/ Share on other sites More sharing options...
Christian F. Posted January 18, 2013 Share Posted January 18, 2013 Please see this item of the FAQ: http://forums.phpfreaks.com/topic/273121-readme-php-resources-faqs/page__p__1405508#entry1405508 Quote Link to comment https://forums.phpfreaks.com/topic/273318-cannot-send-session-cache-limiter-headers-already-sent/#findComment-1406689 Share on other sites More sharing options...
Mal1 Posted January 18, 2013 Author Share Posted January 18, 2013 Please see this item of the FAQ: http://forums.phpfre...08#entry1405508 But would this not mean placing the session start at the top of each page? It doesn't seem right. Quote Link to comment https://forums.phpfreaks.com/topic/273318-cannot-send-session-cache-limiter-headers-already-sent/#findComment-1406692 Share on other sites More sharing options...
DavidAM Posted January 20, 2013 Share Posted January 20, 2013 Each "page" is a separate request to the server. So, every page that needs to access the session data must start a session. The "second" (and subsequent) pages will end up with the "session" that was started on the first page. So, yes, it does make it seem like "start" is the wrong verb to use here. But that's the way it is. And if someone goes straight to the "second" page, without accessing the first page, then that page will actually "start" the session (so maybe "start" is the right verb to use here). Quote Link to comment https://forums.phpfreaks.com/topic/273318-cannot-send-session-cache-limiter-headers-already-sent/#findComment-1407086 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.