Page 1 of 1

login using curl and save session

Posted: Wed Mar 11, 2009 7:31 am
by itsmani1
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.

Code: Select all

 
 $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);

Re: login using curl and save session

Posted: Wed Mar 11, 2009 7:46 am
by php_east
sessions are saved using cookies and cookie jars. just pass the whole jar onwards.

Re: login using curl and save session

Posted: Wed Mar 11, 2009 8:00 am
by itsmani1
Like this:

Code: Select all

$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?

Re: login using curl and save session

Posted: Thu Mar 12, 2009 12:38 am
by php_east
itsmani1 wrote: 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?
nothing. it's already taken care.
the cookies will follow and you can use your secure.php to check for it.
is that what you are asking ?

if you are wondering how you would redirect to mysecure.php, just forget about cURL for one moment and code as usual i.e. header(location:...). think of cURL as browser emulator.

Re: login using curl and save session

Posted: Thu Mar 12, 2009 10:41 am
by itsmani1
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:

Code: Select all

    $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?

Re: login using curl and save session

Posted: Thu Mar 12, 2009 3:19 pm
by php_east
itsmani1 wrote: when this code will get executed and after execution there is page redirect call what will happen? will it go to that page?
that is up to you. { CURLOPT_FOLLOWLOCATION }
itsmani1 wrote: 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.
maybe he is right...=> nextpage=/i4a/ams/amslogin.cfm?pageid=3286&nextparams......
so use absolute addressing.
itsmani1 wrote: Is there a way I can come to know if I am logged in successfully or not?
umm, let me see, if ($logged_in) echo ' login successsful '; ?

Re: login using curl and save session

Posted: Tue Mar 17, 2009 6:21 am
by itsmani1
I tried this code and got error:

Curl error: 1

Code: Select all

    $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;