login using curl and save session

Discussion in 'PHP' started by dizyn, Mar 11, 2009.

  1. #1
    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);
    PHP:

     
    dizyn, Mar 11, 2009 IP
  2. dizyn

    dizyn Active Member

    Messages:
    251
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #2
    I tried 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);
    
    PHP:
    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?
     
    dizyn, Mar 11, 2009 IP
  3. dizyn

    dizyn Active Member

    Messages:
    251
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #3
    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;
    
    PHP:
    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?
     
    dizyn, Mar 12, 2009 IP
  4. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #4
    Try this and tell me what it does.

    $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");   
       curl_exec($ch);
    
        curl_setopt($ch, CURLOPT_URL, 'http://next_website_to_load.com');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    
        curl_setopt($ch, CURLOPT_COOKIEJAR, "file.txt");
        curl_setopt($ch, CURLOPT_COOKIEFILE, "file.txt");   
    
    
        $html=curl_exec($ch);
    
    PHP:
     
    exodus, Mar 12, 2009 IP
  5. dizyn

    dizyn Active Member

    Messages:
    251
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #5
    I tired this code:
    and output is: Curl error: 1


    	$url = "http://www.dizyn.com/demo/admin/act_index.php";
    	$data = "login=admin&pass=redred&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.admin.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;
    
    PHP:
     
    dizyn, Mar 17, 2009 IP