I have a simple php script that issues a curl HTTP POST request then displays the data to the user via redirection. The problem I'm having is, if more than one person runs the script at the same time it will execute and complete successfully for one person, but it fails for the other person. I think it may be session or cookie related, but i'm not using session_start() and cookies are cleared before they are redirected.
Why is this happening and can I adjust my script to support simultaneous users?
<?php
$params = "username=" . $username . "&password=" . $password . "&rememberusername=1";
$url = httpPost("http://www.mysite.com/", $params);
removeAC();
header(sprintf('Location: %s', $url));
exit;
function removeAC()
{
foreach ($_COOKIE as $name => $value)
{
setcookie($name, '', 1);
}
}
function httpPost($url, $params)
{
try {
//open connection
$ch = curl_init($url);
//set the url, number of POST vars, POST data
// curl_setopt($ch, CURLOPT_COOKIEJAR, "cookieFileName");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//execute post
$response = curl_exec($ch);
//print_r(get_headers($url));
//print_r(get_headers($url, 0));
//close connection
curl_close($ch);
return $response;
if (FALSE === $ch)
throw new Exception(curl_error($ch), curl_errno($ch));
// ...process $ch now
}
catch(Exception $e) {
trigger_error(sprintf(
'Curl failed with error #%d: %s',
$e->getCode(), $e->getMessage()),
E_USER_ERROR);
}
}
?>