Jump to content

Change Where the Error Message Displays


jponte

Recommended Posts

Hi,

I have the following code at the top of the page due to the errors I was getting with the Header () funtion. Now my errors display in a new white page and not within the formated page. How can I change were to display the DIE error?

 

<?php 
ob_start();

// Connects to your Database
mysql_connect("localhost", "rt", "es") or die(mysql_error());
mysql_select_db("rma_portal") or die(mysql_error());

//Checks if there is a login cookie
if(isset($_COOKIE['ID_my_site']))

//if there is, it logs you in and directes you to the members page
{
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$check = mysql_query("SELECT * FROM cust_info WHERE Cust_Username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{
	if ($pass != $info['Cust_Password'])
	{
	}
	else
	{
	ob_start();
	header("Location: form0.php");
	ob_end_flush();
	}
}
}

//if the login form is submitted
if (isset($_POST['submit']))
{

// makes sure they filled it in
if(!$_POST['username'] | !$_POST['pass']) {
die('You did not fill in a required field.');
}

// checks it against the database 
if (!get_magic_quotes_gpc()) {
$_POST['username'] = addslashes($_POST['username']);
}

$check = mysql_query("SELECT * FROM cust_info WHERE Cust_Username = '".$_POST['username']."'")or die(mysql_error());

//Gives error if user doesnt exist
$check2 = mysql_num_rows($check);
if ($check2 == 0) {
die('That user does not exist in our database.');
}
	while($info = mysql_fetch_array( $check ))
	{
	$_POST['pass'] = stripslashes($_POST['pass']);
	$info['Cust_Password'] = stripslashes($info['Cust_Password']);
	$_POST['pass'] = md5($_POST['pass']);

		//gives error if the password is wrong
		if ($_POST['pass'] != $info['Cust_Password']) {
		die('Incorrect password, please try again.');
		}
		else
		{
		// if login is ok then we add a cookie
		$_POST['username'] = stripslashes($_POST['username']);
		$hour = time() + 3600;
		setcookie(ID_my_site, $_POST['username'], $hour);
		setcookie(Key_my_site, $_POST['pass'], $hour);


		//Redirects to form page 
		header("Location: form0.php");

		//logged in, register the session..
		session_register("username");
		session_register("pass");
		}
	}
}
else
{
// if they are not logged in
?>

Link to comment
Share on other sites

Using the OR operator to get statements to execute when a function call fails does not address what action your code takes when there is an error, so using OR die(), OR trigger_error() is really only useful for debugging purposes.

 

To control what happens when an error occurs, you must use traditional logic. See the following post for some example code - http://www.phpfreaks.com/forums/index.php/topic,287244.msg1361847.html#msg1361847

 

Once you have traditional logic, you can do things like set a flag/variable to indicate that an error occurred. Typically an array is used to hold error messages and the array being empty is what you would test at some later point in your logic. If the array is empty, no errors. If the array is not empty, it contains the error messages and you can simply output them wherever you would like.

Link to comment
Share on other sites

From my experiences (though i've never used header() function) instead of using die() I will test the function's success using if() and use an else upon failure. Inside the else {} block, set the content string to include the error message.

 

Might not suit your needs though.

Example:

$content = '<html><head><title>$title</title></head><body>';
if ($dbConn = mysql_connect($host, $user, $pass))
{
   // connected successfully - continue into script
}
else
   $content .= 'Could not connect to database. Error given: '.mysql_error().'<br/>'."\n";

$content .= '</body></html>';
// output content to page
print($content);

 

Like i said, I've never used header() before so dont know how this method would work with it.

Link to comment
Share on other sites

I create an error_pg.php  or error_message.php page that it formated similar to other pages on my website (keeps same feel and look) but its main purpose is to echo out error messages.  Lets call that page- error_msg.php.  At the begining of the body of that page- define your messages.  For example;

$no_access = "<center>Sorry- could not connect to the Database</center>";
$no_file ="<center>Sorry that file could not be found</center>";

In the middle of that page I have the following; 

<? echo $message; ?>

 

now on the other page- lets do the testing and error reporting.  Try something like this;

 

if (what ever condition)
{
$message = $no_access;
include("error_msg.php");
exit:
}

 

In this- your code will run until it has an error.  The error is defined and then the error display page (error_msg.php)

is included and the message is displayed.  Works great.

 

Cheers-

 

Link to comment
Share on other sites

If it is returning a blank message- make sure to check that your variables are not miss-spelled. 

 

For instance- you define one message as

 

$first_msg = "whatever";

 

then in the if statement you refer to it as

 

$error = $first_message

 

-----in this instance the page will be called but be blank as it cannot echo $first_message as it is not defined.

 

Link to comment
Share on other sites

Hi,

The syntax is good:

 

index.php:

 

//Gives error if user doesnt exist
$check2 = mysql_num_rows($check);
if ($check2 == 0) {
//die('That user does not exist in our database.');
$no_file = "<center><br>Sorry that file could not be found</center>";
$message = $no_file;
include("errorpage.php");
exit;
}

 

errorpage.php:

 

<P><?php 
$no_access = "<center><br>Sorry could not connect to the Database</center>";
$no_file = "<center><br>Sorry that file could not be found</center>";

echo "Error: $message"; 
echo "<br>look for the error ";
?>

 

The page redirects but the value is not printed

 

Very strange. I tried changing the inlcude order in the index.php page but nothing.

 

Can you see the error?

 

JP

Link to comment
Share on other sites

Hi,

In addition....I have a form in the page were the errors are coming from. Whould this have an impact on my code?

 

     <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
        <table border="0" align="center">
          <tr>
            <td width="144" align="center"><p>Username:</p></td>
          </tr>
          <tr>
            <td><input type="text" name="username" maxlength="15"></td>
          </tr>
          <tr>
            <td align="center">Password:</td>
          </tr>
          <tr>
            <td><input type="password" name="pass" maxlength="15"></td>
          </tr>
          <tr>
            <td align="right"><center>
                <input type="submit" name="submit" value="Login">
   

 

Thanks again,

 

JP

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.