Login using cURL.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Login using cURL.

Post 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?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Login using cURL.

Post 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).
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Login using cURL.

Post 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
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Login using cURL.

Post by twinedev »

they will be on your site and logging into your site??? If so, I don't get the purpose of using cURL?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Login using cURL.

Post by social_experiment »

They'll have to login to access their profiles and other user specific information
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Login using cURL.

Post 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
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Login using cURL.

Post 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
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Login using cURL.

Post 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
Post Reply