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: 12 years ago
Size: 1,672 bytes


Class file image Download

    Get some HTTP headers from an URL

$url = "";

$html = fopen("", "r");
$contents = fread($html, 8192);

$headers = $http_response_header;

    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);

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

$cookie[""] = http_build_cookies($cookies, $url);
$cookie[""] = http_build_cookies($cookies, "", false);

    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[""]."\r\n"

$context = stream_context_create($contexts);
$html = fopen("", "r", false, $context);

while (!
$contents .= fread($html, 8192);

    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