View Full Version : session/header problem...

06-05-2005, 06:54 PM
I've got the following script that validates a sign-in page for a PHP-driven chat room, then forwards the user to the chat room if everything validates OK:


//file authenticate.php

include "connect.php";

if (isset($_POST['submit'])) // name of submit button
$query = "select * from ch_chatters where chatter='$chatter' and password='$password'";
$result = mysql_query($query) or die("not queried") ;

$isAuth = false; //set to false originally

while($row = mysql_fetch_array($result))


print "

<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>

<html lang=en>


<link rel='stylesheet' href='style.css' type='text/css'>

<title>Media Moguls - Virtual Classroom</title>

<meta name = 'description' content = 'Media Moguls provides a seamless integration of media design and current technology. We are an agency that offers copy writing, web page development, computer hardware sales, and print design at competitive prices.'>
<meta name = 'keywords' content = 'business writing, copy writing, web copy writing, web site copy writing, ad copy writing, professional copy writing, write copy, advertisement copy writing, business writing, copy writing, web copy writing, web site copy writing, ad copy writing, professional copy writing, write copy, advertisement copy writing'>

<script language='JavaScript' src='http://www.mediamogulsweb.com/js_menu2.js'></script>
<script language='JavaScript' src='http://www.mediamogulsweb.com/mm_menu.js'></script>

<body bgcolor='#D1C9BE' background='http://www.mediamogulsweb.com/gradient_back_main.jpg' style='background-repeat: repeat-x; margin-top: 0'>

<center><table border='0' cellpadding='0' cellspacing='0' width='800'><tr><td>

<script language='JavaScript1.2'>mmLoadMenus();</script>

<!-- toolbar contents -->

<table border='0' cellpadding='0' cellspacing='0' width='800'>
<td><img src='http://www.mediamogulsweb.com/top_image.jpg'></td>
<table border='0' cellpadding='0' cellspacing='0' style='margin-top: 0'>
<td><img src='http://www.mediamogulsweb.com/top_image_left.jpg'></td>
<td><a href='javascript:;' onMouseOver='MM_showMenu(window.mm_menu_0927223844_0,0,23,null,'image1')' onMouseOut='MM_startTimeout();'><img src='http://www.mediamogulsweb.com/home.jpg' name='image1' border='0' id='image1'></a></td>
<td><a href='javascript:;' onMouseOver='MM_showMenu(window.mm_menu_0927224326_0,0,23,null,'image2')' onMouseOut='MM_startTimeout();'><img src='http://www.mediamogulsweb.com/products.jpg' name='image2' border='0' id='image2'></a></td>
<td><a href='javascript:;' onMouseOver='MM_showMenu(window.mm_menu_0927224655_0,0,23,null,'image3')' onMouseOut='MM_startTimeout();'><img src='http://www.mediamogulsweb.com/services.jpg' name='image3' border='0' id='image3'></a></td>
<td><a href='javascript:;' onMouseOver='MM_showMenu(window.mm_menu_0927224907_0,0,23,null,'image4')' onMouseOut='MM_startTimeout();'><img src='http://www.mediamogulsweb.com/news.jpg' name='image4' border='0' id='image4'></a></td>
<td><a href='javascript:;' onMouseOver='MM_showMenu(window.mm_menu_0927230259_0,0,23,null,'image5')' onMouseOut='MM_startTimeout();'><img src='http://www.mediamogulsweb.com/contact.jpg' name='image5' border='0' id='image5'></a></td>
<td><img src='http://www.mediamogulsweb.com/top_image_right.jpg'></td>
<td><img src='http://www.mediamogulsweb.com/top_image_bottom.jpg'></td>

<!-- end toolbar contents -->

<h3><center>The Virtual Classroom</center></h3>


$check="SELECT * from ch_online where sessionname='$chatter'";
$check2=mysql_query($check) or die("2");
$s="INSERT into ch_online (sessionname, time) values ('$chatter','$day' )";
$s2=mysql_query($s) or die("not queried");

header("location: index.php" );

print "<center><div style='background-color:#ffffff;padding:15px;width:50%;border:outset'><span style='color:#FF0000'>Warning:</span> Username or password entered incorrectly.<br><br><i>Please use your browser's \"Back\" button to return to the form and fix any errors.</i></div></center>


<div style='width:112px;height:41px;clear:both;margin:auto'><img src='http://www.mediamogulsweb.com/logo2.jpg' alt=''></div>


<table border='0' cellpadding='0' cellspacing='0' width='472'><tr>
<td width='20'></td>
<td style='background-image:url(http://www.mediamogulsweb.com/links_gradient2.jpg);width:432px;height:25px'>


<A class=b HREF='http://www.mediamogulsweb.com/index.shtml'>Home</A> | <A class=b HREF='http://www.mediamogulsweb.com/catalog.shtml'>Catalog</A> | <A class=b HREF='http://www.mediamogulsweb.com/quote.shtml'>Quote</A> | <A class=b HREF='http://www.mediamogulsweb.com/industry_news.php'>News</A> | <A class=b HREF='http://www.mediamogulsweb.com/contact.shtml'>Contact</A>



<td width='20'></td>

<br><center><font size='1'>Media Moguls &copy;2005. All rights reserved.</font></center>





I get the following errors when a correct username and password is submitted on the sign-in page:

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/httpd/vhosts/mediamogulsweb.com/httpdocs/chat_script/authenticate.php:2) in /home/httpd/vhosts/mediamogulsweb.com/httpdocs/chat_script/authenticate.php on line 19

Warning: Cannot modify header information - headers already sent by (output started at /home/httpd/vhosts/mediamogulsweb.com/httpdocs/chat_script/authenticate.php:2) in /home/httpd/vhosts/mediamogulsweb.com/httpdocs/chat_script/authenticate.php on line 103

I've fiddled around with my session and header statements, but I can't seem to get rid of the error messages. Instead of seeing those error messages, I should be forwarded to the chat room (index.php).

If you want to see the page and error messages for yourself, go to the following page: http://www.mediamogulsweb.com/chat_script/login.php. Type in "tester" for the username AND the password, and you can view the resulting chaos.

Thanks in advance for any help with this. :)

06-05-2005, 08:05 PM
You're printing out a big chunk of text to the browser and then trying to do a header redirect - that will always fail. If you send anything to the browser, even just a single blank space accidentally typed before the opening <?php tag then you can't send headers as they are already sent when the browser output is sent.

06-05-2005, 08:16 PM
So the session AND the header info have to appear before anything gets sent to the browser -- in other words, before my HTML?

Edit: I moved the browser output to after my header statement, and that didn't work?

06-05-2005, 09:20 PM

06-05-2005, 11:00 PM
thanks marek....thanks both of you

I deleted the lines before the <? and after the ?> and the errors went away....now I know about that (it's hard to believe it was that simple ;) )