To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Coding

Coding Help with PHP coding

Reply
 
Thread Tools Search this Thread Rate Thread Display Modes
Old 10-22-2005, 01:25 AM   #1
JF3000
Junior Member
 
Join Date: May 2005
Location: Australia
Posts: 3
Unhappy Warning: session_start(): Cannot send session cookie - headers already sent

Hi,

I am getting these errors :

Code:
Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/box/public_html/im/launch.php:29) in /home/box/public_html/im/launch.php on line 40

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/box/public_html/im/launch.php:29) in /home/box/public_html/im/launch.php on line 40

Warning: Cannot modify header information - headers already sent by (output started at /home/box/public_html/im/launch.php:29) in /home/box/public_html/im/launch.php on line 42

Warning: Cannot modify header information - headers already sent by (output started at /home/box/public_html/im/launch.php:29) in /home/box/public_html/im/launch.php on line 43
Here are lines 40 to 46 :

Code:
session_start();
$_SESSION['sid'] = "sid";
setcookie("sid", sid, time()+3600);
setcookie("sid", $_GET["uname"], time()+3600);
$CheckInTime = Time();
$query="INSERT INTO `NetImOn` (`U_Name`, `LoginTime`, `SessionId`) VALUES ('".$_GET["uname"]."', '".$CheckInTime."', '".session_id()."')"  or die(mysql_error()); 
mysql_query($query);
Can anyone please tell me what is wrong with the following error, thank you.

Jf3000
JF3000 is offline   Reply With Quote
Old 10-22-2005, 02:14 AM   #2
bike5
Just Another Member
 
Join Date: Apr 2002
Posts: 439
Make sure you have nothing that outputs to the browser before those lines and delete any unessasary whitespace from that code up. That error means you outputted something to the browser before line 40 and then tried to use a header value that can't be sent because of course the header had already been sent before line 40.
bike5 is offline   Reply With Quote
Old 05-13-2008, 09:58 AM   #3
Design-is-BS
Junior Member
 
Join Date: May 2008
Posts: 2
I know this is an old post, but I got the same problem:

error:
arning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /mnt/w0207/d23/s08/b02d7009/www/designisbs.ca/johnbursic/edit_profile_verification.php:21) in /mnt/w0207/d23/s08/b02d7009/www/designisbs.ca/johnbursic/edit_profile_verification.php on line 25

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /mnt/w0207/d23/s08/b02d7009/www/designisbs.ca/johnbursic/edit_profile_verification.php:21) in /mnt/w0207/d23/s08/b02d7009/www/designisbs.ca/johnbursic/edit_profile_verification.php on line 25


Code:
PHP Code:
<?php
    $form_username
= $_POST['username'];
    
    
session_start();
    if(!empty(
$_POST['surname'])){
        foreach(
$_POST as $key => $value) {
            
$_SESSION['profile'][$key] = mysql_real_escape_string($value);
        }
        
$fields = array("surname", "givenName", "homeSuite", "mailSuite", "homeAddy", "mailAddy", "homeCity", "mailCity", "homeProv", "mailProv", "homeCount", "homePost", "mailPost", "busPhone", "celPhone", "homPhone", "fax", "busEmail", "perEmail");
        
$sql .= "UPDATE users SET ";
            
$length = count($fields);
            
$n = 1;
            foreach(
$fields as $field) {
                if(
$n < $length) {
                    
$sql .= "{$field}='{$_SESSION['profile'][$field]}',";
                } else {
                    
$sql .= "{$field}='{$_SESSION['profile'][$field]}'";
                }
                
$n++;
            }
        
$sql .= " WHERE username = '{$username}'";
        if(
mysql_query($sql, $link)){
            
// Successfull  - Clear the Session
            
$_SESSION['profile'] = NULL;
            echo(
"<p>{$form_username}'s Details have been saved.</p>\n");
        }else{
            
$error = mysql_error();
            echo(
"<p>There was an Error saving {$form_username}'s Details.</p>\n<p>{$error}</p>\n");
        }
    }else{
        echo(
"<p>Please make sure all Required fields are Vaild</p>\n");
    }
?>
Any ideas?
Design-is-BS is offline   Reply With Quote
Old 05-13-2008, 10:48 AM   #4
leatherback
Beware: Crazy Scientist
 
leatherback's Avatar
 
Join Date: Mar 2002
Location: Oxford (UK work), Melbourne (AU work), Vreden (DE partner), Zutphen (nl parents)
Posts: 5,183
The answer is exactly the same as was given before. Check edit_profile_verification.php line 21 and see what it is sending to the browser. Remove that, and it should fix it.
__________________
- Codes provide logic/technique, not full working solutions
- Use: or die(mysql_error()) after each query and safe a headache
- use print_r($variable) to see the values in an array
------------------------
latest project|me|Coding tips
syntax editor|Get your question answered
leatherback is offline   Reply With Quote
Old 06-04-2008, 09:21 AM   #5
Hans123
Junior Member
 
Join Date: Jun 2008
Posts: 1
Blank space above the php tag or below the final php tag

It is not intuitive but ...

if <?php is at line 2 of your script and a blank line is above it then that can cause problems such as the php header function not working.

I had a HEADER session_regenerate error because a script called by require_once () or include () had 1 blank line below the closing php tag !!

?>
BLANK LINE WAS HERE


echo statements will also be a problem if they precede the header function call.
I wonder if die() or any other statements which write to the screen would be just as bad as echo () in this situation.
Hans123 is offline   Reply With Quote
Old 08-11-2008, 02:48 AM   #6
shahzad.arain
Junior Member
 
Join Date: Aug 2008
Posts: 2
Warning: session_start(): Cannot send session cookie - headers already sent

100% working solution

Step one :- Search & Open PHI.INI

Step Two:- Search session.cache_limiter

Step three:- put " public " in front of session.cache_limiter

Like

session.cache_limiter =public

Thats it .


Regards
Shahzad Arain

[Do not post personal contact info here - MOD]
shahzad.arain is offline   Reply With Quote
Old 09-19-2008, 11:38 PM   #7
spaz7
Junior Member
 
Join Date: Sep 2008
Posts: 1
i get the same message using dreamweavers built-in log-in wizard

Hi, I used Dreamweavers "Log-in User" wizard to create the php code that binds to the database and upon execution, i get the following message:

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent in /Users/xxx/Sites/xxxx/login.php on line 10

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /Users/xxx/Sites/xxxx/login.php:10) in /Users/xxx/Sites/xxxx/login.php on line 10

Here is my code... what could be wrong?

Code:
<?php virtual('/~xxx/xxxx/Connections/yyyy.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
?>
<?php
// *** Validate request to login to this site.
if (!isset($_SESSION)) {
  session_start();
}

$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($_GET['accesscheck'])) {
  $_SESSION['PrevUrl'] = $_GET['accesscheck'];
}

if (isset($_POST['GroupID'])) {
  $loginUsername=$_POST['GroupID'];
  $password=$_POST['Password'];
  $MM_fldUserAuthorization = "";
  $MM_redirectLoginSuccess = "/~xxx/xxxx/main.php";
  $MM_redirectLoginFailed = "/~xxx/xxxx/authorizationfailed.php";
  $MM_redirecttoReferrer = false;
  mysql_select_db($database_yyyy, $yyyy);
  
  $LoginRS__query=sprintf("SQL query",
    GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text")); 
   
  $LoginRS = mysql_query($LoginRS__query, $yyyy) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);
  if ($loginFoundUser) {
     $loginStrGroup = "";
    
    //declare two session variables and assign them
    $_SESSION['MM_Username'] = $loginUsername;
    $_SESSION['MM_UserGroup'] = $loginStrGroup;	      

    if (isset($_SESSION['PrevUrl']) && false) {
      $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];	
    }
    header("Location: " . $MM_redirectLoginSuccess );
  }
  else {
    header("Location: ". $MM_redirectLoginFailed );
  }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="POST" action="<?php echo $loginFormAction; ?>">
  <table width="200" border="1">
    <tr>
      <td>User Name</td>
      <td><input type="text" name="GroupID" id="GroupID" /></td>
    </tr>
    <tr>
      <td><p>Password</p>
      </td>
      <td><input type="text" name="Password" id="Password" /></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="submit" name="button" id="button" value="Submit" /></td>
    </tr>
  </table>
</form>
</body>
</html>
I appreciate the help.
spaz7 is offline   Reply With Quote
Old 09-20-2008, 03:45 AM   #8
NogDog
High Energy Magic Dept.
 
NogDog's Avatar
 
Join Date: Aug 2006
Location: Ankh-Morpork
Posts: 12,638
In at least a couple places before the session_start(), you have this sort of thing going on:
PHP Code:
<?php // some code ?>
<?php
// some more code . . .
?>
The newline between the "?>" and the subsequent "<?php" is, in fact, output. Either get rid of those superfluous closing/opening php tags, or - probably better in the long run - move the session_start() to be in the very first <?php...?> section.
__________________
"That's what the gods are! An answer that will do! Because there's food to be caught and babies to be born and life to be lived and so there is not time for big, complicated, and worrying answers! Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." -- from Nation, by Terry Pratchett

Email me
NogDog is offline   Reply With Quote
Old 12-28-2008, 04:00 PM   #9
Thimbletack
Junior Member
 
Join Date: Dec 2008
Posts: 4
Unhappy Me Too

I am also getting this error. I've looked around for a while and haven't figured out how to fix it, but my errors are for all of my PHP code. This is kind of weird. So here is my code.
Code:
<?php session_start();?><html><?php
if(isset($_SESSION['loggedinuser'])) 
echo "You're logged in! Click <a href='profile.php'>Here</a> to view your profile";
else {
echo "You're not logged in. Do you want to <a href='login.php'>log in</a> or <a href='register.html'>register</a>?";
}
?>
<head>
    <title>UrComics.com Your comics, Your votes!</title>
    <link rel="stylesheet" type="text/css"
    href="style.css" />
</head>
<body>
<table border="1" cellspacing="0" cellpadding="1" align="left">
<tbody>
<tr>
<td>
<img alt="" src="./Images/UrComicsLogo1.gif" />
<br />
<h5 align="center">
<a href="index.php">Home</a>
<br />
<a href="UrPaper.php">Ur Paper</a>
<br />
<a href="inThePaper.php">In The Paper</a>
<br />
<a href="Help.html">Help/FAQ</a>
<br />
<a href="uploadcomic.php">Submit A Comic</a>
</h5>
</td>
</tr>
</tbody>
</table>
<table border="1" cellspacing="2" cellpadding="2" align="center" width="75%">
<tbody>
<tr>
<td>
<h1 align="center">Welcome to UrComics.com</h1>
</td>
<td>
<h3>Hits</h3>
<hr align="left" width="100%" "noshade" size="2" />
<h3>
<?php
$filename = "counter.txt" ;
$fd = fopen ($filename , "r") or die ("Can't open $filename") ;
$fstring = fread ($fd , filesize ($filename)) ;
echo "$fstring" ;
fclose ($fd) ;
$fd = fopen ($filename , "w") or die ("Can't open $filename") ;
$fcounted = $fstring + 1 ;
$fout = fwrite ($fd , $fcounted) ;
fclose ($fd) ;
?>
</h3>
<br />
</td>
</tr>
<tr>
<td width="50%" class="red">
<h3 align="center">Announcements</h3>
<hr align="left" width="100%" "noshade" class="red"  size="2" />
<br />
<p>Welcome to urcomics.com where you can post your own comics and see what others think of your idea.
Just create an account to get started!</p>
<a href="register.html">Click Here to Get Started!</a>
</td>
<td width="50%" class="blue">
<h3 align="center">Comics Of The Week</h3>
<hr align="left" width="100%" "noshade" class="blue" size="2" />
<br />
<p>Once I figure out the php i am gonna put the top rated comics of the week on here.</p>
</td>
</tr>
<tr>
<td class="green" align="left">
<h3 align="center">Login</h3>
<hr align="left" width="100%" "noshade" class="green" size="2" />
<br />
<?php
if(isset($_SESSION['loggedinuser'])) {
?>
You're currently logged in, do you want to <a href="logout.php">Log out</a>?
<?php 
} else { 
?>
<p>Put in your username and password to login.</p>
<br />
<form name="authenticate" method="post" action="authenticate.php">
Username:<input name="username" type="text" size="20">
<br />
Password:<input name="password" type="password" size="20" />
<br />
<input type="submit" name="submit" value="submit"/>
<input type="reset" name="clear" value="reset"/>
</form>
<?php
}
?>
</td>
</tr>
</tbody>
</table>
</body>
</html>
So I am getting these errors at the top of my page.
Code:
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\file\secret\index.php:1) in C:\anotherfile\file\index.php on line 1

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\files\morefiles\index.php:1) in C:\morefiles\stuff\index.php on line 1
NOTE:That is not really the file location.

But it is weird because the code was working then it just stopped working. and none of my php works so I don't know if this is a problem in my php.ini file or what. Anyone know how to fix this?
Thimbletack is offline   Reply With Quote
Old 12-28-2008, 07:00 PM   #10
Weedpacket
Custom User Title™
 
Weedpacket's Avatar
 
Join Date: Aug 2002
Location: Rapid Offensive Unit "Foreign Object Damage"
Posts: 20,031
Some editors, when they save a file as UTF-8, stick extra characters into the file at the very beginning (the format doesn't require it, but they do it anyway). Then refuse to show you those characters when they display the file. To see if your editor is one of these, try saving an empty file and seeing how many bytes it takes up. If it's more then zero (probably three), then you'll have to tell your editor to stop messing around (and how you do that depends on the editor).
__________________
THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER

Last edited by Weedpacket; 12-28-2008 at 07:08 PM.
Weedpacket is offline   Reply With Quote
Old 12-28-2008, 08:57 PM   #11
Thimbletack
Junior Member
 
Join Date: Dec 2008
Posts: 4
I don't Think so

I don't think so... I am using Microsoft Visual web studio and it doesn't do that because when I wrote this code I wrote it in notepad so It shouldn't be that that is happening.

This is really strange

Thimbletack
Thimbletack is offline   Reply With Quote
Old 12-28-2008, 09:51 PM   #12
NogDog
High Energy Magic Dept.
 
NogDog's Avatar
 
Join Date: Aug 2006
Location: Ankh-Morpork
Posts: 12,638
The error message is specifically telling you that something on line 1 of the script generated output prior to the session_start() call. Either there is something before the opening <?php tag (a space, a newline, a unicode BOM character), or the only other thing I can think of would be if your PHP configuration has an auto_prepend_file specified and that file is generating output, although in that case I think the error message would point to that file instead (though I'm not 100% sure about that).

Therefore pending any further information, I suspect Weedpacket's suggestion is correct, that your editor is saving the file in UTF-8 with a BOM (byte order mark) at the start of the file. Either see if there is an option with your editor to save it as plain ASCII or as UTF-8 without the BOM, or else try another editor that can.
__________________
"That's what the gods are! An answer that will do! Because there's food to be caught and babies to be born and life to be lived and so there is not time for big, complicated, and worrying answers! Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." -- from Nation, by Terry Pratchett

Email me
NogDog is offline   Reply With Quote
Old 12-29-2008, 04:30 AM   #13
Weedpacket
Custom User Title™
 
Weedpacket's Avatar
 
Join Date: Aug 2002
Location: Rapid Offensive Unit "Foreign Object Damage"
Posts: 20,031
Quote:
Originally Posted by Thimbletack
I wrote this code I wrote it in notepad
Yes, Notepad is one of those editors that does this (unless you save the file with an ANSI character set). Visual Studio also provides the option, but if I recall, for HTML files it defaults to Latin-1 and you have to tell it to use UTF-8 (with BOM).
__________________
THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
Weedpacket is offline   Reply With Quote
Old 12-29-2008, 11:45 AM   #14
Thimbletack
Junior Member
 
Join Date: Dec 2008
Posts: 4
I think you're right.

Ok so I saved a blank file and it put 3 bytes and said 1 kb so I do think you are right. So I don't think notepad does that so I am probably going to edit it in there. But is there some way to change the code to have nothing in front of it? because in notepad I see nothing so do you guys think the best way to do this would to open a new notepad file and copy the code down?

Thanks for your help,

Thimbletack
Thimbletack is offline   Reply With Quote
Old 12-29-2008, 11:59 AM   #15
Thimbletack
Junior Member
 
Join Date: Dec 2008
Posts: 4
Cool THANKS!

Sorry for double posting but I just wanted to say thanks for helping me out. I rewrote the code in notepad and it worked! So I just gotta rewrite each page w/ php on it... But anyway thanks again.


Thimbletack
Thimbletack is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Forum Jump


All times are GMT -4. The time now is 02:08 AM.








Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.