HTTP header script review

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

Post Reply
127.0.0.1
Forum Newbie
Posts: 22
Joined: Mon Sep 12, 2005 8:59 pm

HTTP header script review

Post by 127.0.0.1 »

Code: Select all

$ch =       curl_init($_POST['url']);
            curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
	        curl_setopt($ch, CURLOPT_HEADER, 1);
$header =   curl_exec($ch);
            curl_close($ch);
 
$header = explode("\r", $header);
 
for($a = 0; $a < count($header); $a++)
	{
	if(ereg(":", $header[$a]) OR eregi("HTTP", $header[$a]))
		{
		echo $header[$a]."<br>";
		}
	else
		{
		break;
		}
	}
Another little script i coded to get your views on. I feel this could be so much better but i cant find any other way of improving it. Speed and performance wise its fine its just the code doesnt look very nice.
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

in your if and else statement indent the statements:-D
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

In my opinion, this would look a little better:

Code: Select all

$ch = curl_init($_POST['url']);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);

$header = curl_exec($ch);
curl_close($ch);

$header = explode("\r", $header);

for($a = 0; $a < count($header); $a++){
    if(ereg(":", $header[$a]) OR eregi("HTTP", $header[$a])){
        echo $header[$a]."<br>";
    }
    else {
        break;
    }
}
Or even

Code: Select all

$ch = curl_init($_POST['url']);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);

$header = curl_exec($ch);
curl_close($ch);

$header = explode("\r", $header);

for($a = 0; $a < count($header); $a++)
{
    if(ereg(":", $header[$a]) OR eregi("HTTP", $header[$a]))
    {
        echo $header[$a]."<br>";
    }
    else 
    {
        break;
    }
}
depending on personal preference.
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post by pilau »

I'd rather ask - "what does it do?"
127.0.0.1
Forum Newbie
Posts: 22
Joined: Mon Sep 12, 2005 8:59 pm

Post by 127.0.0.1 »

Code: Select all

$fp = fopen('http://www.microsoft.com', 'r');
 
print_r($http_response_header);
I found this way -- much better way.

It returns the http headers for a domain.
Post Reply