Question

I am trying to write an "auto-login" script, that uses curl to log in to a specific site and then echoing the response, so that the user can "bypass" the login. (For example, to grant one-time logins etc)

Obviously, using the following method, the cookie is stored on the server, not on the client, in cookie.txt.

I've tried setting $_COOKIE, but that won't work, since the URLs (actually subdomains) of the auto-login script and the service to be logged into, are different.

Login works fine. You see the screen as if you were logged in, but clicking any link requires you to login again.

Any ideas? Thanks

The code I've used to auto-login:

<?php
$username="email@domain.com"; 
$password="password"; 
$url="http://sub.domain-to-login.com/index.php/sessions/login"; 
$cookie="cookie.txt"; 

$postdata = "email=".$username."&password=".$password.'&btn_login=Login'; 

$ch = curl_init(); 
curl_setopt ($ch, CURLOPT_URL, $url); 
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); 
curl_setopt ($ch, CURLOPT_TIMEOUT, 60); 
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie); 
curl_setopt ($ch, CURLOPT_REFERER, $url); 

curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata); 
curl_setopt ($ch, CURLOPT_POST, 1); 
$result = curl_exec ($ch); 
curl_close($ch);
echo $result;  
?>
Was it helpful?

Solution

I guess that you need to replace all links (in $result) from http://sub.domain-to-login.com/index.php/stuff to something like http://mysite.com?url=http://sub.domain-to-login.com/index.php/stuff and then using curl download this site again. You just cannot set cookie for diffrent site.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top