Forum Moderators: coopster

Message Too Old, No Replies

I can't figure out where I am going wrong?

internal server error

         

rodriguez1804

10:16 am on Aug 17, 2009 (gmt 0)

10+ Year Member



Hey guys,

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

Nutter

11:09 am on Aug 17, 2009 (gmt 0)

10+ Year Member



Don't know if this is the only issue, but

return 'false'

should be return false - without the quotes. You're returning a string with the letters f,a,l,s, & e; not the Boolean value false.

Do you have access to your server error logs? They're usually more descriptive of what the error is.

rodriguez1804

6:03 pm on Aug 17, 2009 (gmt 0)

10+ Year Member



Oops! Yeah, I had the return false & true without quotes: I just forgot to change it back after messing with it for a while. Anyways, here's what my error log says:

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().'&nbsp;&nbsp;
<a href="logout.php">Logout</a>

&nbsp;<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.

CyBerAliEn

6:23 pm on Aug 17, 2009 (gmt 0)

10+ Year Member



Can you elaborate what exactly line '13' and line '42' are? I put your code in a text editor and the line numbers on my end don't match with your "I think the error is happening here" line.

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();

rodriguez1804

7:14 pm on Aug 17, 2009 (gmt 0)

10+ Year Member



Ok, so line 13 is referring to this:


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().'&nbsp;&nbsp;
<a href="logout.php">Logout</a>

&nbsp;<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();
}

?>


Yeah, I also contacted hostgator about the 500 error and they weren't able to do anything to fix it. The thing that puzzles me is that I have other pages with similar code using multiple header requests (like checkLogin.php) and it works just fine? I just can't seem to use ob_start() on "protected.php" without getting the same error? I am thinking it has to do with either:
1.)the session
2.)headers
3.)or my wrong use of "return true & false" values in the "if($dbConn->isSessionSet() == (false)" statment

I don't know, what do you guys think?

rodriguez1804

9:00 pm on Aug 17, 2009 (gmt 0)

10+ Year Member



Ha, for some weird reason it's working now! Same code and everything, I just erased the problem lines and then rewrote them (all the same). Haha, all the brain cracking for nothing! Thanks though