By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,652 Members | 1,842 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,652 IT Pros & Developers. It's quick & easy.

interpreting error messages from include files...

P: n/a
hello,

i am new to PHP programming and wondered if u could help.

lets say i have the following files:

1. error.php: that is routed to for all errors. this page should
display proper and explanatory error messages to the user based on the
parameter provided.
2. errordef.php: has all the error codes and their explanations
"defined" as constants.

now, say if i have 3 different files viz. a.php, b.php and c.php, and
when any error occuring in either these files, i want to route to
error.php for detailed explanation. each a.php, b.php and c.php passes
error code to error.php, but error.php should interpret the code from
the definition file errordef.php!
how can i implement this? any quick help will be appreciated.

Jul 17 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
DH
r6****@gmail.com wrote:
hello,

i am new to PHP programming and wondered if u could help.

lets say i have the following files:

1. error.php: that is routed to for all errors. this page should
display proper and explanatory error messages to the user based on the
parameter provided.
2. errordef.php: has all the error codes and their explanations
"defined" as constants.

now, say if i have 3 different files viz. a.php, b.php and c.php, and
when any error occuring in either these files, i want to route to
error.php for detailed explanation. each a.php, b.php and c.php passes
error code to error.php, but error.php should interpret the code from
the definition file errordef.php!
how can i implement this? any quick help will be appreciated.


If the error occurs in a.php then

@header("Location: ./error.php?id=1");
exit;

And in error.php ...

$id = array_key_exists('id', $_GET) ?
strip_tags(stripslashes(trim($_GET['id']))) : '';

// HTML header here

switch($id)
{

Case 1:
echo '<p>Message 1</p>';
break;

Case 2:
echo '<p>Message 2</p>';
break;

default:
echo '<p>An unknown error occurred</p>';
break;

}; # End switch

// HTML footer
Jul 17 '05 #2

P: n/a
An unknown person wrote:
@header("Location: ./error.php?id=1");


The Location header needs a full URL.

header("Location: http://www.example.com/error.php?id=1");

Regards,
Matthias
Jul 17 '05 #3

P: n/a
r6****@gmail.com wrote:
lets say i have the following files:

1. error.php: that is routed to for all errors.
How is it routed?
2. errordef.php: has all the error codes and their explanations
"defined" as constants.
I'd isolate the explanations inside errordef.php alone. Although
defining constants might make the error treatment faster we hope that
there won't be that many errors as to make it important.
now, say if i have 3 different files viz. a.php, b.php and c.php, and
when any error occuring in either these files, i want to route to
error.php for detailed explanation. each a.php, b.php and c.php passes
error code to error.php, but error.php should interpret the code from
the definition file errordef.php!
how can i implement this? any quick help will be appreciated.


I will call the error code received in error.php as $err_code.

somewhere in error.php include the line

$description = interpret_code($err_code);

where "interpret_code" is a function defined in errordef.php that takes
an error code as an argument and returns a string with the detailed
explanation which you can then use to show the users.
Happy Coding :-)
--
Mail to my "From:" address is readable by all at http://www.dodgeit.com/
== ** ## !! ------------------------------------------------ !! ## ** ==
TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
may bypass my spam filter. If it does, I may reply from another address!
Jul 17 '05 #4

P: n/a
I route to the error.php exactly as mentioned by DH :

header(Location: error.php?err=INVALID_USER);

(BTW, what use is the '@' symbol here? not having the '@' works fine
for me)

I also tried a couple of things and got it working. Heres what I did:

As the error.php will be used for displaying all sorts of errors, I've
made it as a form with action = "GET" so I have $_GET['err'] storing
the error code.

so if i wanted to display the description for the error code
'INVALID_USER' (which has been defined in errordef.php), i simply used:

echo constant($_GET['err']);

It worked! But, I'm not sure if this is a right thing to do! What do
you reckon?

Jul 17 '05 #5

P: n/a
JAS
r6****@gmail.com wrote:
(BTW, what use is the '@' symbol here? not having the '@' works fine
for me)


I prevents an error message being displayed if one should happen. I
always use it with mysql, mail, cookie, and similar functions and trap
errors rather than just letting my app crap out displaying errors that
might compremize the application to the public.

J
Jul 17 '05 #6

P: n/a
On 31 Dec 2004 04:49:05 -0800, <r6****@gmail.com> wrote:
I route to the error.php exactly as mentioned by DH :

header(Location: error.php?err=INVALID_USER);
must be: header('Location:
'.$_SERVER['HTTP_HOST'].'/error.php?err=INVALID_USER');

(BTW, what use is the '@' symbol here? not having the '@' works fine
for me)
@ disables error reporting for certain command/expression.

echo constant($_GET['err']);


using unverified data from external source is a bit unwise.

maybe put errors in an array
$errors = array(
'INVALID_USER'=>'whatever',
'ETC'=>'etc',
);

and use fail-safe
echo isset($errors[$_GET['err']])?$errors[$_GET['err']]:'Unknown Error';
--
* html {redirect-to: url(http://browsehappy.pl);}
Jul 17 '05 #7

P: n/a
JAS wrote:
r6****@gmail.com wrote:
(BTW, what use is the '@' symbol here? not having the '@' works fine
for me)

I prevents an error message being displayed if one should happen. I
always use it with mysql, mail, cookie, and similar functions and trap
errors rather than just letting my app crap out displaying errors that
might compremize the application to the public.

There is another way to avoid showing errors to the public, and I think
it's better:

<quote src="http://es2.php.net/manual/en/security.errors.php">

A better option is not to disable error reporting, but just not showing
them, and let them log to a file.

For example, set in your php.ini file:
log_errors on
display_errors off
error_log /var/log/php_errors

</quote>
This way you can still see the errors, not in the client window but
rather in a private file local to the server. When there's need to
debug, you see the real errors as they happened , instead of having to
edit the source code to let them show and then reproduce them.

Obviously, trapping errors when they happen is the best thing to do, but
it's very difficult to catch 'em all.
Jul 17 '05 #8

P: n/a
JAS
Dani CS wrote:
JAS wrote:
r6****@gmail.com wrote:
(BTW, what use is the '@' symbol here? not having the '@' works fine
for me)


I prevents an error message being displayed if one should happen. I
always use it with mysql, mail, cookie, and similar functions and trap
errors rather than just letting my app crap out displaying errors that
might compremize the application to the public.


There is another way to avoid showing errors to the public, and I think
it's better:

<quote src="http://es2.php.net/manual/en/security.errors.php">

A better option is not to disable error reporting, but just not showing
them, and let them log to a file.

For example, set in your php.ini file:
log_errors on
display_errors off
error_log /var/log/php_errors

</quote>
This way you can still see the errors, not in the client window but
rather in a private file local to the server. When there's need to
debug, you see the real errors as they happened , instead of having to
edit the source code to let them show and then reproduce them.

Obviously, trapping errors when they happen is the best thing to do, but
it's very difficult to catch 'em all.


Yah I totally agree -- I just use them for those few that I are most
likely to occur and are easily trappable @mysql_connect .... or die ...

J
Jul 17 '05 #9

P: n/a
Hi,

why do you not set up an error handling function with
set_error_handler('myErrorHandlingFunctionName') ?

The error handling function typically has parameters ($level, $message,
$filePath, $lineNumber). Trom the error handling function you can log
the error.
if ($level & error_reporting()) {
You can redirect the browser from the error handling function by
outputting the following javascript:
<script>
document.location.href=error.php
</script>;
}
(This usually works better then header("Location: ./error.php");
exit; because it is not limited to before any output has been done)

Greetings,

Henk Verhoeven,
www.phpPeanuts.org.

r6****@gmail.com wrote:
hello,

i am new to PHP programming and wondered if u could help.

lets say i have the following files:

1. error.php: that is routed to for all errors. this page should
display proper and explanatory error messages to the user based on the
parameter provided.
2. errordef.php: has all the error codes and their explanations
"defined" as constants.

now, say if i have 3 different files viz. a.php, b.php and c.php, and
when any error occuring in either these files, i want to route to
error.php for detailed explanation. each a.php, b.php and c.php passes
error code to error.php, but error.php should interpret the code from
the definition file errordef.php!
how can i implement this? any quick help will be appreciated.

Jul 17 '05 #10

P: n/a
Henk Verhoeven wrote:
You can redirect the browser from the error handling function by
outputting the following javascript:
<script>
document.location.href=error.php
</script>;
}
(This usually works better then header("Location: ./error.php");
exit; because it is not limited to before any output has been done)


While it is true that your proposed javascript code is not affected by
the "headers already sent" syndrome, it creates some serious problems
for the user. Read these:

- http://www.w3.org/QA/Tips/reback
- http://www.useit.com/alertbox/990530.html (the first one)

<snip>
Jul 17 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.