• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 528
  • Last Modified:

PHP & CURL to Login to a website

Hi there, I used to use a script called snoopyclass to do some html scraping but i want to switch over to curl.

I'm trying to auto login to a website with the following parameters (obviously ive replaced the email and password for posting this. Yet when i execute and echo the result, its not logging in at all. Can you point out where im going wrong?



Cheers

James
//Login to site
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://trophymanager.com/index.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "email=$email&password=$pass&login_submit=login_submit&remember=0"); 
curl_setopt($ch, CURL_COOKIEFILE, '');  // Enables session support
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_REFERER, "http://trophymanager.com/index.php");
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_FOLLOWLOCATION, 1);
//curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 500);
//curl_setopt($ch, CURLOPT_TIMEOUT, 500);

$test = curl_exec($ch);
echo "Test right here<BR><BR>";
echo $test;

Open in new window

0
Delerium1978
Asked:
Delerium1978
  • 2
  • 2
1 Solution
 
xtermCommented:
Please change line #3 to what I have below (AKA, remove index.php) and comment out line #6 for now (it generates warnings).

If I do that, I can at least get the page to return "Log In Error" in the output.
curl_setopt($ch, CURLOPT_URL, "http://trophymanager.com/");

Open in new window

0
 
Delerium1978Author Commented:
No change for me.
0
 
xtermCommented:
If you run it the way I propose above, do you get "Log In Error" in the output?  What exactly is returned?

I created a login on that site, and then did exactly this and I'm logged in.  The output that I get back looks like this:
http://tinyurl.com/3ls8q5k

Below is my exact curl script, with the password & email changed.
#!/usr/bin/php -q
<?php
//Login to site
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://trophymanager.com");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "email=my@email.address&password=mypassword&login_submit=login_submit&remember=0"); 
#curl_setopt($ch, CURL_COOKIEFILE, '');  // Enables session support
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_REFERER, "http://trophymanager.com/index.php");
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_FOLLOWLOCATION, 1);
//curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 500);
//curl_setopt($ch, CURLOPT_TIMEOUT, 500);

$test = curl_exec($ch);
echo "Test right here<BR><BR>";
echo $test;

?>

Open in new window

0
 
Delerium1978Author Commented:
Well i commented out the wrong line :) Commented out cookiejar rather than cookie file. Additionally, i still had the '/' on the end of the url.

As soon as I changed it, it worked perfectly.

Many thanks xterm for pointing out my sillyness :)

James
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now