Page 1 of 2
@ (at) signs in php and still getting errors. (php newbie)
Posted: Thu Jan 08, 2009 7:54 am
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.
Re: @ (at) signs in php and still getting errors. (php newbie)
Posted: Thu Jan 08, 2009 8:46 am
by cptnwinky
Could you post the function or class that's causing trouble and the code in which you use said function?
Re: @ (at) signs in php and still getting errors. (php newbie)
Posted: Thu Jan 08, 2009 9:10 am
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();
}
}
}
}
Re: @ (at) signs in php and still getting errors. (php newbie)
Posted: Thu Jan 08, 2009 9:14 am
by papa
Try:
if(!$res)
Re: @ (at) signs in php and still getting errors. (php newbie)
Posted: Thu Jan 08, 2009 9:20 am
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)
Re: @ (at) signs in php and still getting errors. (php newbie)
Posted: Thu Jan 08, 2009 9:30 am
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.
Re: @ (at) signs in php and still getting errors. (php newbie)
Posted: Thu Jan 08, 2009 9:48 am
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.
Re: @ (at) signs in php and still getting errors. (php newbie)
Posted: Thu Jan 08, 2009 9:50 am
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.
Re: @ (at) signs in php and still getting errors. (php newbie)
Posted: Thu Jan 08, 2009 9:56 am
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.
Re: @ (at) signs in php and still getting errors. (php newbie)
Posted: Thu Jan 08, 2009 10:14 am
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.
Re: @ (at) signs in php and still getting errors. (php newbie)
Posted: Thu Jan 08, 2009 10:57 am
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.
Re: @ (at) signs in php and still getting errors. (php newbie)
Posted: Thu Jan 08, 2009 11:02 am
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.
Re: @ (at) signs in php and still getting errors. (php newbie)
Posted: Thu Jan 08, 2009 11:04 am
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.
Re: @ (at) signs in php and still getting errors. (php newbie)
Posted: Thu Jan 08, 2009 11:55 am
by cptnwinky
Re: @ (at) signs in php and still getting errors. (php newbie)
Posted: Thu Jan 08, 2009 12:27 pm
by musikit
cptnwinky
thank you very much. you have been very informative.
/bow