I’m trying to create a ‘remote login’ site, but when I try the below code, I get the error about cookies being disabled, I’ve googled for a solution, but the only solved case has cURL way of doing that(cookie jar), how could I solve the cookie problem? And how could I find the cookie that the site needs(it has about 10 of them)?
<?php
$vars = array(
'email' => "*****",
'pass'=>"*****"
);
$content = http_build_query($vars);
$host = 'www.site.com';
$service_uri = '/login.php';
$vars ='login_mode=1';
$header = "Host: $host\r\n";
$header .= "User-Agent: PHP Script\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Referer: http://www.site.com \r\n";
$header .= "Content-Length: ".strlen($content)."\r\n";
$header .= "Connection: close\r\n\r\n";
$fp = fsockopen("".$host,80, $errno, $errstr);
if (!$fp)
{
echo "$errstr ($errno)<br/>\n";
echo $fp;
}
else
{
fputs($fp, "POST $service_uri HTTP/1.1\r\n");
fputs($fp, $header.$content);
while(!feof($fp))
{
echo fgets($fp, 128);
}
fclose($fp);
}
?>
You first have to request the homepage URL, which will give you cookie headers. Parse them and send them to the server by adding them to your
$headervariable.You don’t want to care about which cookie the site needs – just send all of them, which makes it future-proof.
If you don’t want to do that all yourself, have a look at PEAR’s HTTP_Request2 package – it has cookie handling built-in.