I haven't forgotten you, I've just been snowed under the last couple of days. Below is a script which intercepts the cookies sent from the server after opening the site using the fopen() function.
It also reads cookies sent back from the client which start with CLIENT__ and sends these along with the request back to the server. This will ensure that the cookie traffic is emulated between the server and the site.
I have used the fopen function rather han fsockopen, as you will not have to worry about sending an HTTP 1.1 request and possibly deal with a chunked response. It also means that it will be compatible with https requests. I've commented where possible, but if there is anything you need explaining, just reply.
<?php
// look for cookies which we need to send they are prepended with CLIENT__ - create an array of headers
$cookieList = array();
foreach($_COOKIE as $name => $value) {
if (substr($name, 0, 8) == 'CLIENT__') {
$name = substr($name, 8); //strip CLIENT__ from th cookie name
$cookieList[] = urlencode($name) . '=' . urlencode($value);
}
}
$cookieList = 'Cookie: ' . implode(";", $cookieList); // merge into a string Cookie: var=value;var=value ...
/* set the user agent and append the query data to this using \r\n */
//privacy programs sometimes block the UA string
$user_agent = @$_SERVER['HTTP_USER_AGENT']?$_SERVER['HTTP_USER_AGENT']:'PHP-Server';
ini_set('user_agent', $user_agent . "\r\n" . $cookieList);
$fp = fopen("http://www.phpbuilder.com/forum/", 'r'); // sendthe request (change as neccessary)
$response = stream_get_meta_data($fp); // this gets other info about the response
$headers = $response['wrapper_data']; // response headers are contained here
foreach($headers as $header) {
$pos = strpos($header, ':'); // find the position of the first : which siginfies the header name
if ($pos === false) continue; // bad header?? ignore -- this should never happen
$headerName = substr($header, 0, $pos);
$headerValue = substr($header, $pos + 1);
if ($headerName == 'Set-Cookie') { // look for cookies
process_cookie($headerValue);
}
}
function process_cookie($cookieData)
{
$cookie = array();
$cookieData = explode(';', $cookieData); //split the cookie data
/* each item in the cookieData array should now be a name=value pair
the first item is the cookie name and value, these items are urlencoded()
and must he decoded before setting the cookie this end
*/
$first = true; // the first item is the value - we need to know if this is the first
foreach($cookieData as $item) {
$item = trim($item);
$pos = strpos($item, '='); // find the position of the first = for the item name
if ($pos === false) continue;
$name = substr($item, 0, $pos);
$value = substr($item, $pos+1);
if ($first) {
$first = false;
/* decode the value and cookie name */
$cookie['name'] = urldecode($name);
$cookie['value'] = urldecode($value);
} else {
$cookie[$name] = $value;
}
}
/* set the cookie and prepend it with a special value */
setcookie('CLIENT__' . $cookie['name'],
$cookie['value'],
@$cookie['expires']?strtotime($cookie['expires']):null,
@$cookie['path'],
$_SERVER['SERVER_NAME'], // cookie domain is always this domain
@$cookie['secure']);
}
?>