Page 1 of 1

session_start() causing errors

Posted: Tue Jan 28, 2014 4:15 pm
by Bobarino
I have code for a log in/register page that I know works (worked on an XAMPP server I was using to make it). Now that I have converted to my GoDaddy server and database, I get errors that look like this:
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/content/02/8573202/html/fakehome.php:4) in /home/content/02/8573202/html/Core.php on line 3

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/content/02/8573202/html/fakehome.php:4) in /home/content/02/8573202/html/Core.php on line 3

I did everything I know to do (delete white spaces and make sure it is UTF8 without BOM), but am still getting the errors. This is my first time really working at a somewhat higher level with PHP and I would love some help. As far as output goes, I know that there can't be any before the session_start() code, but does that include all pages in my website? My other pages such as index.php and fakehome.php have output, but since they are completely different files from the Core.php (page where I use session() start), I don't know whether or not that would matter. Here is the code, and thanks so much for anyone who could give me some insight.

Code: Select all

//Core.php
<?php
ob_start();
session_start();
$current_file = $_SERVER['SCRIPT_NAME'];
$http_referer = $_SERVER['HTTP_REFERER'];

function loggedin() {
	if(isset($_SESSION['user_id']) && !empty($_SESSION['user_id'])) {
		return true;
	} else {
		return false;
	}
}

function getUserField($field) {
	$query = "SELECT `$field` FROM `users` WHERE `id`='".$_SESSION['user_id']."'";
	if($query_run = mysql_query($query)) {
		if ($query_result = mysql_result($query_run, 0, $field)) {
			return $query_result;
		}
	}
}	
?>
and

Code: Select all

//fakehome.php -- page that includes Core and is where I am getting the error.
<html><head>
 </head>
	<body style="" >
<?php
require 'Connect.php';
require 'Core.php';

if(loggedin()) {
	$firstname = getUserField('firstname');
	$surname = getUserField('surname');
	echo 'You have successfully logged in, '.$firstname.' '.$surname.'. <a href="LogOut.php">Log Out</a><br>';
} else {
	include 'LogInForm.php';
}
?>
//some html code
	</body></html>

Re: session_start() causing errors

Posted: Tue Jan 28, 2014 5:10 pm
by pickle
The one good thing about the "session already started" error is that it's fairly verbose. The error told you where the output started - in fakehome.php.

Output is any output. Looking at fakehome.php, you're outputing some html text before calling loggedin(). As soon as that text gets output, headers get sent. You need to re-organize your page so that absolutely nothing gets output before session_start() gets called.

Re: session_start() causing errors

Posted: Wed Jan 29, 2014 9:34 pm
by Bobarino
pickle wrote:The one good thing about the "session already started" error is that it's fairly verbose. The error told you where the output started - in fakehome.php.

Output is any output. Looking at fakehome.php, you're outputing some html text before calling loggedin(). As soon as that text gets output, headers get sent. You need to re-organize your page so that absolutely nothing gets output before session_start() gets called.
Thanks so much, got it all working now! I figured this was the case, but my php/html editor deleted everything before the initial html tags, so I thought that I was doing something else wrong. Got a new editor and its all working smoothly.