Page 1 of 1

Session Problem

Posted: Sat Sep 02, 2006 2:41 pm
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?

Posted: Sat Sep 02, 2006 3:09 pm
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.

Posted: Sat Sep 02, 2006 3:43 pm
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";
?>

Posted: Sat Sep 02, 2006 3:53 pm
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.

Posted: Sat Sep 02, 2006 5:00 pm
by tmaiden
Does that mean that my problem is because something is being sent prior to the header information?

Posted: Sat Sep 02, 2006 5:17 pm
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.

Posted: Sat Sep 02, 2006 7:53 pm
by tmaiden
I am so lost with what is going on here.

Nor if I need session_start() and where it should be placed.

Posted: Sat Sep 02, 2006 8:02 pm
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

Posted: Sat Sep 02, 2006 8:43 pm
by tmaiden
It did, thanks.