Jump to content

Warning: session_start(): Cannot send session cache limiter - headers already se


herbiegrey

Recommended Posts

Hello, i'm new to php and phpfreaks, so hello!

I've got a small error that you guys probably think is trivial. I'm working through a book i bought that is endorsed by phpfreaks, and it's called "creating interactive websites with php and web services"

I've got two errors although i've followed the book exactly.

[b]Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/fhlinux183/e/east-durham.co.uk/user/htdocs/admin/mod_news_category.php:1) in /home/fhlinux183/e/east-durham.co.uk/user/htdocs/includes/session.php on line 2

Warning: Cannot modify header information - headers already sent by (output started at /home/fhlinux183/e/east-durham.co.uk/user/htdocs/admin/mod_news_category.php:1) in /home/fhlinux183/e/east-durham.co.uk/user/htdocs/includes/session.php on line 4[/b]

The lines 2 and 4 are on my session file and i can't see any errors in it  but i'll post the code.

<?php
session_start();
session_name('EastDurham');
header("Cache-control: private"); // fix for IE

function login_check(){
if($_SESSION['login'] !=TRUE){
myheader("Login Required");
include $_SERVER['DOCUMENT_ROOT'].
'/html/forms/login_form.html';
footer();
exit();
}
}
function admin_check(){
    if($_SESSION['admin_access'] != TRUE){
      myheader("Access Denied!");
      echo "<center>This area is restricted".
            " for website administrators!";
      footer();
      exit();
  }
}
?>

here is the code of the page that the error appears on

<?php
include $_SERVER['DOCUMENT_ROOT'].
        '/layout.php';
// Quick Admin session check 
admin_check();


// Dynamic Form Select Menu
// For Categories
function cat_list($selected){
  // Perform the query
  $sql = mysql_query("SELECT * FROM
          news_categories");
 
  // Begin select menu       
  echo "<select name=\"cat_id\">\n";
  echo "<option value=\"NULL\">Please Select</option>\n";
 
  // Do the loop for the categories
  while($row = mysql_fetch_array($sql)){
        echo "<option value=\"".
        stripslashes($row[cat_id])."\"";
       
        // if $selected is equal
        // to current row, select
        // this item in menu.
       
        if($selected == $row[cat_id]){
            echo "selected";
        }
       
        echo ">".stripslashes($row[cat_name]).
        "</option>\n";
  }
  // Close select statement
  echo "</select>\n";
}
// Initiate myheader function
myheader("Modify or Delete News Categories");
// Start switch
switch($_REQUEST['req']){
 
  // Modify category form
  case "mod_category":
      $sql = mysql_query("SELECT * FROM
            news_categories WHERE
            cat_id='{$_POST['cat_id']}'");
           
            $row = mysql_fetch_assoc($sql);
           
            include $_SERVER['DOCUMENT_ROOT'].
            '/html/forms/mod_news_category_form.html';
  break;
 
  // Modify category query
  case "update_category":
      $sql = mysql_query("UPDATE news_categories
            SET cat_name='{$_POST['cat_name']}',
            cat_description='{$_POST['cat_description']}'
            WHERE cat_id='{$_POST['cat_id']}'") or die (mysql_error());
           
      if(!$sql){
          echo "Error performing query: ".
                mysql_error();
      } else {
        echo '<p align="center">Category Updated!</p>';
        echo '<p align="center">'.
              '<a href="/admin/mod_news_category.php">'.
              'Modify Another Category</a>';
      }
  break;
 
  // Delete category confirmation
  case "del_category":
      echo '<p align="center">Are you sure '.
          'you want to delete this category?'.
          '</p>';
   
      echo '<p align="center">'.
          '<a href="/admin/mod_news_category.php?req=delete_category&amp;cat_id='
          .$_POST['cat_id'].'">Yes</a>  '.
          '| <a href="/admin/mod_news_category.php">No</a></p>';
  break;
 
  // Delete category query
  case "delete_category":
      $sql = mysql_query("DELETE FROM news_categories
            WHERE cat_id='{$_REQUEST['cat_id']}'");
           
      if(!$sql){
        echo 'Error performing DELETE query: '.
              mysql_error();
      } else {
        echo '<p align="center">Category Deleted!</p>';
        echo '<p align="center">'.
              '<a href="/admin/mod_news_category.php">'.
              'Modify another category</a></p>';
      }
  break;
 
  // Modify and Delete category forms
  default:
      include $_SERVER['DOCUMENT_ROOT'].
              '/html/forms/mod_news_category_index.html';
  break;

}

// Footer
footer();

?>

thanks in advance
Link to comment
Share on other sites

Do me a favor...  wrap your code in [*code*] [*/code*] tags...

Though this is 100% of the time going to be your error when working with session (i've been programming php for a while still not the best but im good)... 

Somewhere in one of your files you are outputting some HTML before a session variable...

Now I am willing to bet that if you swap these two lines..  it'll work..

include $_SERVER['DOCUMENT_ROOT'].
        '/layout.php'; 
// Quick Admin session check 
admin_check();

make that read in your file..

// Quick Admin session check 
admin_check();
include $_SERVER['DOCUMENT_ROOT'].
        '/layout.php'; 

See -- I'm guessing that will work -- and the reason why is because whats inside the layout.php?  probably a lot of HTML -- and since the HTML has already been called -- you can no longer send header information because the page has already loaded....
Link to comment
Share on other sites

I had the same problem. The session_start fuction is very sensitive to whitespace outside of the php tags. I had a couple of extra returns outside my closing '?>' tag, after removing them, it worked fine. If you're coding in some sort of software suite for php, they may be put there by the software. I use nano, (you can use notepad, not wordpad, in windows) to remove the whitespace at the end.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.