I have 2 sites that are the same, but different databases and data. One site is http://www.somesite.com and the other is sub.somesite.com. What I am doing is if someone logs into http://www.somesite.com and the login details don’t match http://www.somesite.com, but they do match sub.somesite.com, then use cURL to submit the information and log them in. I have the cURL working great:
foreach($_POST as $key=>$p) {
if ($post != "") {
$post .= "&";
}
$post .= $key."=".$p;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://sub.somesite.com");
curl_setopt($ch, CURLOPT_POST, count($_POST));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
$cArray = curl_getinfo($ch);
$newURL = $cArray[url];
echo str_replace('</head>','<base href="'.$newURL.'" /></head>',$result);
curl_close($ch);
What this does is collects the post variables, submits them to the other site and adds a base url so that the paths are correct.
The problem that I am having is any other page after. So say I am on somesite.com, submit the data. Even though it is going to sub.somesite.com/welcome.php, it displays in the address bar somesite.com and the information is stored in the cookies.txt. Now when I click a link (sub.somesite.com/page2.php) on sub.somesite.com/welcome.php, it redirects to the login page because the sessions are in the cookies.txt and now aren’t accessible.
How would I make the sessions continue to work on all pages?
Thanks in advance.
It is because CURL executes it on the server not on the client (browser). Your server might have the users cookies but the user does not.
You want to set the cookies for the browser using setcookie and forget about the Curl.