4 Replies - 722 Views - Last Post: 14 November 2012 - 09:14 AM Rate Topic: -----

#1 c9-adams  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 98
  • Joined: 12-December 11

php session_start(); error

Posted 14 November 2012 - 08:54 AM

Hi guys i am getting the following error:
An error occurred in script 'C:\xampp\htdocs\web-programming\modules\main.inc.php' on line 1:
session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\web-programming\modules\config.inc.php:90)
Date/Time: 11-14-2012 16:45:32
This is what I have tried to resolve this but with no avail.
<?php session_start();
if(!isset($_SESSION['username'])) {
echo"welcome guest!";
} else {
echo "<font color='red'>You are logged in as </font><font color='purple'>$_SESSION[username].</font><font color='orange'><a href='index2.php?p=logout'>  logout?</a> </font><br><font color='red'>Dont forget $_SESSION[username]. always feel free to contact us for support when needed most</font><br><br>";
}
?>


Any help would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: php session_start(); error

#2 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: php session_start(); error

Posted 14 November 2012 - 08:57 AM

Hello again,

'headers already sent' means that you have output something before the call to session_start();.

Just to be sure put a newline after <?php rather than a space. So like this...
<?php
session_start();


Make sure also that it is the first thing called, just to be sure no output is sent. If the code above is in a file being called by something elsewhere, the session_start() needs to move out and up to what calls it.
Was This Post Helpful? 0
  • +
  • -

#3 c9-adams  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 98
  • Joined: 12-December 11

Re: php session_start(); error

Posted 14 November 2012 - 09:04 AM

Still don't quite understand. I have moved session_start(); to a new line but still getting the error. I have looked through the config file but cant seem to find where it is calling the session_start();

This is the code for the config.inc file:
<?php # config.inc.php

/* 
 *	File name: config.inc.php
 *	Configuration file does the following things:
 *	- Has site settings in one location.
 *	- Stores URLs and URIs as constants.
 *	- Sets how errors will be handled.
 *
 *	Framework adopted from Larry Ullman http://www.dmcinsights.com
 */

# ******************** #
# ***** SETTINGS ***** #

// Errors are emailed here.
$contact_email = 'chad.1234@hotmail.co.uk'; 

// Determine whether we're working on a local server
// or on the real server:
if (stristr($_SERVER['HTTP_HOST'], 'local') || (substr($_SERVER['HTTP_HOST'], 0, 7) == '192.168')) {
	$local = TRUE;
} else {
	$local = FALSE;
}

// Determine location of files and the URL of the site:
// Allow for development on different servers.
if ($local) {

	// Always debug when running locally:
	$debug = TRUE;
	
	// Define the constants:
	define ('BASE_URI', '/path/to/html/folder/');
	define ('BASE_URL',	'http://localhost/directory/');
	define ('DB', '/path/to/mysql.inc.php');
	
} else {

	define ('BASE_URI', '/path/to/live/html/folder/');
	define ('BASE_URL',	'http://www.example.com/');
	define ('DB', '/path/to/live/mysql.inc.php');
	
}
	
/* 
 *	Most important setting...
 *	The $debug variable is used to set error management.
 *	To debug a specific page, add this to the index.php page:

if ($p == 'thismodule') $debug = TRUE;
require_once('./includes/config.inc.php');

 *	To debug the entire site, do

$debug = TRUE;

 *	before this next conditional.
 */

// Assume debugging is off. 
if (!isset($debug)) {
	$debug = FALSE;
}

# ***** SETTINGS ***** #
# ******************** #


# **************************** #
# ***** ERROR MANAGEMENT ***** #

// Create the error handler.
function my_error_handler ($e_number, $e_message, $e_file, $e_line, $e_vars) {

	global $debug, $contact_email;
	
	// Build the error message.
	$message = "An error occurred in script '$e_file' on line $e_line: \n<br />$e_message\n<br />";
	
	// Add the date and time.
	$message .= "Date/Time: " . date('n-j-Y H:i:s') . "\n<br />";
	
	// Append $e_vars to the $message.
	$message .= "<pre>" . print_r ($e_vars, 1) . "</pre>\n<br />";
	
	if ($debug) { // Show the error.
	
		echo '<p class="error">' . $message . '</p>';
		
	} else { 
	
		// Log the error:
		error_log ($message, 1, $contact_email); // Send email.
		
		// Only print an error message if the error isn't a notice or strict.
		if ( ($e_number != E_NOTICE) && ($e_number < 2048)) {
			echo '<p class="error">A system error occurred. We apologize for the inconvenience.</p>';
		}
		
	} // End of $debug IF.

} // End of my_error_handler() definition.

// Use my error handler:
set_error_handler ('my_error_handler');

# ***** ERROR MANAGEMENT ***** #
# **************************** #


Was This Post Helpful? 0
  • +
  • -

#4 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: php session_start(); error

Posted 14 November 2012 - 09:08 AM

Hello again,

It does not always mean that you are calling session_start(); before, it means you could be outputting anything at all! As soon as PHP get's the call to echo something, it starts sending that to the client (through your web server) while the rest of the script could still be running, this is one of the things that makes PHP fast.

So, to make sure things don't go wrong, certain features require you to have sent nothing through to the server/client, whether it be set by the header() function or simple echoing of text.

Put session_start(); as the FIRST thing in your entire application. Before anything.
Was This Post Helpful? 0
  • +
  • -

#5 c9-adams  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 98
  • Joined: 12-December 11

Re: php session_start(); error

Posted 14 November 2012 - 09:14 AM

I see, so I should put session_start(); at the start of the config.inc.php file. I tried that and it works. Thankyou for your help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1