Jump to content

Archived

This topic is now archived and is closed to further replies.

Re-JeeP

Feedback on class that collects headers

Recommended Posts

Hi!

I have made a class that collects the headers a server sends when you visit it threw port 80.

Because I'm new to OOP I'd want some feedback on my class.

[code]<?php

class headers
{
var $server;
var $port;
var $fp;

function headers($server, $port = 80)
{
$this->server = $server;
$this->port = $port;
}

function get_headers()
{
$this->fp = $this->connect();
if($this->fp)
{
$this->send_request();
$output = $this->save();
$this->close();

return $this->trim_output($output);
}
else {
return array("Failed!");
}
}

function trim_output($output)
{
$rows = explode("\n", $output);

foreach($rows as $value)
{
if($value == "\r") {
return $headers;
} else {
$headers[] = htmlentities($value);
}
}
return $headers;
}

function connect()
{
return fsockopen($this->server, $this->port, $errno, $errstr, 5);
}

function send_request()
{
return fputs($this->fp, "GET / HTTP/1.1\r\nHost: $this->server\r\n\r\n");
}

function save()
{
while(!feof($this->fp)) {
$result .= fgets($this->fp, 128);
}
return $result;
}

function close()
{
return fclose($this->fp);
}
}

$headers = new headers("domain.com");
$get = $headers->get_headers();

foreach($get as $value) {
echo $value."<br />";
}

?>[/code]

Thanks!

// Johan

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.