Forum Moderators: coopster

Message Too Old, No Replies

Site login withn cUrl

running into difficulties

         

Hugene

12:18 am on Mar 27, 2005 (gmt 0)

10+ Year Member



Hi everyone. I am trying to automate a submission I do daily.

Manually, I access a page of the type [signin.example.com...] When I fill the username / password, I am automatically redirected to another https page, of the type [home.example.com...] and the page confirms that I am logged in, from there on I can update my posting.

When I am logged in, there are 2 cookies set on my computer, one by home.example.com and one by signin.example.com.

When I log out both cookies are gone.

Now, I am trying to automate that login, and I am using the following script as example: [curl.haxx.se...]

Here is my code


$id = "id";
$password = "password";
$cookie_file_path = "absolute_server_path/httpdocs/cookiejar/";
$LOGINURL = "https://www.signin.example.com/loginForm.html";
$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
$POSTFIELDS = 'UserName='.$id.'&hiddenVar1=hiddenVar1Value&UserPass='.$password;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$LOGINURL);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // this line makes it work under https

$result=curl_exec ($ch);
curl_close ($ch);

echo("Results: <br>".$result);

//post info for login
$LOGINURL = "https://www.signin.example.com/loginForm.html";
$reffer = "https://www.signin.example.com/loginForm.html";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$LOGINURL);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$POSTFIELDS);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_REFERER, $reffer);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$result = curl_exec ($ch);
curl_close ($ch);

echo("Results: <br>".$result);

When I run this code, I don't even get the output from the las echo statement (Results: <br> ...), as the server I query automatically redirects me to the same login page [signin.example.com...] but with no error messages or explanations.

Now, as you can see I attempt to have the cookies created in a folder on my web server, using an absolute path absolute_server_path/httpdocs/cookieJar. Can this be one of the problems, does the cookieJar have to sit in my httpsdocs folder instead of the httpdocs? I set the absolute_server_path/httpdocs/cookiejar/ folder to 997 I believe.

Also, when I post the info for login in the second part of the code, I am not sure if $LOGINURL should be [signin.example.com...] or [home.example.com?...]

Also, I am not comfortable wit cUrl at all yet, so I am doing something else wrong.

Thank you very much for all your input.

kpaul

1:10 am on Mar 27, 2005 (gmt 0)

10+ Year Member



not sure if this is the answer, but you might try setting the cookie to an actual file. make sure it's writable by the script.

also, it might help to try doing the process in a browser. at each stage (or each page) view the source so you can see how it's set-up. look for the various form elements to see what URL to point to and also what secondary variables you might need.

sticky me and i'll send you another example if you like.

Elijah

2:22 am on Mar 27, 2005 (gmt 0)

10+ Year Member



You might also find the LiveHTTPHeaders extension for Mozilla Firefox useful to see exactly what is going on when you sign in. ;)

Hugene

8:15 pm on Mar 28, 2005 (gmt 0)

10+ Year Member



so I am using liveHTTPheader now, to see exactly what's hapening. First, there is one odd thing, the headers always keep saying that the refferer is www.my-site.com/my-curls-script.php and not [signin.example.com...] like I try to force it with the code!

Also, I didnt mention that the loging page is not exactly [signin.example.com...] but something like [signin.example.com...]

when I look at the http headers during manual login, I can see that there is a GET some_random_looking_data statement, after which some cookies action occurs. This statement is totally lacking from the header responses to my code, eventhough the curl_setopt($ch, CURLOPT_URL,$LOGINURL); command includes the full url, with the some_random_looking_data in $LOGINURL.

I am starting to get discouraged here, and I find that the cUrl documentation is hard to find.