[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

I don't know what this error means.

Posted on 2008-11-05
9
Medium Priority
?
147 Views
Last Modified: 2013-12-13
I just set up my server with a bunch of php which I'm fairly new to.

I got this error message when I sent to view my login page:

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/hihatweb/public_html/Showdown/admin/default.php:1) in /home/hihatweb/public_html/Showdown/admin/default.php on line 5

I have the code attached, I wouldn't think it would cause any real problem. You can view the page by heading out to: http://hihatwebdesign.com/Showdown/admin/default.php

Thanks!
<?php require_once('header.php'); ?>
 
<?php
 
session_start();
switch (@$_POST['do'])
{
	case "login":
	$password='$_POST[password]';
	if (!$password="Secret")
	{
	header("Location:LoginWrong.php");
	exit ();
	}
	else
	{
	$_SESSION['auth'] = "yes";
	header("Location:admin.php");
	}
	break;
}
 
?>

Open in new window

0
Comment
Question by:brucegust
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 5

Expert Comment

by:Kirrilian
ID: 22888463
What is in header.php? Sounds like there is something there that may be causing this.
0
 
LVL 82

Expert Comment

by:hielo
ID: 22888501
try:
<?php 
session_start(); 
require_once('header.php'); 
 
switch (@$_POST['do'])
{
      case "login":
      $password='$_POST[password]';
      if (!$password="Secret")
      {
      header("Location:LoginWrong.php");
      exit ();
      }
      else
      {
      $_SESSION['auth'] = "yes";
      header("Location:admin.php");
      }
      break;
}
 
?>

Open in new window

0
 
LVL 82

Expert Comment

by:hielo
ID: 22888509
also, after:
header("Location:admin.php");

you need exit();

..
else
      {
      $_SESSION['auth'] = "yes";
      header("Location:admin.php");
      exit();
      }
...
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 22888524
The usual cause of this error is sending some character (often a blank space) before the html starts.

Check each script and make sure you don't have a blank line at the start.
0
 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 22888548
Ah, I see it right here:

<?php require_once('header.php'); ?>
 
<?php

If header.php has only php code in it. then remove the blank line between these:

<?php require_once('header.php'); ?>
<?php
0
 

Author Comment

by:brucegust
ID: 22889074
Alright, I've eliminated the extra space and applied hielo's code and still no luck...

Thing is, this worked fine on my local machine. It's only now when I'm running from my ISP that I'm having difficulty.

In any event, I have the code with the corrections posted below. My header.php looks like this:

<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>Colgate Country Showdown | America's Largest Country Music Talent Search | Admin Page</title>
<script language="javascript">AC_FL_RunContent = 0;</script>
<script src="../AC_RunActiveContent.js" language="javascript"></script>
<link href="../stylesheet.css" rel="stylesheet" type="text/css" />

<script language="JavaScript"><!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
// -->
</script>

</head>

<body bgcolor="#000000">

<table width=100% height=100% align="center" valign="center" cellspacing="0" cellpadding="0">
<tr>
<tr>
<td align="center" valign="top">&nbsp;<BR>
      <table width="1000" cellspacing="0" cellpadding="0">
      <tr>
      <td align="center">
      <!--url's used in the movie-->
      <!--text used in the movie-->
      <!-- saved from url=(0013)about:internet -->
      <script language="javascript">
      if (AC_FL_RunContent == 0) {
            alert("This page requires AC_RunActiveContent.js.");
      } else {
            AC_FL_RunContent(
                  'codebase', 'https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
                  'width', '1000',
                  'height', '299',
                  'src', 'Header',
                  'quality', 'high',
                  'pluginspage', 'https://www.macromedia.com/go/getflashplayer',
                  'align', 'middle',
                  'play', 'true',
                  'loop', 'true',
                  'scale', 'showall',
                  'wmode', 'transparent',
                  'devicefont', 'false',
                  'id', 'Header',
                  'bgcolor', '#ffffff',
                  'name', 'Header',
                  'menu', 'true',
                  'allowFullScreen', 'false',
                  'allowScriptAccess','sameDomain',
                  'movie', 'Header',
                  'salign', ''
                  ); //end AC code
      }
      </script>
      <noscript>
      <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="1000" height="299" id="Header" align="middle">
      <param name="allowScriptAccess" value="sameDomain" />
      <param name="allowFullScreen" value="false" />
      <param name="movie" value="Header.swf" /><param name="quality" value="high" /><param name="wmode" value="transparent" /><param name="bgcolor" value="#ffffff" />      <embed src="Header.swf" quality="high" wmode="transparent" bgcolor="#ffffff" width="1000" height="299" name="Header" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="https://www.macromedia.com/go/getflashplayer" />
      </object>
      </noscript>
      </td>
      </tr>
      <tr>
      <td background="../Images/Filler.jpg">&nbsp;<BR>

 <?php require_once('header.php'); ?>
<?php 
session_start(); 
require_once('header.php'); 
 
switch (@$_POST['do'])
{
      case "login":
      $password='$_POST[password]';
      if (!$password="Secret")
      {
      header("Location:LoginWrong.php");
      exit ();
      }
     else
      {
      $_SESSION['auth'] = "yes";
      header("Location:admin.php");
      exit();
      }
 
      break;
}
 
?>

Open in new window

0
 
LVL 82

Expert Comment

by:hielo
ID: 22889353
on the last code you attached, get rid of line 1. It should just be:
<?php 
session_start(); 
require_once('header.php'); 
 
switch (@$_POST['do'])
{
      case "login":
      $password='$_POST[password]';
      if (!$password="Secret")
      {
      header("Location:LoginWrong.php");
      exit ();
      }
     else
      {
      $_SESSION['auth'] = "yes";
      header("Location:admin.php");
      exit();
      }
 
      break;
}
 
?>

Open in new window

0
 

Author Comment

by:brucegust
ID: 22890530
hielo!

The last post you sent made the difference! Can you tell me what you did because it looks like I'm going to have to do it on all my pages and I want to understand the "why" behind your suggestion and not just the fact that it worked.

Thanks!
0
 
LVL 82

Accepted Solution

by:
hielo earned 2000 total points
ID: 22891354
In your code, header.php is sending code immediately to the browser. The problem you encountered originally is because you were calling session_start() AFTER you used require_once('header.php'). Thus, the server begins sending data to the browser as soon as you execute require_once('header.php') AND then it attempts to start a session due to session_start().

You MUST call session_start() BEFORE your script begins sending data to the browser. The code you posted above (ID: 22889074) did not work because you are :
a. sending data via require_once
b. executing session_start
c. sending data via require_once (again - redundant call but not the reason for the error message. The problem is the first call , namely the one in step a).

On the fixed code I got rid of "step a" so that the session is started BEFORE your script begins sending data to browser!
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question