<?php
function PostToHost($url, $data, $sessionid = NULL) {
$rdata = array();
if(!function_exists('parse_url') || !function_exists('http_build_query')) {
return FALSE;
}
$data = http_build_query($data);
$host = parse_url($url, PHP_URL_HOST);
$path = parse_url($url, PHP_URL_PATH);
if(!$fp = @fsockopen($host, 80)) {
echo 'Verbindungsaufbau fehlgeschlagen!';
return FALSE;
}
fputs($fp, 'POST '.$path.' HTTP/1.1'."\r\n");
fputs($fp, 'Host: '.$host."\r\n");
if(isset($sessionid)) {
fputs($fp, 'Set-Cookie: PHPSESSID='.$sessionid."\r\n");
}
fputs($fp, 'Referer: '.$url."\r\n");
fputs($fp, 'Content-type: application/x-www-form-urlencoded'."\r\n");
fputs($fp, 'Content-length: '.strlen($data)."\r\n");
fputs($fp, 'Connection: close'."\r\n\r\n");
fputs($fp, $data);
while(!feof($fp)) {
$result .= fgets($fp, 128);
}
$lines = explode($result, "\n");
foreach($lines as $line) {
// Wenn die Zeile nicht leer ist
if(trim($line) != '') {
// Wenn noch nichts über das HTTP bekannt ist und es sich bei der Zeile um HTTP-Informationen handelt
if(!array_key_exists('HTTP', $rdata) && strstr($line, 'HTTP') !== FALSE) {
$temp = explode($line, ' ');
print_r($temp);
$version = explode($temp[0], '/');
$version = $version[1];
$rdata['HTTP']['version'] = $version; // HTTP-Version
$rdata['HTTP']['status'] = (int) $temp[1]; // HTTP-Statuscode
unset($version, $temp);
}
// Ansonsten: andere Header-Informationen
else {
$temp = explode($line, ':', 2);
switch(trim(strtolower($temp[0]))) {
// Datums- und Zeitangabe
case 'date':
$rdata['DATE'] = trim($temp[1]);
break;
// Server-Informationen
case 'server':
$rdata['SERVER'] = trim($temp[1]);
break;
case 'x-powered-by':
$rdata['X_POWERED_BY'] = trim($temp[1]);
break;
// Cookie-Informationen
case 'set-cookie':
$array = explode($temp[1], ',');
// Bereinigt und analysiert die Informationen
foreach($array as $key => $value) {
$temp2 = explode($value, '=', 2);
if($key == 0) {
$name = $temp2[0]; // Namen des Cookies speichern
$content = trim($temp2[1]); // Inhalt des Cookies
}
// Andere Daten:
else {
// Ablaufzeitpunkt, Pfad und Domain ermitteln
switch(strtolower(trim($temp2[0]))) {
// Ablaufzeitpunkt
case 'expires':
$expires = trim($temp2[1]);
break;
// Pfad
case 'path':
$path = trim($temp2[1]);
break;
// Domain
case 'domain':
$domain = trim($temp2[1]);
break;
// Ansonsten: ignorieren
default: break;
}
}
}
$rdata['COOKIE'][] = array('NAME' => $name, 'DATA' => $content, 'EXPIRES' => $expires, 'PATH' => $path, 'DOMAIN' => $domain);
unset($name, $content, $expires, $path, $domain, $array, $key, $value, $temp2);
break;
// Cache-Informationen
case 'cache-control':
$array = explode(trim($temp[1]), ',');
// Bereinigt die Informationen
foreach($array as $value) {
// Bereinigte Information in temporäres Array speichern
$temp2[] = trim($value);
}
$rdata['CACHE'] = $temp;
unset($temp2, $value, $array);
break;
// Verweis auf Seite
case 'location':
$rdata['LOCATION'] = trim($temp[1]);
break;
// Handlungen
case 'pragma':
$rdata['PRAGMA'] = trim($temp[1]);
break;
// Inhaltsgröße
case 'content-length':
$rdata['CONTENT_LENGTH'] = trim($temp[1]);
break;
// Informationen über den Verbindungsstatus
case 'connection':
$rdata['CONNECTION'] = trim($temp[1]);
break;
// Typ des Inhalts
case 'content-type':
$rdata['CONTENT'] = trim($temp[1]);
break;
// Ablaufzeitpunkt
case 'expires':
$rdata['EXPIRES'] = trim($temp[1]);
break;
// Andere Informationen
default:
$rdata['OTHER'][] = trim($temp[1]);
break;
}
}
}
}
fclose($fp);
return array('referer' => $url, 'host' => $host, 'path' => $path, 'data' => $rdata, 'resource' => $result);
}
?>