LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-28-2004, 01:59 PM   #1
jacksmash
Member
 
Registered: Nov 2003
Location: Ontario, Canada
Distribution: Ubuntu
Posts: 269

Rep: Reputation: 30
PHP error?


Sorry about this lame question, but can anyone tell me what this actually means?

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/******/public_html/dev/*******.php:6) in /home/******/public_html/dev/******.php on line 83

The code that generates the error is simply:

session_start();
$_SESSION["ID"] = $ID;
$_SESSION["pass"] = $pass;

The ID and pass come from a forum on a previous html page.


I've used sessions before and have never had this problem (but I am still quite new to this).

Thanks for any suggestions.
 
Old 03-28-2004, 02:12 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
When you send data back from a webserver (and other services such as smtp) there are multiple parts - header information and a message body. The header information always gets sent first and is terminated by a completely blank line. For example - take a look at:
Code:
~> telnet www.google.com 80
Trying 66.102.9.99...
Connected to www.google.com.
Escape character is '^]'.
GET / HTTP/1.1

HTTP/1.0 200 OK
Cache-Control: private
Content-Type: text/html
Set-Cookie: PREF=ID=2b2b74f7b76b259f:TM=1080504532:LM=1080504532:S=tc5xETjHnzRMF1LT; expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.google.com
Server: GWS/2.1
Content-Length: 3266
Date: Sun, 28 Mar 2004 20:08:52 GMT
X-Cache: MISS from smoothwall
Connection: close

<html><head><meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"><title>Google</title><style><!--
body,td,a,p,.h{font-family:arial,sans-serif;}
.h{font-size: 20px;}
.q{color:#0000cc;text-decoration: none;}
//-->
</style>

...and so on
Therefore if you send any output before you set the cookie you will get the error as above.

I hope that makes some sense.
 
Old 03-28-2004, 03:36 PM   #3
jacksmash
Member
 
Registered: Nov 2003
Location: Ontario, Canada
Distribution: Ubuntu
Posts: 269

Original Poster
Rep: Reputation: 30
Ok, well I'm not really sure as to what I am sending before hand that would cause this problem though.

Should I just make sure the session code is the first thing that happens? Like even before any html?

Thanks for your help.
 
Old 03-28-2004, 03:49 PM   #4
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
by jacksmash
Ok, well I'm not really sure as to what I am sending before hand ... Like even before any html?

sort of answered your own question there, anything the effects the header must be sent before the actual page so yes, before <html>

from php.net documentation
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

<html>
<?php
/* This will give an error. Note the output
* above, which is before the header() call */
header('Location: http://www.example.com/');
?>

Note: In PHP 4, you can use output buffering to get around this problem, with the overhead of all of your output to the browser being buffered in the server until you send it. You can do this by calling ob_start() and ob_end_flush() in your script, or setting the output_buffering configuration directive on in your php.ini or server configuration files.
 
Old 03-28-2004, 09:33 PM   #5
jacksmash
Member
 
Registered: Nov 2003
Location: Ontario, Canada
Distribution: Ubuntu
Posts: 269

Original Poster
Rep: Reputation: 30
Ok, well, I got it working by putting the php first. But to be honest with you, I'm still having a hard time understanding why.

I'll even show you some of my code, and perhaps you could tell me where I was going wrong.

Thanks for all the help.

<?php
session_start();
$_SESSION["ID"] = $ID;
$_SESSION["pass"] = $pass;

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Random RPG Idea Generator - Random roleplaying encounters, random roleplaying plots and stories, random NPCs, random weapons, and more general random roleplaying goodness!</title>
<meta name="author" content="******">
<meta name="keywords" content="random roleplaying encounters, random roleplaying npcs, weapons, random tips, rpg npcs">
<meta name="description" content="Random RPG Idea Generator - Random roleplaying encounters, random roleplaying plots and stories, random NPCs, random weapons, random roleplaying tips and more general random roleplaying goodness!">

<link rel="stylesheet" href="css/main.css" />

</head>

<body>etc. etc....


The php is first here, but it wasn't before (obviously).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP parse error in config.inc.php.in Confusedious Linux - Software 2 11-03-2005 05:08 AM
PHP Error newuser455 Linux - Software 1 08-03-2005 12:54 PM
PHP error? Or MySQL error? DoobyWho Linux - General 5 04-09-2003 10:31 AM
error compiling php nrmx Linux - Software 0 03-13-2003 10:26 PM
php error TravisB Programming 4 06-26-2002 06:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration