PDA

View Full Version : PHP: Get returned HTTP code


oracle128
September 2nd, 2005, 04:56 PM
How can I get PHP to check the returned HTTP code (eg. 200 for OK, 404 for page not found) of a dynamically-constructed URL? I need it to authenticate users in my mail server that uses HTTP authentication. I'd authenticate using the mail server's MySQL database directly, but the creators won't disclose how they encrypted the passwords (we all know lack of information is half of security), so this is not possible.

MishY
September 2nd, 2005, 05:02 PM
EDIT: actually this would be better http://sourceforge.net/projects/snoopy/

oracle128
September 2nd, 2005, 05:16 PM
Thanks, but I might have to use the function, since I don't know how to use PHP classes yet :P

oracle128
September 2nd, 2005, 07:27 PM
OK, I got it working, and here's the code I used to get the status:
$addr="mydomain.com";
$port=80;
$timeout=30;
$path="/";

$urlHandle = fsockopen($addr, $port, $errno, $errstr, $timeout);
$urlString = "GET $path HTTP/1.0\r\nHost: $addr\r\nConnection: Keep-Alive\r\nUser-Agent: MyURLGrabber\r\n";
$urlString .= "\r\n";
fputs($urlHandle, $urlString);
$response=fgets($urlHandle);
echo "Response is $response";
fclose($urlHandle);