Forum Moderators: coopster
I am having the biggest time trying to figure out where I am going wrong with the following code. I keep getting an internal 500 server error. Here's the code:
<?php
session_start();include_once "dbFunctions.php";
$dbConn = new dbConnection();
$webPage = new generateHTML();
//echo "testing"; *****IF I INSERT THIS IN HERE, IT WORKS, BUT WHENEVER I TAKE IT OUT, I GET AN INTERNAL 500 ERROR. I DON'T WANT TO ECHO SOMETHING THOUGH, SO THIS IS NOT A SOLUTION. *****
if($dbConn->isSessionSet() == (false)) /I THINK I AM SCREWING THIS PART UP
{
echo "Wrong Password";
require_once("index.html"); die();
}else if($dbConn->isSessionSet() == (true)){
if(isset($_POST['Post']))
{
$dbConn->connect();
//do whatever
and the function is simply
function isSessionSet()
{
if(isset($_SESSION['username'])){
return 'true';
}else{
return 'false';
}
}
I know the problem is with the if(isset parts of my code, just can't figure out what. If I am doing it wrong, please show me how. Thanks
PHP Warning: Cannot modify header information - headers already sent by (output started at /protected.php:13) in /protected.php on line 42
and here is the full source of protected.php.
[code]
<?php
session_start();
include_once "dbFunctions.php";
include_once ("generatePage.php");
$dbConn = new dbConnection();
$webPage = new generateHTML();
if($dbConn->isSessionSet() == (false))
{
echo "Wrong Password or Username.</div><br />";
require_once("index.html"); die();
}else if($dbConn->isSessionSet() == (true)){
if(isset($_POST['Post']))
{
$dbConn->connect();
$comment=stripslashes(mysql_real_escape_string($_POST['comment']));
$timeStamp = $_POST['date'];
if(empty($comment))
{
echo "Please enter your comment.</div><br />";
require_once("protected.php"); die();
}
$dbConn->insertComment($dbConn->viewSessionUsername(),$comment,$timeStamp);
$dbConn->closeConn();
header(''.$_SERVER['PHP_SELF'].'');
}
else if(!isset($_POST['Post']))
{
header("index.html"); ******SEEMS THAT THE ERROR IS HERE. IF I PUT ob_start() AT VERY TOP OF PAGE TO TURN OUTPUT BUFFERING ON, THEN I STILL GET THE SAME ERROR? ******
}
$webPage->addHeader('title');
$webPage->addLeftColumn('
<div id="userName">'.$dbConn->viewSessionUsername().'</div>
<div id="userPic">'.
$myProfilePic.'
</div>
<div id="picOptions"><a href="editUserPic.php">Edit/Delete</a></div>
<br /><br />
<div id="userContactInfo">
<table>
<tr>
<td><span class="smallTxt">Username:
<td>'.$dbConn->viewSessionUsername().'</td></td></tr>
<td><span class="smallTxt">First Name:</span></td>
<td>'.$dbConn->viewSessionFirstName().'</td></tr>
<td><span class="smallTxt">Last Name:
<td>'.$dbConn->viewSessionEmail().'</a></td></td></tr>
</tr></table>
</div>
');
$dbConn = new dbConnection();
$webPage->greetUser('
You are logged in as: '.$dbConn->viewSessionUsername().'
<a href="logout.php">Logout</a>
<a href=profile.php>Profile</a>'
, '');
$webPage->addContent('
<div align="left" id="mainContent">
<div id="postArea">
<h3>Make a Post</h3>
<table>
<tr>
<form name="userInfo" method="post" action="checkUserPost.php">
<td><textarea name="comment" cols="30" rows="10"/></textarea></td></tr>
<td><input type="hidden" name="date" value='.date("y-m-j,H:i:s a").' />
<tr><td><input class="submitBtn" type="submit" value="Post" name="Post"/>
</form></td></tr>
</table></div>
<div align="left" id="postContent">
'.$dbConn->retrieveComments().'
</div>
');
$webPage->addFooter(date('Y'), 'SiteSolutions Inc.');
echo $webPage->getPage();
}else{
echo "<div align='center' style='color:blue; font-weight:bold'>Wrong Password or Username. Try again or register.</div><br />";
require_once("index.html"); die();
}
?>
Thanks for any and all help.
What the error means though... is that you output something on line 13... and when you tried doing a header call on line 42, PHP got angry and stopped, cause you already output content.
My thoughts are... why are you getting a 500 error? Something more is happening! 500 error should only happen with a server issue; a simple header issue as described above would simply halt PHP and display an error.
What is happening in your "/index.html" file? In my text editor, line 13 contains:
require_once("index.html"); die();
require_once("index.html"); die(); and line 42 refers to this;
header("index.html"); Here's the process that takes place.
1.) index.html simply has a user login window that takes username and password and passes it on to "checkLogin.php". Simply html form, so error is not here.
2.) "checkLogin.php" checks for valid username & password and then redirects user to "protected.php".
Here is the checkLogin.php
<?php
ob_start();
session_start();
include_once ("dbFunctions.php");
header("Cache-control: private, no-cache");
header("Expires: Mon, 26 Jun 1997 05:00:00 GMT");
header("Pragma: no-cache");if(isset($_POST['Submit'])){
$dbConn = new dbConnection();
$dbConn->retrieveUser($_POST['username'], $_POST['password1']);
$dbConn->sessionRegister();
//if all is well to this point, send them to their unique page
header("location: protected.php");
}
else if(!isset($_POST['Submit']))
{
header("location: newUser.html");
}
else
{
echo "Wrong Username or Password";
header("location: index.html");
}
ob_end_flush();
?>
and here is the protected.php once again
<?php
session_start();include_once "dbFunctions.php";
include_once ("generatePage.php");
$dbConn = new dbConnection();
$webPage = new generateHTML();
if($dbConn->isSessionSet() == (false))
{
echo "Wrong Password or Username.</div><br />";
require_once("index.html"); die();
}else if($dbConn->isSessionSet() == (true)){
if(isset($_POST['Post']))
{
$dbConn->connect();
$comment=stripslashes(mysql_real_escape_string($_POST['comment']));
$timeStamp = $_POST['date'];
if(empty($comment))
{
echo "Please enter your comment.</div><br />";
require_once("protected.php"); die();
}
$dbConn->insertComment($dbConn->viewSessionUsername(),$comment,$timeStamp);
$dbConn->closeConn();
header(''.$_SERVER['PHP_SELF'].'');
}
else if(!isset($_POST['Post']))
{
header("index.html"); ******SEEMS THAT THE ERROR IS HERE. IF I PUT ob_start() AT VERY TOP OF PAGE TO TURN OUTPUT BUFFERING ON, THEN I STILL GET THE SAME ERROR? ******
}
$webPage->addHeader('title');
$webPage->addLeftColumn('
<div id="userName">'.$dbConn->viewSessionUsername().'</div>
<div id="userPic">'.
$myProfilePic.'
</div>
<div id="picOptions"><a href="editUserPic.php">Edit/Delete</a></div>
<br /><br />
<div id="userContactInfo">
<table>
<tr>
<td><span class="smallTxt">Username:
<td>'.$dbConn->viewSessionUsername().'</td></td></tr>
<td><span class="smallTxt">First Name:</span></td>
<td>'.$dbConn->viewSessionFirstName().'</td></tr>
<td><span class="smallTxt">Last Name:
<td>'.$dbConn->viewSessionEmail().'</a></td></td></tr>
</tr></table>
</div>
');
$dbConn = new dbConnection();
$webPage->greetUser('
You are logged in as: '.$dbConn->viewSessionUsername().'
<a href="logout.php">Logout</a>
<a href=profile.php>Profile</a>'
, '');
$webPage->addContent('
<div align="left" id="mainContent">
<div id="postArea">
<h3>Make a Post</h3>
<table>
<tr>
<form name="userInfo" method="post" action="checkUserPost.php">
<td><textarea name="comment" cols="30" rows="10"/></textarea></td></tr>
<td><input type="hidden" name="date" value='.date("y-m-j,H:i:s a").' />
<tr><td><input class="submitBtn" type="submit" value="Post" name="Post"/>
</form></td></tr>
</table></div>
<div align="left" id="postContent">
'.$dbConn->retrieveComments().'
</div>
');
$webPage->addFooter(date('Y'), 'SiteSolutions Inc.');
echo $webPage->getPage();
}else{
echo "<div align='center' style='color:blue; font-weight:bold'>Wrong Password or Username. Try again or register.</div><br />";
require_once("index.html"); die();
}
?>
I don't know, what do you guys think?