Web Hosting Talk







View Full Version : Cannot send session cookie - headers already sent


333halfevil
12-31-2009, 09:36 PM
Hi everyone. I am having trouble with this php. Any help is appreciated. Thanks :)

I keep getting these errors:
[30-Dec-2009 23:23:54] PHP Warning: session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cookie - headers already sent by (output started at /home/roguedat/public_html/roguesubmit.php:6) in /home/roguedat/public_html/roguesubmit.php on line 67
[30-Dec-2009 23:23:54] PHP Warning: session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cache limiter - headers already sent (output started at /home/roguedat/public_html/roguesubmit.php:6) in /home/roguedat/public_html/roguesubmit.php on line 67
[30-Dec-2009 23:23:54] PHP Parse error: syntax error, unexpected '?', expecting ']' in /home/roguedat/public_html/fns.php on line 39

Here is a the code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>RogueDatabase.net | A database for rogue software</title>
<link href="style.css" rel="stylesheet" type="text/css"/>
<?php
//start session
session_start();

// Include all the output functions
require_once('fns.php');


$_SESSION['myForm'] = $_POST;

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"">

<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <div id="uploadform"><?php
// contact form
if (isset($_POST['submitted']) && ('true' == $_POST['submitted'])) {
// checks if the form is submitted and then processes it
process_form();

} else {
// else prints the form
print_form();
}


?></div>
</body>
</html>
<?php session_destroy(); //unset session data ?></div>
<center>
</body>
</html>


<?php
//start session
session_start();





// prints form
function print_form(){
?>

<p><span class="required">*</span> Required fields</p>
<form method="post" action="<?php echo $_SERVER[’PHP_SELF’];?>" id="uploadform" enctype="multipart/form-data">
<p><label for="namefrom">Name <span class="required">*</span></label>
<input name="namefrom" id="namefrom" type="text" class="field" value="<?= $_SESSION['myForm']['namefrom']; ?>" tabindex="1"/></p>


<p><label for="emailfrom">Email <span class="required">*</span></label>
<input name="emailfrom" id="emailfrom" type="text" class="field" value="<?= $_SESSION['myForm']['emailfrom']; ?>" tabindex="3"/></p>


<p><label for="attachment">File Upload<br />(1 file only, max file size 10mb. Allowed file formats are .zip)</label>
<input name="attachment" id="attachment" type="file" tabindex="7">

<p><input type="submit" name="submit" id="submit" value="Send Email!" tabindex="8"/></p>
<p><input type="hidden" name="submitted" value="true" /></p>
</form>
<?php
}

// enquiry form validation

function process_form() {
// Read POST request params into global vars
// FILL IN YOUR EMAIL
$to = "Email@email.com";

$namefrom = trim($_POST['namefrom']);

$emailfrom = trim($_POST['emailfrom']);


// Allowed file types. add file extensions WITHOUT the dot.
$allowtypes=array("zip");

// Require a file to be attached: false = Do not allow attachments true = allow only 1 file to be attached
$requirefile="true";

// Maximum file size for attachments in KB NOT Bytes for simplicity. MAKE SURE your php.ini can handel it,
// post_max_size, upload_max_filesize, file_uploads, max_execution_time!
// 2048kb = 2MB, 1024kb = 1MB, 512kb = 1/2MB etc..
$max_file_size="10024kb";

// Thank you message
$thanksmessage="Your email has been sent, thank you for contributing to the fight against malware.";

$errors = array(); //Initialize error array

//checks for a name
if (empty($_POST['namefrom']) ) {
$errors[]='You forgot to enter your name';
}

//checks for an email
if (empty($_POST['emailfrom']) ) {
$errors[]='You forgot to enter your email';
} else {

if (!eregi ('^[[:alnum:]][a-z0-9_\.\-]*@[a-z0-9\.\-]+\.[a-z]{2,4}$', stripslashes(trim($_POST['emailfrom'])))) {
$errors[]='Please enter a valid email address';
} // if eregi
} // if empty email



// checks for required file
if($requirefile=="true") {
if($_FILES['attachment']['error']==4) {
$errors[]='You forgot to attach a file';
}
}

//checks attachment file
// checks that we have a file
if((!empty($_FILES["attachment"])) && ($_FILES['attachment']['error'] == 0)) {
// basename -- Returns filename component of path
$filename = basename($_FILES['attachment']['name']);
$ext = substr($filename, strrpos($filename, '.') + 1);
$filesize=$_FILES['attachment']['size'];
$max_bytes=$max_file_size*1024;

//Check if the file type uploaded is a valid file type.
if (!in_array($ext, $allowtypes)) {
$errors[]="Invalid extension for your file: <strong>".$filename."</strong>";

// check the size of each file
} elseif($filesize > $max_bytes) {
$errors[]= "Your file: <strong>".$filename."</strong> is to big. Max file size is ".$max_file_size."kb.";
}

} // if !empty FILES

if (empty($errors)) { //If everything is OK

// send an email
// Obtain file upload vars
$fileatt = $_FILES['attachment']['tmp_name'];
$fileatt_type = $_FILES['attachment']['type'];
$fileatt_name = $_FILES['attachment']['name'];

// Headers
$headers = "From: $emailfrom";

// create a boundary string. It must be unique
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";

// Add a multipart boundary above the plain message
$message ="This is a multi-part message in MIME format.\n\n";
$message.="--{$mime_boundary}\n";
$message.="Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$message.="Content-Transfer-Encoding: 7bit\n\n";
$message.="From: ".$namefrom."\n";


if (is_uploaded_file($fileatt)) {
// Read the file to be attached ('rb' = read binary)
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);

// Base64 encode the file data
$data = chunk_split(base64_encode($data));

// Add file attachment to the message
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
}


// Send the completed message

$envs = array("HTTP_USER_AGENT", "REMOTE_ADDR", "REMOTE_HOST");
foreach ($envs as $env)
$message .= "$env: $_SERVER[$env]\n";

if(!mail($to,$subject,$message,$headers)) {
exit("Mail could not be sent. Sorry! An error has occurred, please report this to the website administrator.\n");
} else {
echo '<div id="formfeedback"><h3>Thank You!</h3><p>'. $thanksmessage .'</p></div>';
unset($_SESSION['myForm']);
print_form();

} // end of if !mail

} else { //report the errors
echo '<div id="formfeedback"><h3>Error!</h3><p>The following error(s) has occurred:<br />';
foreach ($errors as $msg) { //prints each error
echo " - $msg<br />\n";
} // end of foreach
echo '</p><p>Please try again</p></div>';
print_form();
} //end of if(empty($errors))

} // end of process_form()
?>

someone150
01-01-2010, 07:35 AM
session_start(); must be before any other output (html).

foobic
01-01-2010, 07:38 AM
Cannot send session cookie - headers already sent by (output started at /home/roguedat/public_html/roguesubmit.php:6) in /home/roguedat/public_html/roguesubmit.php on line 67You need to finish setting up your session data before anything goes out to the browser. So put all your form processing code in before that initial "<!DOCTYPE..."

PHP Parse error: syntax error, unexpected '?', expecting ']' in /home/roguedat/public_html/fns.php on line 39What it says: look for the missing "]" / unquoted string / badly formed regex etc. on line 39 of that file (or if you don't find it on that line, look back a line or two - sometimes a syntax error on a previous line isn't caught immediately).

Edit: Would you believe it: he waits 10 hours for an answer and then two come at once! ;)

someone150
01-01-2010, 08:03 AM
You need to finish setting up your session data before anything goes out to the browser. So put all your form processing code in before that initial "<!DOCTYPE...
Actually, you just need to put session_start(); before other output. Session data can be set even after output, because they are on server side.

Another option is to use ob_start(); before session_start(); which allows you to set cookies, use header("Location: /"); anywhere in code.

333halfevil
01-01-2010, 10:08 AM
Thank you for your help :) What a co-incidence exactly at the same time haha :P

I'm still having trouble with this though. After looking through it I could find nothing missing?

?>

<p><span class="required">*</span> Required fields</p>
Line 39 - <form method="post" action="<?php echo $_SERVER[’PHP_SELF’];?>" id="uploadform" enctype="multipart/form-data">
<p><label for="namefrom">Name <span class="required">*</span></label>
<input name="namefrom" id="namefrom" type="text" class="field" value="<?= $_SESSION['myForm']['namefrom']; ?>" tabindex="1"/></p>


<p><label for="emailfrom">Email <span class="required">*</span></label>
<input name="emailfrom" id="emailfrom" type="text" class="field" value="<?= $_SESSION['myForm']['emailfrom']; ?>" tabindex="3"/></p>


<p><label for="attachment">File Upload<br />(1 file only, max file size 10mb. Allowed file formats are .zip)</label>
<input name="attachment" id="attachment" type="file" tabindex="7">

<p><input type="submit" name="submit" id="submit" value="Send Email!" tabindex="8"/></p>
<p><input type="hidden" name="submitted" value="true" /></p>
</form>
<?php

Neseema M M
01-02-2010, 01:05 AM
I too cannot find any error there.


<form method="post" action="<?php echo $_SERVER[’PHP_SELF’];?>" id="uploadform" enctype="multipart/form-data">


Anyway change the quotes in the $_SERVER array and put it correctly so that PHP_SELF got printed in red color ;) . I think it may cause the problem.

333halfevil
01-02-2010, 02:46 AM
Thank you very much. It is now fixed :)