@ (at) signs in php and still getting errors. (php newbie)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

musikit
Forum Newbie
Posts: 7
Joined: Thu Jan 08, 2009 7:44 am

@ (at) signs in php and still getting errors. (php newbie)

Post by musikit »

hello,

i'm pretty new to PHP and just inherited a large amount of PHP work.

i have been getting helpful advice from friends that code in PHP but this question is a little more then their familiarity.

i have some code that connects using fsockopen and/or socket_create (and that series of functions) to communicate with another host.

sometimes this host does not respond. and i get a PHP "fatal error" causing my stack to be completely be destroyed and end the script. i have valid code i wrote to handle the error condition however this fatal error is stopping it from executing.

i heard of putting an @ (at) sign at the beginning of the function and that would resolve the issue however it does not seem to be working that way. i still get a "fatal error" stopping my script from executing.

is there anyone who can assist me so i can effectively handle the condition correctly so instead of my script exiting it just goes to the error condition i wrote?

thank you all for any assistance you could provide.
cptnwinky
Forum Commoner
Posts: 84
Joined: Sat Dec 27, 2008 10:58 am
Location: Williamstown, MA

Re: @ (at) signs in php and still getting errors. (php newbie)

Post by cptnwinky »

Could you post the function or class that's causing trouble and the code in which you use said function?
musikit
Forum Newbie
Posts: 7
Joined: Thu Jan 08, 2009 7:44 am

Re: @ (at) signs in php and still getting errors. (php newbie)

Post by musikit »

i cant post the code as is. but here is a dumbed down version

Code: Select all

 
class MySocketClass
{
    function connect($host, $port)
    {
        $res = @fsockopen($host,$port, $errno, $errstr, 10);
        if($res !=== false)
        {
            echo "i get fatal error on the page instead of my code here";
            return false;
        }
        return true;
    }
}
 
class MyClass
{
 
    function myMethod($param1, $param2)
    {
        $obj = new MySocketClass();
        if($obj !== null)
        {
            $res = $obj->connect('63.210.148.206','15668');
            if($res === true)
            {
                $cmdout = $obj->method1('hello', 'world');
                $cmdout = $obj->method2($param1, $param2);
                $cmdout = $obj->method3();
                $cmdout = $obj->close();
            }
        }
    }
 
}
 
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: @ (at) signs in php and still getting errors. (php newbie)

Post by papa »

Try:

if(!$res)
cptnwinky
Forum Commoner
Posts: 84
Joined: Sat Dec 27, 2008 10:58 am
Location: Williamstown, MA

Re: @ (at) signs in php and still getting errors. (php newbie)

Post by cptnwinky »

Your returning false upon a succesful connection. Its just backwords. Your checking to make sure true is returned from the function but false is actually being returned instead.

This
if($res !=== false)

Should be changed to this
if($res === false)
musikit
Forum Newbie
Posts: 7
Joined: Thu Jan 08, 2009 7:44 am

Re: @ (at) signs in php and still getting errors. (php newbie)

Post by musikit »

ive tried both of those. neither stops the fatal error from appearing instead of my code path for the error.

the fatal error states the problem is on line 9. "cant connect to host" or similiar.
cptnwinky
Forum Commoner
Posts: 84
Joined: Sat Dec 27, 2008 10:58 am
Location: Williamstown, MA

Re: @ (at) signs in php and still getting errors. (php newbie)

Post by cptnwinky »

This may sound like a dumb question but have you made sure that the port is accesible (no firewalls on either side) outside of your code? Also, are you positive that the transport protocol your using is TCP? fsockopen will default to TCP if no transport method is specified.
musikit
Forum Newbie
Posts: 7
Joined: Thu Jan 08, 2009 7:44 am

Re: @ (at) signs in php and still getting errors. (php newbie)

Post by musikit »

cptnwinky,

i have stated in my first post "sometimes this host does not respond. and i get a PHP "fatal error" causing my stack to be completely be destroyed and end the script."

i am trying to effectively handle the condition you are specifically asking me to check. i.e. i want to connect to a host that does not exist or not open on that port and it not destroy my script execution.
cptnwinky
Forum Commoner
Posts: 84
Joined: Sat Dec 27, 2008 10:58 am
Location: Williamstown, MA

Re: @ (at) signs in php and still getting errors. (php newbie)

Post by cptnwinky »

Ah, yes your right, I'm sorry. I just spaced that apparently.

If your using PHP5 you can specify your own error handler fairly easily and turn off all error reporting (error_reporting(0)). Heres an example of a custom error handler http://us.php.net/manual/en/function.se ... andler.php.
musikit
Forum Newbie
Posts: 7
Joined: Thu Jan 08, 2009 7:44 am

Re: @ (at) signs in php and still getting errors. (php newbie)

Post by musikit »

yes i am using that function to send me an email when my script fails. hence how i found out about this issue.

it handles errors 1,2,4,16,32,64,128,256,512,

there a mapping somewhere from the numerical values to the E_values so i can better understand what im catching and what im not?

thanks.
cptnwinky
Forum Commoner
Posts: 84
Joined: Sat Dec 27, 2008 10:58 am
Location: Williamstown, MA

Re: @ (at) signs in php and still getting errors. (php newbie)

Post by cptnwinky »

Now that I think about it, I don't think you can catch fatal errors. There has to be something going on in the background that is causing fsockopen to raise E_ERROR instead of returning false upon an unsucesful attempt. Sorry but I'm kind of stumped at this point.
cptnwinky
Forum Commoner
Posts: 84
Joined: Sat Dec 27, 2008 10:58 am
Location: Williamstown, MA

Re: @ (at) signs in php and still getting errors. (php newbie)

Post by cptnwinky »

I found this php bug that seems to relate but it's from 2003. http://bugs.php.net/bug.php?id=23131

It sounds to me though exactly whats going on in your code.
musikit
Forum Newbie
Posts: 7
Joined: Thu Jan 08, 2009 7:44 am

Re: @ (at) signs in php and still getting errors. (php newbie)

Post by musikit »

cptnwinky,

ok thank you very much for your help. i guess ill see what errors i get back for a while and decide if i need to log those to my email.

yeah it does seem there solution was not to have the ability to actually supress the output but instead install an exception handler to allow you to customize the output.

very wierd.

thank you though apprecaite the help.

if anyone has a numerical -> const name mapping somewhere or can tell me where i can find it i would appreciate it.
cptnwinky
Forum Commoner
Posts: 84
Joined: Sat Dec 27, 2008 10:58 am
Location: Williamstown, MA

Re: @ (at) signs in php and still getting errors. (php newbie)

Post by cptnwinky »

musikit
Forum Newbie
Posts: 7
Joined: Thu Jan 08, 2009 7:44 am

Re: @ (at) signs in php and still getting errors. (php newbie)

Post by musikit »

cptnwinky

thank you very much. you have been very informative.

/bow
Post Reply