Session Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
tmaiden
Forum Commoner
Posts: 64
Joined: Fri Feb 24, 2006 3:15 pm
Location: Philadelphia
Contact:

Session Problem

Post by tmaiden »

I am trying to create templates for my site; so, it doesnt have to keep using hardcoded html/php.

My news.php and index.php are setup like so: (meaning the use of includes)

Code: Select all

<?php

	include('config.php');
		include 'templates/header.php';
			include 'templates/navigation/toptabs.php';		
?>
			<div id="container">
<?
				include 'templates/forms/logon.php';
				include 'templates/logo.php';
				include 'templates/navigation/navitabs.php';
				include 'templates/description.php';
?>
				<div id="main">
					</p>
					<h3>Thursday, August 31th 2006:</h3>
					<p>
							More HTML -> PHP migration.
					</p>
					<br>
<?
					include 'templates/pagead.php';
?>
				</div>
<?

				include 'templates/sidebar.php';
    			include 'templates/footer.php';
?>
Notice the include 'templates/forms/logon.php'; on about the 5th line. This loads a form which I am trying to use to allow the user to logon.
Here is the code for templates/forms/logon.php

Code: Select all

<?php
	include('config.php');

	if(isset($_POST['username'])){
		//Connect to database
		mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
		mysql_select_db($dbname) or die(mysql_error());

		$username = $_POST['username'];
		$password = md5($_POST['password']);

		$query = "SELECT * FROM ff_users WHERE username = '" . $username . "' and password = '" . $password . "'";
		$result = mysql_query($query);
		if (! mysql_num_rows($result)){
			echo "<form name=\"login\" method=\"post\" action=\"" . substr($_SERVER['REQUEST_URI'], 1) . "\">
			     <div id=\"sideform\">
			     <input type=\"text\" name=\"username\" size=\"15\" class=\"\" maxLength=\"20\">
			     <input type=\"password\" name=\"password\" size=\"15\" class=\"\" maxLength=\"20\">
			     <input type=\"submit\" name=\"submit\" value=\"Logon\">
			     </div>
			     </form><br><b>Invalid Username or Password</b>";
		}
		else{
                                                session_start(); 
			$_SESSION['username'] = $username;					
		}
	}
	if(isset($_SESSION['username'])){
		echo "<div id=\"sideform\">Welcome, <b>" . $_SESSION['username'] . "</b></div></form>";
	}
	else{
		echo "<form name=\"login\" method=\"post\" action=\"" . substr($_SERVER['REQUEST_URI'], 1) . "\">
		<div id=\"sideform\">
		<input type=\"text\" name=\"username\" size=\"15\" class=\"\" maxLength=\"20\">
		<input type=\"password\" name=\"password\" size=\"15\" class=\"\" maxLength=\"20\">
		<input type=\"submit\" name=\"submit\" value=\"Logon\">
		</div>
		</form>";
	}
?>
I'm now getting this error when viewing the news.php
Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/content/t/i/m/timothytmaiden/html/templates/header.php:46) in /home/content/t/i/m/timothytmaiden/html/templates/forms/logon.php on line 24

Any clue to how I can resolve this?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

The session cache delimiter is a http header. http headers must be sent to the client before any output via echo,print etc or anything outside a <?php ?> block.
User avatar
tmaiden
Forum Commoner
Posts: 64
Joined: Fri Feb 24, 2006 3:15 pm
Location: Philadelphia
Contact:

Post by tmaiden »

What if I echo stuff out in my header?

Code: Select all

<php
     ....
	echo "	<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">
			<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">
				<head>
					<meta name=vs_targetSchema content=\"http://schemas.microsoft.com/intellisense/ie5\">
					<title>" . $page_title . "</title>
					<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">
					<meta name=\"description\" content=\"" . $page_description . "\">
					<meta name=\"keywords\" content=\"" . $page_keywords . "\">
					<meta name=\"author\" content=\"" . $page_author . "\">
					<link rel=\"stylesheet\" type=\"text/css\" href=\"andreas02.css\" media=\"screen\" title=\"andreas02 (screen)\" />
					<link rel=\"stylesheet\" type=\"text/css\" href=\"print.css\" media=\"print\" />
				</head>
				<body><a name=\"top\"></a> \n";
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

http doesn't care about what you echo - wether it's the header of a html document or an jpeg image doesn't matter.
Any output sent to the client (and that includes everything echo'd) makes it impossible to sent http header afterwards.
User avatar
tmaiden
Forum Commoner
Posts: 64
Joined: Fri Feb 24, 2006 3:15 pm
Location: Philadelphia
Contact:

Post by tmaiden »

Does that mean that my problem is because something is being sent prior to the header information?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Yes, and php tells you exactly where the (first) output occured.
output started at /home/content/t/i/m/timothytmaiden/html/templates/header.php:46
46 is the line number

At the very least there is this line
<div id="container">
outside a php block. It is sent as-is to the client before logon.php is included.
User avatar
tmaiden
Forum Commoner
Posts: 64
Joined: Fri Feb 24, 2006 3:15 pm
Location: Philadelphia
Contact:

Post by tmaiden »

I am so lost with what is going on here.

Nor if I need session_start() and where it should be placed.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Code: Select all

session_start();
Should be at the top of the page, before anything is outputted.

Basicly...

Code: Select all

<?php
        sesson_start(); // THIS

        include('config.php');
                include 'templates/header.php';
                        include 'templates/navigation/toptabs.php';          
?>
                        <div id="container">
<?
                                include 'templates/forms/logon.php';
                                include 'templates/logo.php';
                                include 'templates/navigation/navitabs.php';
                                include 'templates/description.php';
?>
                                <div id="main">
                                        </p>
                                        <h3>Thursday, August 31th 2006:</h3>
                                        <p>
                                                        More HTML -> PHP migration.
                                        </p>
                                        <br>
<?
                                        include 'templates/pagead.php';
?>
                                </div>
<?

                                include 'templates/sidebar.php';
                include 'templates/footer.php';
?>
But, your getting Errors because your also trying to include files, after you have text outputted, the "<div id="container">".

Which it does not allow, so i sugest eather having "<div id="container">" printed out with one of the includes, or change up the way you have it.

Hope this helps out with your confusion a bit.

-NSF
User avatar
tmaiden
Forum Commoner
Posts: 64
Joined: Fri Feb 24, 2006 3:15 pm
Location: Philadelphia
Contact:

Post by tmaiden »

It did, thanks.
Post Reply