Show server headers

Discussion in 'PHP' started by AHA7, May 18, 2007.

  1. #1
    Hello,

    I need a PHP script that can show me the server response headers. For example; status, content-type,...

    Is there a function or so that I can use to show the server headers?
     
    AHA7, May 18, 2007 IP
  2. chopsticks

    chopsticks Active Member

    Messages:
    565
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #2
    chopsticks, May 18, 2007 IP
  3. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #3
    that'l only work for php5

    
    <?php
    class socket{
    
         var $socket ;
         
        function connect( $hostname, $port = 80 ) {
             
            $this->hostname = $hostname ;
             $this->port = $port ;
             
             if( !function_exists('socket_create') and !dl( sprintf('php_sockets.%s', PHP_SHLIB_SUFFIX ) ) ):
                 return die( sprintf( 'socket::__construct( ) requires php_sockets.%s to be loaded', PHP_SHLIB_SUFFIX ) );
            elseif( !( $this->socket = socket_create( AF_INET, SOCK_STREAM, SOL_TCP ) ) ):
                return $this->_error( 'socket_create() failed' );
            elseif( !socket_connect( $this->socket, $hostname, $port ) ):
                return $this->_error( 'socket_connect() failed' );
            endif;
            
            return true ;
        }    
        function _error( $string ) {
            die( 
                sprintf( '%s errno [ %s ] : %s', 
                         $string, 
                         socket_last_error( $this->socket ), 
                         socket_strerror( socket_last_error( $this->socket ) ) 
                )
            );    
        }
        function read( $length = 1024 ){
            if( ( $data = socket_read( $this->socket, $length ) ) === false ) return $this->_error( 'socket::read( ) failed' );
            else return $data;
        }
        function write( $string ){
            if( ( $length = socket_write( $this->socket, $string ) ) === false ) return $this->_error( sprintf( 'socket::write( "%s" ) failed', $string ) );
            else return $length ;
        }
        function headers( $uri = '/' ){
            if( $this->write( 
                sprintf( "HEAD %s HTTP/1.1\r\n", $uri ).
                sprintf( "Host: %s\r\n", $this->hostname ).
                "Connection: Close\r\n\r\n"
            ) ) return $this->headers_assoc( $this->read( ) );
            else return $this->_error( sprintf( 'socket::headers( "%s" ) failed', $uri ) );
        }
        function get( $uri, $headers = "" ){
            if( $this->write( 
                sprintf( "GET %s HTTP/1.1\r\n", $uri ).
                sprintf( "Host: %s\r\n", $this->hostname ).
                $headers.
                "Connection: Close\r\n\r\n"
            ) ) return true;
            else return $this->_error( sprintf( 'socket::get( "%s" ) failed', $uri ) );
        }
        function post( $uri, $post, $headers = "" ){
            if( $this->write( 
                sprintf( "POST %s HTTP/1.1\r\n", $uri ).
                sprintf( "Host: %s\r\n", $this->hostname ).
                "Content-Type: application/x-www-form-urlencoded\r\n".
                $headers.
                sprintf( "Content-Length: %s\r\n", strlen( $this->serialize( $post ) ) ).
                "Connection: Close\r\n\r\n"
            ) and $this->write( $this->serialize( $post ) ) ) return true;
            else return $this->_error( sprintf( 'socket::post( "%s", "%s" ) failed', $uri, @$this->serialize( $post ) ) );    
        }
        function headers_assoc( $headers ){
             $headers = explode( "\r\n", $headers );
             preg_match('~HTTP/([0-9\.]+) ([0-9]+) ([a-zA-Z ]+)~', $headers[0], $http );
             $return["HTTP"] = $http[1];
             $return["Code"] = $http[2];
             $return["Mesg"] = $http[3];
            foreach( $headers as $line => $header ) {
                if( $header and $line != 0 ) {
                    $temp = split( ':', $header );
                    $return[ trim( $temp[0] ) ] = trim( $temp[1] );
                }
            }
            return $return ;
        }
        function auth( $username, $password, $type = 'Basic', $handler = null )
        {
            return sprintf(
                "Authorization: %s %s\r\n",
                $type,
                function_exists( $handler ) ? call_user_func( $handler, $username, $password ) : base64_encode("$username:$password")
            );
        }
        function serialize( $array ){
            foreach( $array as $key => $value )
                $serial[ ] = sprintf( '%s=%s', $key, urlencode( $value ) );
            return implode( '&', $serial );
        }
        function close( )
        {
            return !$this->socket ? false : socket_close( $this->socket );
        }
    }
    $socket = new socket( );
    $socket->connect( 'digitalpoint.com' );
    print_r( $socket->headers( ) );
    $socket->close( );
    $socket->connect( 'krakjoe.com' );
    print_r( $socket->headers( '/index.php' ) );
    $socket->close( );
    ?>
    
    PHP:
    that'll work on anything with sockets, that code does some other pretty neat stuff too ....
     
    krakjoe, May 18, 2007 IP