Page 1 of 1

Login using cURL.

Posted: Mon Nov 07, 2011 12:26 am
by social_experiment
I want to create a script that uses cURL to log in a user and i've search around on how to do this. There are plenty of examples but the 'how' still escapes me;
all the scripts i've found are similar to the example code below

Code: Select all

<?php
 $loginUrl = 'username=' . $_POST['username'] . '&';
 $loginUrl .= 'password=' . $_POST['password'] . '&';
 $loginUrl .= 'loginBtn=TRUE';
 $url = 'login.php';

 $curl = curl_init();
 //
 curl_setopt($curl, CURLOPT_URL, $url);
 curl_setopt($curl, CURLOPT_FAILONERROR, 1);
 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($curl, CURLOPT_POST, true); 
 // 
 curl_setopt($curl, CURLOPT_POSTFIELDS, $loginUrl);

 $result = curl_exec($curl);
 //

 if (curl_errno($curl)) {
  echo 'Curl error: ' . curl_error($curl);
  exit();
 }
?>
My question(s):
1. How do i 'call' this page. Currently i have a form that goes to 'login.php' which contains the above sample of code but if i try to load the page containing the form; the page keeps on loading. My knowledge of cURL (and it's use) are limited to what i've read in the php manual.
2. A cURL call like this, how does it compare against a normal POST-ing of login information? Assume that no https will be used, is using cURL anymore secure than just using $_POST?

Re: Login using cURL.

Posted: Mon Nov 07, 2011 1:49 am
by twinedev
Curl is basically a programmed way to interact with a (web) server instead of using a browser to do so. The main thing to understand before giving you advice is to know what exactly are you trying to accomplish?

For most sites that you are "logging into" via curl, you will need a way to to handle the session (most likely, but programming it to accept a cookie from the server you are logging into, and then make sure all calls after that send the cookie information as well. Again, I haven't had to do this, so can't give much in examples on code for it.

-Greg

(Oh, and you CAN do https via Curl, that is how many sites handle payment processing requests to places like Authorize.Net, etc).

Re: Login using cURL.

Posted: Mon Nov 07, 2011 2:05 am
by social_experiment
twinedev wrote:The main thing to understand before giving you advice is to know what exactly are you trying to accomplish?
A simple login, user will enter a username & password which will be checked against a database and on success be re-directed to a specific page

Re: Login using cURL.

Posted: Mon Nov 07, 2011 2:29 am
by twinedev
they will be on your site and logging into your site??? If so, I don't get the purpose of using cURL?

Re: Login using cURL.

Posted: Mon Nov 07, 2011 3:10 am
by social_experiment
They'll have to login to access their profiles and other user specific information

Re: Login using cURL.

Posted: Mon Nov 07, 2011 3:54 am
by twinedev
I'm still not getting it, why use cURL? That would be just doubling up on everything. You would need somehow establish a session with the user, so your script that makes curl calls knows how to tell the difference from one to another, then you will have to program the code around curl to handle holding the session between curl and a call right back to your own site.

Seems more trouble than is worth. Nothing seems to be automated/masked, two of the main reasons to use cURL for something.

-Greg

Re: Login using cURL.

Posted: Mon Nov 07, 2011 4:50 am
by social_experiment
twinedev wrote:Seems more trouble than is worth. Nothing seems to be automated/masked, two of the main reasons to use cURL for something.
Ok, like i mentioned my knowledge of cURL is very limited (as i demonstrated in this topic :) ) so i didn't know if it would be a better / worse choice to use than plain old POST.

Thanks for the feedback on the topic

Re: Login using cURL.

Posted: Mon Nov 07, 2011 4:24 pm
by twinedev
Yeah, for just your site, use POST. At first I thought you were asking about making it so people go to your URL, then that takes them off to another site (kinda like and anonymizer site).

Like I mentioned, think of cURL as a programmed web browser request. (sort of like an Ajax call with js)

-Greg