I got this function below from zend.com. It can be used to simulate HTTP POST (sending form values to a script).
<?php
function post_it($datastream, $url) {
$url = preg_replace("@^[url]http://@i[/url]", "", $url);
$host = substr($url, 0, strpos($url, "/"));
$uri = strstr($url, "/");
$reqbody = "";
foreach($datastream as $key=>$val) {
if (!empty($reqbody)) $reqbody.= "&";
$reqbody.= $key."=".urlencode($val);
}
$contentlength = strlen($reqbody);
$reqheader = "POST $uri HTTP/1.0\r\n".
"Host: $host\n". "User-Agent: PostIt\r\n".
"Content-Type: application/x-www-form-urlencoded\r\n".
"Content-Length: $contentlength\r\n\r\n".
"$reqbody\r\n";
$socket = fsockopen($host, 80, $errno, $errstr);
if (!$socket) {
$result["errno"] = $errno;
$result["errstr"] = $errstr;
return $result;
}
fputs($socket, $reqheader);
while (!feof($socket)) {
$result[] = fgets($socket, 4096);
}
fclose($socket);
return $result;
}
?>
Example how to use it :
$data["Username"] = "something";
$data["Password"] = "thepassword";
post_it($data, "http://www.example.com/login.php");
if (isset($result["errno"])) {
$errno = $result["errno"];
$errstr = $result["errstr"];
echo "<B>Error $errno</B> $errstr";
exit;
} else {
for($i=0;$i< count($result); $i++) echo $result[$i];
}
I think it works fine. But the script "stays". I meant, in the login.php it will check the username and password and if correct, user will be redirected to "main.php" (and created a php session).
When I use the function, I got no redirection.
Anyone know any workaround to this problem?
Thanks.