PHP Classes

File: test/test_Cookie_Jar.php

Recommend this page to a friend!
  Classes of Keyvan Minoukadeh   Cookie Jar   test/test_Cookie_Jar.php   Download  
File: test/test_Cookie_Jar.php
Role: Unit test script
Content type: text/plain
Description: test script for class
Class: Cookie Jar
Class for handling cookies (for HTTP clients)
Author: By
Last change:
Date: 22 years ago
Size: 3,071 bytes
 

Contents

Class file image Download
<?php
error_reporting
(E_ALL);
define('HTTP_DEBUG', true);
echo
'<pre>';

// include class
require_once('../Cookie_Jar.php');

//
// Loading and saving
//
print_section('Loading and saving cookies');
$options = array(
   
'file_persistent' => 'cookies.txt'
   
);
$jar =& new Cookie_Jar($options);
var_dump($jar->cookies);
// save cookies to new file
$jar->save_persistent_cookies('saved_cookies.txt');


//
// Get matching cookies
//
print_section('Get matching cookies');
$params = array('domain'=>'www.ebay.co.uk', 'path'=>'/', 'secure'=>false);
$cookies = $jar->get_matching_cookies($params);
echo
"\n",'Cookies found: ',$cookies;


//
// Parsing a Set-Cookie HTTP header field
//
print_section('Parsing a Set-Cookie HTTP header field');
// create instance of cookie jar class
$jar =& new Cookie_Jar();
// As I've not yet finished the HTTP response class,
// I'll be using the parse_set_cookies() method.

// set bbc.co.uk cookie
$bbc_cookie = 'BBC-UID=83edcf03bb5df4d84a1ef78f21763b2af0b64907604890a0f250fc054979da5a0Mozilla%2f5%2e0%20%28Windows%3b%20U%3b%20Windows%20NT%205%2e0%3b%20en%2dUS%3b%20rv%3a1%2e0%2e1%29%20Gecko%2f20020826; expires=Mon, 08-Dec-03 21:44:40 GMT; path=/; domain=bbc.co.uk;';
// we have to pass the host and path details so the method can determine whether it can set the cookie
// or not.
$jar->parse_set_cookies(array($bbc_cookie), array('host'=>'www.bbc.co.uk', 'path'=>'/'));

// set stopwar.org.uk cookie
$stopwar_cookie = 'ASPSESSIONIDGGGGGGGG=DRUHSKGCDJMNBPCABXCMFSQQPQQQKGSX; path=/';
$jar->parse_set_cookies(array($stopwar_cookie), array('host'=>'www.stopwar.org.uk', 'path'=>'/'));

echo
"\n",'Cookie jar holds: ',"\n";
echo
var_dump($jar->cookies);



//
// Use the cookie jar with a callback function
//
print_section('Use the cookie jar with a callback function');
// define callback function to print out all cookies in jar at the moment
function print_cookies(&$jar, $cookie_parts, $param)
{
   
extract($cookie_parts);
    echo
'<tr>';
    echo
'<td>',$name,'</td>';
    echo
'<td>',$value,'</td>';
    echo
'<td>',$domain,'</td>';
    echo
'<td>',$path,'</td>';
    echo
'<td>',(is_int($expires) ? gmdate('d m Y, H:i:s', $expires) : 'session'),'</td>';
    echo
'</tr>';
    return
true;
}
echo
'<table border="1"><tr><th>';
echo
implode('</th><th>', array('name','value','domain','path','expires'));
echo
'</th></tr>';
// call scan() with callback
$param = null;
$jar->scan('print_cookies', $param);
echo
'</table>';




//
// Save session cookies
//
print_section('Save session cookies');
if (
$jar->save_session_cookies('saved_session_cookies.txt')) {
    echo
'Session cookies saved!';
} else {
    echo
'Failed saving session cookies';
}




function
print_section($desc='')
{
    static
$num = 1;
    echo
"\n\n";
    echo
'+---------------------------------------------',"\n";
    echo
'| Test ',$num,' - ',$desc,"\n";
    echo
'+---------------------------------------------',"\n";
    echo
"\n";
   
$num++;
}
?>