,

Login into remote website using PHP cURL

First let me tell you I am not a programmer, but I am able to understand a little bit of code to edit the code. I have a WordPress website and I wanted a code that can login to remote website and tell the user if the login credentials are correct or not.

I found a working PHP cURL script here on this site. The script has all the needed explanation, so it should make it easy to understand even for novice. I changed code a little to suit my needs. Here is the code after changing.

<html><head>
<title>Test</title></head>
<body>

<?php

$username='off2oz';
$password='auntiem';
$postdata = 'user='.$username.'&pass='.$password;

// INIT CURL
$ch = curl_init();

// SET URL FOR THE POST FORM LOGIN
curl_setopt($ch, CURLOPT_URL, 'http://www.hotfile.com/login.php');

// ENABLE HTTP POST
curl_setopt ($ch, CURLOPT_POST, 1);

// SET POST PARAMETERS : FORM VALUES FOR EACH FIELD
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);

// IMITATE CLASSIC BROWSER'S BEHAVIOUR : HANDLE COOKIES
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');

# Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL
# not to print out the results of its query.
# Instead, it will return the results as a string return value
# from curl_exec() instead of the usual true/false.
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

// EXECUTE 1st REQUEST (FORM LOGIN)
$store = curl_exec ($ch);

// CLOSE CURL
curl_close ($ch);

if ($store=='') {

//username and password are valid
//if login is valid, hotfile website returns nothing
echo '<br><i>Login Works!! Enjoy.</i>';

} else {

//username and password are not valid
//if username or password is invalid, the website returns
//invalid username or password page
echo '<br><i>Login does not work anymore. Try another one.</i>';

}

?>

</body></html>

Code worked quite well. But then I thought of limited number of login attempt one can have on the remote website. So I never actually used this code on my website. I have posted this code here because this could help people like me who are in need of this functionality.


Edit to make code work for you (this section is for novice):


Here in the code you will need to edit the POST FORM LOGIN URL.

// SET URL FOR THE POST FORM LOGIN
curl_setopt($ch, CURLOPT_URL, 'http://www.hotfile.com/login.php');

Here I have used Hotfile post form login. Where you will find URL that is used to login into Hotfile website. Open www.hotfile.com, Look at the login form on the site.


Hotfile.com login form


Now, if you are on Firefox go to Tools > Web Developer > Page Source. Find Username: and look for form tag before and after Username:. See the screenshot below.


Hotfile post login form code


Now take note of value of action=”/login.php” in the code, that value is used is in code below.

// SET URL FOR THE POST FORM LOGIN
curl_setopt($ch, CURLOPT_URL, 'http://www.hotfile.com/login.php');


Now we need to look at $postdata = 'user='.$username.'&pass='.$password; where user is taken from name value in the input tag.

<p> <label>Username:</label> <input name="user" type="text" class="textfield" /> </p>

This will be different for different sites so the same user is placed in $postdata = 'user='.$username.'&pass='.$password; And similar goes for pass in the code. It is taken from name value in the input tag.


After adding the required values run this code.

Leave a Reply

Every comment is moderated. Keyword titled comments are deleted.