Bluehost.com Web Hosting $6.95

How can i get the location of an exit()/die() from within register_shutdown_function()?

This is a discussion on How can i get the location of an exit()/die() from within register_shutdown_function()? within the PHP General forums, part of the PHP Programming Forums category; Hello there, I have an shutdown function to catch fatal-errors etc.. Now when there is an exit() somewhere i ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-16-2008
Mathijs van Veluw
 
Posts: n/a
Default How can i get the location of an exit()/die() from within register_shutdown_function()?

Hello there,

I have an shutdown function to catch fatal-errors etc..
Now when there is an exit() somewhere i get an empty message from get_last_error().
I want to know the location of this exit() or die().

Is there a way to get the file and line-number from where the exit/die originated?

Thx in advance.
Reply With Quote
  #2 (permalink)  
Old 07-16-2008
Chris
 
Posts: n/a
Default Re: [PHP] How can i get the location of an exit()/die() from withinregister_shutdown_function()?

Mathijs van Veluw wrote:
> Hello there,
>
> I have an shutdown function to catch fatal-errors etc..
> Now when there is an exit() somewhere i get an empty message from
> get_last_error().
> I want to know the location of this exit() or die().
>
> Is there a way to get the file and line-number from where the exit/die
> originated?


debug_backtrace ?

--
Postgresql & php tutorials
http://www.designmagick.com/
Reply With Quote
  #3 (permalink)  
Old 07-16-2008
Mathijs van Veluw
 
Posts: n/a
Default Re: [PHP] How can i get the location of an exit()/die() from withinregister_shutdown_function()?

Chris wrote:
> Mathijs van Veluw wrote:
>> Hello there,
>>
>> I have an shutdown function to catch fatal-errors etc..
>> Now when there is an exit() somewhere i get an empty message from
>> get_last_error().
>> I want to know the location of this exit() or die().
>>
>> Is there a way to get the file and line-number from where the exit/die
>> originated?

>
> debug_backtrace ?
>


This won't work from within the register_shutdown_function() function.
This because the scope is cleared, and the debug_backtrace starts from within the register_shutdown_function() function.
Reply With Quote
  #4 (permalink)  
Old 07-16-2008
Thijs Lensselink
 
Posts: n/a
Default Re: [PHP] How can i get the location of an exit()/die() fromwithin register_shutdown_function()?

Quoting Mathijs van Veluw <mathijs.van.veluw@smscity.com>:

> Hello there,
>
> I have an shutdown function to catch fatal-errors etc..
> Now when there is an exit() somewhere i get an empty message from
> get_last_error().
> I want to know the location of this exit() or die().
>
> Is there a way to get the file and line-number from where the exit/die
> originated?
>
> Thx in advance.
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php


I don't think this is possible in PHP. When exit(0); is called. PHP
execution stops. In the manual there is a small comment about exit
inside a register_shutdown_function

[snip]
Multiple calls to register_shutdown_function() can be made, and each
will be called in the same order as they were registered. If you call
exit() within one registered shutdown function, processing will stop
completely and no other registered shutdown functions will be called.
[/snip]
Reply With Quote
  #5 (permalink)  
Old 07-16-2008
tedd
 
Posts: n/a
Default Re: [PHP] How can i get the location of an exit()/die() fromwithin register_shutdown_function()?

At 9:23 AM +0200 7/16/08, Mathijs van Veluw wrote:
>Hello there,
>
>I have an shutdown function to catch fatal-errors etc..
>Now when there is an exit() somewhere i get an empty message from
>get_last_error().
>I want to know the location of this exit() or die().
>
>Is there a way to get the file and line-number from where the
>exit/die originated?
>
>Thx in advance.


Mathijs:

For MySQL failures I use:

$result = mysql_query($query) or die(report($query,__LINE__ ,__FILE__));


function report($query, $line, $file)
{
echo($query . '<br>' .$line . '<br/>' . $file . '<br/>' .
mysql_error());
}

Perhaps you can modify that for your use.

Cheers,

tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
Reply With Quote
  #6 (permalink)  
Old 07-16-2008
Eric Butera
 
Posts: n/a
Default Re: [PHP] How can i get the location of an exit()/die() from within register_shutdown_function()?

On Wed, Jul 16, 2008 at 3:23 AM, Mathijs van Veluw
<mathijs.van.veluw@smscity.com> wrote:
> Hello there,
>
> I have an shutdown function to catch fatal-errors etc..
> Now when there is an exit() somewhere i get an empty message from
> get_last_error().
> I want to know the location of this exit() or die().
>
> Is there a way to get the file and line-number from where the exit/die
> originated?
>
> Thx in advance.


The way I handle this is by throwing exceptions in my code. So let's
say that there is a db connection/query failure for whatever reason.
Instead of using query() or die() which is not user friendly, I throw
an exception which bubbles up. Once it hits the top then I can catch
it, log it accordingly, and show the user a friendlier error page
saying Oops!

With an exception you get exactly what you want, a full-blown stack
trace complete with paths, line numbers etc. You also get the ability
to be graceful about what you show to the end user.

....but I have the feeling that you're already dealing with a situation
in lots of existing code. Perhaps you could combine some suggestions
in this thread and replace your die/exit statements with a custom
function which logs a debug_backtrace() and then really dies, but
gracefully of course. :)

As an aside, if I were to see some jibberish about a query and line
numbers when I click a link I'd leave that site. (And for the
archives) It is a security vuln to show full file paths to an end
user. If someone is tampering with your system we shouldn't give them
any more information than they can already get.
Reply With Quote
  #7 (permalink)  
Old 07-16-2008
Mathijs van Veluw
 
Posts: n/a
Default Re: [PHP] How can i get the location of an exit()/die() from withinregister_shutdown_function()?

Eric Butera wrote:
> On Wed, Jul 16, 2008 at 3:23 AM, Mathijs van Veluw
> <mathijs.van.veluw@smscity.com> wrote:
>> Hello there,
>>
>> I have an shutdown function to catch fatal-errors etc..
>> Now when there is an exit() somewhere i get an empty message from
>> get_last_error().
>> I want to know the location of this exit() or die().
>>
>> Is there a way to get the file and line-number from where the exit/die
>> originated?
>>
>> Thx in advance.

>
> The way I handle this is by throwing exceptions in my code. So let's
> say that there is a db connection/query failure for whatever reason.
> Instead of using query() or die() which is not user friendly, I throw
> an exception which bubbles up. Once it hits the top then I can catch
> it, log it accordingly, and show the user a friendlier error page
> saying Oops!
>
> With an exception you get exactly what you want, a full-blown stack
> trace complete with paths, line numbers etc. You also get the ability
> to be graceful about what you show to the end user.
>
> ...but I have the feeling that you're already dealing with a situation
> in lots of existing code. Perhaps you could combine some suggestions
> in this thread and replace your die/exit statements with a custom
> function which logs a debug_backtrace() and then really dies, but
> gracefully of course. :)
>
> As an aside, if I were to see some jibberish about a query and line
> numbers when I click a link I'd leave that site. (And for the
> archives) It is a security vuln to show full file paths to an end
> user. If someone is tampering with your system we shouldn't give them
> any more information than they can already get.


Well i don't use 'OR die()' stuff. But exceptions.

For some reason from within the register_shutdown_function() function i get an empty error message.
This only occurs, as far as i know, when there is an exit somewhere.

I want to trace where this comes from.

But i think that this isn't possible, as i looked on the web and there arn't any solutions.

Thx for the help.
Reply With Quote
  #8 (permalink)  
Old 07-16-2008
Eric Butera
 
Posts: n/a
Default Re: [PHP] How can i get the location of an exit()/die() from within register_shutdown_function()?

On Wed, Jul 16, 2008 at 9:41 AM, Mathijs van Veluw
<mathijs.van.veluw@smscity.com> wrote:
> Well i don't use 'OR die()' stuff. But exceptions.
>
> For some reason from within the register_shutdown_function() function i get
> an empty error message.
> This only occurs, as far as i know, when there is an exit somewhere.
>
> I want to trace where this comes from.
>
> But i think that this isn't possible, as i looked on the web and there arn't
> any solutions.
>
> Thx for the help.


Well if this is a very specific issue that you're trying to resolve
perhaps you could try and figure out how the user triggered the error.
You could just save the remote address and request uri, do some
access log searching and re-produce the path the user took through
your site. This has been a helpful technique for me several times.
One of the main problems for me is that I know how to use the systems
I build, so I wouldn't click on stuff in the weird order some users
do.
Reply With Quote
  #9 (permalink)  
Old 07-16-2008
Mathijs van Veluw
 
Posts: n/a
Default Re: [PHP] How can i get the location of an exit()/die() from withinregister_shutdown_function()?

Eric Butera wrote:
> On Wed, Jul 16, 2008 at 9:41 AM, Mathijs van Veluw
> <mathijs.van.veluw@smscity.com> wrote:
>> Well i don't use 'OR die()' stuff. But exceptions.
>>
>> For some reason from within the register_shutdown_function() function i get
>> an empty error message.
>> This only occurs, as far as i know, when there is an exit somewhere.
>>
>> I want to trace where this comes from.
>>
>> But i think that this isn't possible, as i looked on the web and there arn't
>> any solutions.
>>
>> Thx for the help.

>
> Well if this is a very specific issue that you're trying to resolve
> perhaps you could try and figure out how the user triggered the error.
> You could just save the remote address and request uri, do some
> access log searching and re-produce the path the user took through
> your site. This has been a helpful technique for me several times.
> One of the main problems for me is that I know how to use the systems
> I build, so I wouldn't click on stuff in the weird order some users
> do.


Well it is an cronjobed php script that executs several other scripts depending on the time.
Something within those cronjobs triggers the shutdown function without an exit, which i find very strange.
I think i have to debug it to trace the exact steps what its doing, because there is no exit within that script.
Reply With Quote
  #10 (permalink)  
Old 07-16-2008
tedd
 
Posts: n/a
Default Re: [PHP] How can i get the location of an exit()/die() fromwithin register_shutdown_function()?

At 9:15 AM -0400 7/16/08, Eric Butera wrote:
>As an aside, if I were to see some jibberish about a query and line
>numbers when I click a link I'd leave that site. (And for the
>archives) It is a security vuln to show full file paths to an end
>user. If someone is tampering with your system we shouldn't give them
>any more information than they can already get.


It can certainly help you for debugging, but I agree, it's not for production.

tedd

--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 12:10 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0