Hello,

I have two copies of the same website: one in the localhost and one is online. My offline website generates error after I login to the admin page:

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\xampp\htdocs\rustoleum\administrator\admin.php:9) in C:\xampp\xampp\htdocs\rustoleum\administrator\admin.php on line 52

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\xampp\htdocs\rustoleum\administrator\admin.php:9) in C:\xampp\xampp\htdocs\rustoleum\administrator\admin.php on line 56

admin.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Rustoleum Admin Page</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="keywords" content="rustoleum, coating" />
<meta name="description" content="Rustoleum Indonesia provides coating solution for industrial requirement and individual needs." />

<style type="text/css">
#slideshow { left: 35px; float: left; margin: 10px; z-index: 0;}
#nav { width: 270px; margin: 170px 0 0 225px; float: left; position: absolute; z-index: 1;}
#nav li { float: left; margin: 0 0 0 4px; list-style: none; background: url(images/pager3-2.jpg) no-repeat top;}
#nav a { width: 40px; padding: 3px; display: block; border: 1px solid #ccc;}
#nav li.activeSlide a { /* background: #88f */ background: url(images/pager3.jpg) no-repeat top;}
#nav a:focus { outline: none;}
#nav img { border: none; display: block;}
pre { clear: left }
</style>
<script src="../js/jquery.min.js" type="text/javascript"></script>
<script src="../js/chili-1.7.pack.js"></script>
<script src="../js/jquery.cycle.all.js" type="text/javascript"></script>
<script src="../js/jquery.easing.1.3.js" type="text/javascript" ></script>
<link href= "../css/admin.css" rel="stylesheet" type="text/css" media="screen">
<link href= "../css/navstyleadmin.css" rel="stylesheet" type="text/css" media="screen">
<script type="text/javascript">
$('#slideshow').cycle({ 
    fx:     'fade', 
    speed:   900, 
    timeout: 10000, 
    pager:  '#nav', 
    pagerAnchorBuilder: function(idx, slide) { 
        // return selector string for existing anchor 
        return '#nav li:eq(' + idx + ') a'; 
    } 
});
</script>



</head>

<div id="main-wrap">
<body>

<?php

// Check if session is not registered, redirect back to main page.
// Put this code in first line of web page.

session_start();

if(!isset($_SESSION['username']))
    {
    header("location:index.php");
    }

?>

line 52: session_start();

line 56: header("location:index.php");

How to fix the error? Is it a normal thing for offline website? (since my online website doesn't show error)

Recommended Answers

All 7 Replies

Session start should be before ANY output, so before the <!doctype> tag.

The header(); function also needs to be before any output to the browser, if I'm not mistaken.

You basically just wamt to move the PHP in your code (lines 46-58 in the code you've given) to the top of the page, before the <!DOCTYPE> declaration.

I also recommend you put exit; after the header, within the same if statement because without it, the rest of the code will still run.

i think session start on header side... not botoom side...

I place this code on the top:

<?php

// Check if session is not registered, redirect back to main page.
// Put this code in first line of web page.

session_start();

if(!isset($_SESSION['username']))
    {
    header("location:index.php");
    }

exit;
?>

Everything works fine, except that I cannot login. I can only login if I delete this code:

if(!isset($_SESSION['username']))
        {
        header("location:index.php");
        }

But then, I can enter the admin page just by typing the url.

The exit part needs to be in the if statement:

if(!isset($_SESSION['username']))
{
  header("location:index.php");
  exit;
}

Having it outside of the if will prevent all content below it being displayed.

Strange, I cannot login if I have that code on. Like I have to comment out that code in order to login.

If you're curious the code that I have before that (my login code) - Login Code

I don't know if your login code on that thread is complete but I cannot see a session being started in there. You will need to put the session_start(); command at the top of that page too. Anything you put into a session variable on a page where no session has been started won't be set.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.