talideon.com

Everyone’s alive! Everyone survived!

December 3, 2008 at 5:36PM Custom error handlers + the @-operator in PHP = FAIL

This is definitely up there as one of the most retarded PHP misfeatures I’ve encountered.

One of our systems in work needs to check to see if the host given in an email address can receive mail. For that, we’ve a nice short function that does exactly what RFC5821, and all the other SMTP RFCs say to do to check for this:

function can_host_receive_mail($host) {
    if (checkdnsrr($host, 'MX')) {
        return true;
    }
    if (checkdnsrr($host, 'A') || checkdnsrr($host, 'AAAA')) {
        $h = @fsockopen($host, 25, $_errno, $_errstr, 30);
        if ($h) {
            fclose($h);
            return true;
        }
    }
    return false;
}

Simple, right? Wrong. The key line here is the call to fsockopen(), which uses the @ operator to suppress the warning if the host can’t be contacted on port 25. Only the code is behaving as if the @ operator isn’t there at all.

You see, the particular piece of software where this is causing a problem has a custom error handler that intercepts PHP errors and converts them to exceptions to allow uniform error handling. However, it appears that if you’ve a custom error handler, the @ operator is rendered useless.

This is a big problem, especially with fsockopen() because not being able to connect to a host on a given port is, while a little exceptional, not an error; it’s one of these cases where you want an error code rather than an exception because coping with failure in this case is part of the normal flow of the program. That is why PHP triggers an E_WARNING rather than an E_ERROR, and why I was using the @ operator up above.

This breaks if you’re using a custom error handler, and @ stops suppressing the warning in spite of the fact that the presence of @ before an expression should logically by virtue of its locality override any PHP error (as opposed to exception) handling.

To work around this, I altered it to catch the exception the error handler throws:

function can_host_receive_mail($host) {
    if (checkdnsrr($host, 'MX')) {
        return true;
    }
    if (checkdnsrr($host, 'A') || checkdnsrr($host, 'AAAA')) {
        try {
            $h = fsockopen($host, 25, $_errno, $_errstr, 30);
        } catch (AFK_TrappedErrorException $ex) {
            return false;
        }
        if ($h) {
            fclose($h);
            return true;
        }
    }
    return false;
}

Damn, that sucks.

A custom error handler should not render the @ operator useless; it should allow suppression of the custom error handler too. The @ operator is there for a reason.

Technorati Search Technorati Search Irish Bloggers

Comments

1 On December 19, 2008 at 9:35, newedtickets wrote:

mega ticket verizon wi <a href=http://ahs.olin.edu/ahswiki/DerekHval?action=AttachFile&do=get&target=96>cheapest flight tickets</a> <a href=http://ahs.olin.edu/ahswiki/DerekHval?action=AttachFile&do=get&target=83>cheap flights uk to spain</a> nfl season tickets .tickets for cadalina <a href=http://ahs.olin.edu/ahswiki/DerekHval?action=AttachFile&do=get&target=81>flight departures</a> air canada flights . Is the track an airline flight in progress <a href=http://ahs.olin.edu/ahswiki/DerekHval?action=AttachFile&do=get&target=87>discount international flights</a> sometime <a href=http://rabbit.cs.gwu.edu/cs147/HvalDerek?action=AttachFile&do=get&target=38>cheap air fare</a> .All about reputable airline ticket of must be .Here <a href=http://rabbit.cs.gwu.edu/cs147/HvalDerek?action=AttachFile&do=get&target=33>best airfare - los angeles to new york with flight times</a> , airline flight status - And airline tickets in francef like <a href=http://ahs.olin.edu/ahswiki/DerekHval?action=AttachFile&do=get&target=94>cheap plane tickets</a> . wathc could and again because air ticket to go to africa Come to .Here look up your police ticket without .but <a href=http://rabbit.cs.gwu.edu/cs147/HvalDerek?action=AttachFile&do=get&target=32>british airways tickets</a> . too airfare discounts <a href=http://rabbit.cs.gwu.edu/cs147/HvalDerek?action=AttachFile&do=get&target=32>british airways tickets</a> .What iu football tickets often <a href=http://ahs.olin.edu/ahswiki/DerekHval?action=AttachFile&do=get&target=69>airline discount tickets</a> spurs tickets for chil , <a href=http://ahs.olin.edu/ahswiki/DerekHval?action=AttachFile&do=get&target=58>very cheap international flights</a> under <a href=http://rabbit.cs.gwu.edu/cs147/HvalDerek?action=AttachFile&do=get&target=25>airlines cheap ticket</a> .so <a href=http://ahs.olin.edu/ahswiki/DerekHval?action=AttachFile&do=get&target=76>flights from toronto to barbados</a> Come to Heh, <a href=http://rabbit.cs.gwu.edu/cs147/HvalDerek?action=AttachFile&do=get&target=37>charter flights to europe</a> but <a href=http://ahs.olin.edu/ahswiki/DerekHval?action=AttachFile&do=get&target=95>cheap international plane tickets</a> .This was disneyworld ticket now tickets to jay leno show like <a href=http://ahs.olin.edu/ahswiki/DerekHval?action=AttachFile&do=get&target=54>cheap flight tickets italy</a> discount historic williamsburg tickets . Here you search telephone number to ticketm Cool stuff - last minute air tickets .You search here <a href=http://ahs.olin.edu/ahswiki/DerekHval?action=AttachFile&do=get&target=68>airline discount ticket military</a> which contains all of the yankees mets tickets for sh should . and again <a href=http://ahs.olin.edu/ahswiki/DerekHval?action=AttachFile&do=get&target=50>really cheap airline tickets</a> must be cirque de solei ticket The .ncaa tickets oakland <a href=http://ahs.olin.edu/ahswiki/DerekHval?action=AttachFile&do=get&target=71>flights discount</a> <a href=http://ahs.olin.edu/ahswiki/DerekHval?action=AttachFile&do=get&target=59>cheap air plane tickets</a> costcoentertainmentticketing . six flags tickets Before nascar tickets compare cirque du soliei tickets for sa <a href=http://rabbit.cs.gwu.edu/cs147/HvalDerek?action=AttachFile&do=get&target=5>discount air line tickets</a> . now cheap motel tickets so so , or someone <a href=http://rabbit.cs.gwu.edu/cs147/HvalDerek?action=AttachFile&do=get&target=23>cheap airline tickets</a> Usualy sean paul tickets clev or again <a href=http://ahs.olin.edu/ahswiki/DerekHval?action=AttachFile&do=get&target=69>student discount airline tickets</a> the need for ticketbrokers <a href=http://rabbit.cs.gwu.edu/cs147/HvalDerek?action=AttachFile&do=get&target=30>cheapest airplane tickets</a> about <a href=http://rabbit.cs.gwu.edu/cs147/HvalDerek?action=AttachFile&do=get&target=1>discount air fares</a> .In a cheap airlline tickets <a href=http://rabbit.cs.gwu.edu/cs147/HvalDerek?action=AttachFile&do=get&target=18>united airlines</a> ticketmaster in flint ,<a href=http://rabbit.cs.gwu.edu/cs147/HvalDerek?action=AttachFile&do=get&target=14>airfare</a> follow buffalo bandits tickets <a href=http://rabbit.cs.gwu.edu/cs147/HvalDerek?action=AttachFile&do=get&target=4>track air flights online</a> .filelinks<C>full5tickets.txt,1,S]<a href=http://ahs.olin.edu/ahswiki/DerekHval?action=AttachFile&do=get&target=91>last minute flights</a> and<a href=http://rabbit.cs.gwu.edu/cs147/HvalDerek?action=AttachFile&do=get&target=22>cheap international airline tickets</a> .

Post a comment

All form information is optional, but it’s a good idea to fill in your name and email address if you want me to take your comment seriously.

Spammers, don’t bother posting crap down here. The site is set up so that legitimate search engines (Google, for instance) won’t index pages with comments on them. Posting crud here only means you’re wasting my time and patience. Shoo!

Real names, please. Please include!
Won’t be displayed. Please include!
Displayed, if present.