Jump to content


Photo

Feedback on class that collects headers


  • Please log in to reply
No replies to this topic

#1 Re-JeeP

Re-JeeP
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 09 August 2006 - 11:53 AM

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.

<?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 />";	
}

?>

Thanks!

// Johan




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users