Login using curl and save session

Hi

I wan to login using curl and I know how to post data using CURLOPT_POST.
after post i want to save session and then browse using that session can anyone help me out in it.

First I want to login, then I want to save session so that i can use it for next steps.

here is code for login using post method.


 $ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "http://127.0.0.1/post.php"); 
curl_setopt($ch, CURLOPT_POST);
curl_setopt($ch, CURLOPT_POSTFIELDS, "login=admin&pass=test");
curl_exec($ch);

First, define a cookie jar, to store the sessionid cookie between requests.
http://www.php.net/manual/en/function.curl-setopt.php

CURLOPT_COOKIEJAR The name of a file to save all internal cookies to when the connection closes.

Second, what is the problem ?

Like this:

$ch = curl_init("http://127.0.0.1/i4a/ams/amslogin.cfm");
curl_setopt($ch, CURLOPT_POST);
curl_setopt($ch, CURLOPT_POSTFIELDS, "username=14962&password=Sici14962");
curl_setopt($ch, CURLOPT_COOKIEJAR, "file.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "file.txt");
curl_exec($ch);

It the login and password both are correct then I want to go to page http://127.0.0.1/mysecure.php page what should I do for this?

Curl send requests. It can catch the output, but it’s barely all.
If you want to mimick a browser session, then you have to realize as much Curl calls than you want to simulate link clicks.

Send your post to login.
Then send a new request for a given page, as you need.
You can have curl giving you back the html source of the page by including this:


$ch = curl_init("http://127.0.0.1/i4a/ams/amslogin.cfm");
curl_setopt($ch, CURLOPT_POST);
curl_setopt($ch, CURLOPT_POSTFIELDS, "username=14962&password=Sici14962");
curl_setopt($ch, CURLOPT_COOKIEJAR, "file.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "file.txt");
[B]curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);[/B]
[B]$html[/B]=curl_exec($ch); 

Then, it’s up to you to parse this html.

Great

thanks I will try it now :slight_smile:

I have question here.

when this code will get executed and after execution there is page redirect call what will happen? will it go to that page?

Here is my code:

	$url = "http://127.0.0.1/amslogin.cfm?nextPage=/i4a/ams/amslogin.cfm?pageid=3286&%20CFID=5621833&CFTOKEN=8c3da5edca2fa1ca-FAAE3F6D-15C5-F08A-6C588A7D51F0D4C7&jsessionid=82306d1bf8ac58106f3a";

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);	
	curl_setopt($ch, CURLOPT_POST,true);
	curl_setopt($ch, CURLOPT_POSTFIELDS, "submit=Login&username=MyLogin&password=MyPass&nextpage=/i4a/ams/amslogin.cfm?pageid=3286&nextparams=");
	curl_setopt($ch, CURLOPT_COOKIEJAR, "file.txt");
	curl_setopt($ch, CURLOPT_COOKIEFILE, "file.txt");	
	$html=curl_exec($ch);
	echo $html;
	exit;

it takes me to next page “http://127.0.0.1/i4a/ams/amslogin.cfm?pageid=3286” and says:

The requested URL /i4a/ams/amslogin.cfm was not found on this server.

Is there a way I can come to know if I am logged in successfully or not?

There’s a lot of options you can use. Have a read through them, and you should be able to get ideas on how to go about doing something.

http://www.php.net/manual/en/function.curl-setopt.php

I tried this code and got:
Curl error: 1

	$url = "http://www.dizyn.com/demo/admin/act_index.php";
	$data = "login=admin&pass=test&Submit= Login ";
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);	
	curl_setopt($ch, CURLOPT_POST,true);
	curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
	curl_setopt($ch, CURLOPT_COOKIEJAR, "file.txt");
	curl_setopt($ch, CURLOPT_COOKIEFILE, "file.txt");	
	if($html = curl_exec($ch) == false)
		echo 'Curl error: ' . curl_error($ch);
	else
	{
		echo 'Operation completed without any errors1';
		
		curl_setopt($ch, CURLOPT_URL, 'http://www.dizyn.com/demo/admin/adminhome.php?log=welcome');
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_COOKIEJAR, "file.txt");
		curl_setopt($ch, CURLOPT_COOKIEFILE, "file.txt");
		
		
		if($html = curl_exec($ch) == false)
		{
			echo 'Curl error: ' . curl_error($ch);
		}
		else
		{
			echo 'Operation completed without any errors2';
		}
	}
//	$html=curl_exec($ch);
	echo $html;
	exit;

Just because $html is empty doesn’t mean there was an error. You check for an error using curl_error() or curl_errno(), or you can strictly check for boolean false.

Remember, in php, an empty string, or string zero ‘0’ evaluates to false when you use the loose comparison operator == . If you don’t want that, you need to use strict comparison using either === or !==
See http://www.php.net/manual/en/types.comparisons.php

You’re also trying to get a bit too fancy with your assignments and expressions.
This


if($html = curl_exec($ch) == false)

Is the same as writing this


if (curl_exec($ch) == false) {
    $html = true;
} else {
    $html = false;
}

if ($html) {
   ...

You probably meant something like this, although theres other ways to do it.


if(false === $html = curl_exec($ch)) {