PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of windylea   HTTPCookie   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: An example for the usage of this class
Class: HTTPCookie
Manipulate cookies from request header values
Author: By
Last change: Small changes
Date: 13 years ago
Size: 1,672 bytes
 

Contents

Class file image Download
<pre>
<?php
require_once("httpcookie.php");

/*
    Get some HTTP headers from an URL
*/

$url = "http://msn.com";

$html = fopen("http://www.msn.com", "r");
$contents = fread($html, 8192);
fclose($html);

$headers = $http_response_header;
var_dump($headers);

/*
    Parse cookies from the HTTP headers by calling the function http_parse_cookies(). Note that the 2nd arg for this function is the ORIGINAL URL that returned the HTTP headers.
*/

$cookies = http_parse_cookies($headers, $url);
var_dump($cookies);

/*
    Rebuild cookie from the cookies array, call the function http_build_cookies(). Cookie can be built for different URLs.
*/

$cookie["msn.com"] = http_build_cookies($cookies, $url);
$cookie["www.msn.com"] = http_build_cookies($cookies, "http://www.msn.com", false);
var_dump($cookie);

/*
    Make a HTTP request with the returned cookie
*/

$contexts = array(
   
"http" => array(
   
"method" => "GET",
   
"header" =>
       
"Content-type: application/x-www-form-urlencoded\r\n".
       
"Cookie: ".$cookie["www.msn.com"]."\r\n"
   
)
);

$context = stream_context_create($contexts);
$html = fopen("http://www.msn.com", "r", false, $context);

while (!
feof($html))
{
   
$contents .= fread($html, 8192);
}
fclose($html);

/*
    Cookies may also be saved for later use :
    $saved_cookie = http_save_cookies($cookies); // return string on success
    $load_cookie = http_load_cookies($saved_cookie);

    Or :
    $cookie_file = "my_cookie.txt";
    http_save_cookies($cookies, $cookie_file); // return true on success
    $load_cookie = http_load_cookies($cookie_file, true); // 2nd arg to tell that the 1st arg is a file name
*/
?>
</pre>