how to use php class with html form - php

ok so i created the html form which i wish to connect with the following php class but im clueless on how to get it done because i have never worked on php class before.
here is my form
<form action="sms.php" method="post">
<input type="text" name="uid" value=""/>
<input type="password" name="pwd" value=""/>
<input type="number" name="mobile" value=""/>
<textarea cols="30" rows="10" name="msg"></textarea>
<input type="submit" name="submit" value="Send"/>
this is the php class which i need to be able to connect with the above html form
<?php
class WAY2SMSClient
{
var $curl;
var $timeout = 30;
var $jsToken;
var $way2smsHost;
var $refurl;
function login($username, $password)
{
$this->curl = curl_init();
$uid = urlencode($username);
$pwd = urlencode($password);
// Go where the server takes you :P
curl_setopt($this->curl, CURLOPT_URL, "http://way2sms.com");
curl_setopt($this->curl, CURLOPT_HEADER, true);
curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, TRUE);
$a = curl_exec($this->curl);
if (preg_match('#Location: (.*)#', $a, $r))
$this->way2smsHost = trim($r[1]);
// Setup for login
curl_setopt($this->curl, CURLOPT_URL, $this->way2smsHost . "Login1.action");
curl_setopt($this->curl, CURLOPT_POST, 1);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, "username=" . $uid . "&password=" . $pwd . "&button=Login");
curl_setopt($this->curl, CURLOPT_COOKIESESSION, 1);
curl_setopt($this->curl, CURLOPT_COOKIEFILE, "cookie_way2sms");
curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($this->curl, CURLOPT_MAXREDIRS, 20);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5");
curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, $this->timeout);
curl_setopt($this->curl, CURLOPT_REFERER, $this->way2smsHost);
$text = curl_exec($this->curl);
// Check if any error occured
if (curl_errno($this->curl))
return "access error : " . curl_error($this->curl);
// Check for proper login
$pos = stripos(curl_getinfo($this->curl, CURLINFO_EFFECTIVE_URL), "ebrdg.action");
if ($pos === "FALSE" || $pos == 0 || $pos == "")
return "invalid login";
// Set the home page from where we can send message
$this->refurl = curl_getinfo($this->curl, CURLINFO_EFFECTIVE_URL);
$newurl = str_replace("ebrdg.action?id=", "main.action?section=s&Token=", $this->refurl);
curl_setopt($this->curl, CURLOPT_URL, $newurl);
// Extract the token from the URL
$this->jstoken = substr($newurl, 50, -41);
//Go to the homepage
$text = curl_exec($this->curl);
return true;
}
function send($phone, $msg)
{
$result = array();
// Check the message
if (trim($msg) == "" || strlen($msg) == 0)
return "invalid message";
// Take only the first 140 characters of the message
$msg = substr($msg, 0, 140);
// Store the numbers from the string to an array
$pharr = explode(",", $phone);
// Send SMS to each number
foreach ($pharr as $p) {
// Check the mobile number
if (strlen($p) != 10 || !is_numeric($p) || strpos($p, ".") != false) {
$result[] = array('phone' => $p, 'msg' => $msg, 'result' => "invalid number");
continue;
}
// Setup to send SMS
curl_setopt($this->curl, CURLOPT_URL, $this->way2smsHost . 'smstoss.action');
curl_setopt($this->curl, CURLOPT_REFERER, curl_getinfo($this->curl, CURLINFO_EFFECTIVE_URL));
curl_setopt($this->curl, CURLOPT_POST, 1);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, "ssaction=ss&Token=" . $this->jstoken . "&mobile=" . $p . "&message=" . $msg . "&button=Login");
$contents = curl_exec($this->curl);
//Check Message Status
$pos = strpos($contents, 'Message has been submitted successfully');
$res = ($pos !== false) ? true : false;
$result[] = array('phone' => $p, 'msg' => $msg, 'result' => $res);
}
return $result;
}
/**
* logout of current session.
*/
function logout()
{
curl_setopt($this->curl, CURLOPT_URL, $this->way2smsHost . "LogOut");
curl_setopt($this->curl, CURLOPT_REFERER, $this->refurl);
$text = curl_exec($this->curl);
curl_close($this->curl);
}
}
function sendWay2SMS($uid, $pwd, $phone, $msg)
{
$client = new WAY2SMSClient();
$client->login($uid, $pwd);
$result = $client->send($phone, $msg);
$client->logout();
return $result;
}
?>
Appreciate your time and help

sms.php
<?php
//include WAY2SMSClient file
include_once 'WAY2SMSClient.php';
$uid = $_POST['uid'];
$pwd = $_POST['pwd'];
$phone = $_POST['mobile'];
$msg = $_POST['msg'];
$result = sendWay2SMS($uid, $pwd, $phone, $msg);
?>

You have a file named WAY2SMSCLIENT.php and you are posting your form data to sms.php.
So from your sms.php you will have to create an object of the WAY2SMSCLIENT class and then on that object you may call the methods passing them the required data as parameters.
Am sorry as I can't post the code right now as am using phone and am traveling now. But if is still hard for you let me know i will try my level best to help you.

Related

Get response from php script with curl after posting response

I have a form which uses curl to submit the apiKey to my server and then the script on my server verify the key and returns true and false. but instead of response. I'm getting Trying to access array offset on value of type null. I want to know How to get response from my server after curl submission.
Curl Submit
$post['apiKey'] = $apiKey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_URL,"https://www.pawnhost.com/phevapi/verify_api.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$json = curl_exec($ch);
$response = json_decode($json, true);
Server Script
<?php
define("ERROR_HEADER_URL", "Location: " . $_SERVER['HTTP_REFERER'] . "?error=");
require("includes/initialize.php");
if ($_SERVER['REQUEST_METHOD'] != 'POST') header(ERROR_HEADER_URL . "invalidRequest");
$postParams = allowedPOSTParams($allowed_params=['apiKey']);
if (!isset($postParams['apiKey'])) header(ERROR_HEADER_URL . "verficationFailed");
$apiKey = escape($postParams['apiKey']);
if (isInputEmpty($apiKey)) {
header(ERROR_HEADER_URL . "emptyFields");
} elseif (!$apiKey == 25) {
header(ERROR_HEADER_URL . urlencode("invalidKey"));
} else {
$response = [];
if (getApiKeyUserDetails($apiKey, $connection)) {
if (getApiKeyUserDetails($apiKey, $connection)['apiKeyUsed'] > 0) {
$response['success'] = false;
$response['error'] = 'apiKeyUsed';
} else {
makeApiKeyUsed($apiKey, $connection);
$response['success'] = true;
}
} else {
$response['success'] = false;
$response['error'] = 'invalidApiKey';
}
return json_encode($response);
}
Allowed Post Params Function:
function allowedPOSTParams($allowed_params=[]) {
$allowed_array = [];
foreach ($allowed_params as $param) {
if (isset($_POST[$param])) {
$allowed_array[$param] = $_POST[$param];
} else {
$allowed_array[$param] = NULL;
}
}
return $allowed_array;
}
Replace
curl_setopt($ch, CURLOPT_POSTFIELDS, $apiKey);
with
curl_setopt($ch, CURLOPT_POSTFIELDS, array('apiKey'=>$apiKey));
Then, you will able to find apiKey as POST parameter.

How can I scrape LinkedIn company pages with cURL and PHP?

I want to scrape some LinkedIn company pages with cURL and PHP with login Credentials. I tried this code. But I got error like
Unauthorized
You must be authenticated to access this page.
Before scraping the company page I have to sign in at LinkedIn with a personal account via cURL, but it doesn't seems to work.
Instead of using simple_html_dom we used above fetch_value.
function fetch_value($str, $find_start = '', $find_end = '') {
if ($find_start == '') {
return '';
}
$start = strpos($str, $find_start);
if ($start === false) {
return '';
}
$length = strlen($find_start);
$substr = substr($str, $start + $length);
if ($find_end == '') {
return $substr;
}
$end = strpos($substr, $find_end);
if ($end === false) {
return $substr;
}
return substr($substr, 0, $end);
}
$linkedin_login_page = "https://www.linkedin.com/uas/login";
$linkedin_ref = "https://www.linkedin.com";
$username = 'username';
$password = 'password';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $linkedin_login_page);
curl_setopt($ch, CURLOPT_REFERER, $linkedin_ref);
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7)');
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
$login_content = curl_exec($ch);
if (curl_error($ch)) {
echo 'error:' . curl_error($ch);
}
$var = array(
'isJsEnabled' => 'false',
'source_app' => '',
'clickedSuggestion' => 'false',
'session_key' => trim($username),
'session_password' => trim($password),
'signin' => 'Sign In',
'session_redirect' => '',
'trk' => '',
'fromEmail' => ''
);
$var['loginCsrfParam'] = fetch_value($login_content, 'type="hidden" name="loginCsrfParam" value="', '"');
$var['csrfToken'] = fetch_value($login_content, 'type="hidden" name="csrfToken" value="', '"');
$var['sourceAlias'] = fetch_value($login_content, 'input type="hidden" name="sourceAlias" value="', '"');
$post_array = array();
foreach ($var as $key => $value) {
$post_array[] = urlencode($key) . '=' . urlencode($value);
}
$post_string = implode('&', $post_array);
curl_setopt($ch, CURLOPT_URL, "https://www.linkedin.com/uas/login-submit");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
$store = curl_exec($ch);
if (stripos($store, "session_password-login-error") !== false) {
$err = trim(strip_tags(fetch_value($store, '<span class="error" id="session_password-login-error">', '</span>')));
echo "Login error : ".$err;
} elseif (stripos($store, 'profile-nav-item') !== false) {
curl_setopt($ch, CURLOPT_URL, 'https://www.linkedin.com/company-beta/10667/?pathWildcard=10667');
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, "");
$content = curl_exec($ch);
curl_close($ch);
echo $content;
} else {
echo "unknown error";
}
Any suggestion please help?
Thanks!

How to integrate way2sms in php?

I would like to integrate way2sms api in php code.
here my sample code , its error free but no message is sending . friends please help me where i have did mistake ? what do i need to change ?
sms.php
$uid='9876543210';//10 digit mobile number
$pwd='password';
$phone='9876543210';
$msg='from way 2 sms master ' ;
include ('way2sms-api.php');
$res= sendWay2SMS ( $uid , $pwd , $phone , $msg);
way2sms-api.php
<?php
function sendWay2SMS($uid, $pwd, $phone, $msg)
{
$curl = curl_init();
$timeout = 30;
$result = array();
$uid = urlencode($uid);
$pwd = urlencode($pwd);
$autobalancer = rand(1, 8);
curl_setopt($curl, CURLOPT_URL, http://site".$autobalancer.".way2sms.com/Login1.action");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, "username=".$uid."&password=".$pwd."&button=Login");
//curl_setopt($curl , CURLOPT_PROXY , '144.16.192.218:8080' );
curl_setopt($curl, CURLOPT_COOKIESESSION, 1);
curl_setopt($curl, CURLOPT_COOKIEFILE, "cookie_way2sms");
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_MAXREDIRS, 20);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5");
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_REFERER, "http://site".$autobalancer.".way2sms.com/");
$text = curl_exec($curl);
// Check if any error occured
if (curl_errno($curl))
return "access error : ". curl_error($curl);
// Check for proper login
$pos = stripos(curl_getinfo($curl, CURLINFO_EFFECTIVE_URL), "Main.action");
if ($pos === "FALSE" || $pos == 0 || $pos == "")
return "invalid login";
if (trim($msg) == "" || strlen($msg) == 0)
return "invalid message";
$msg = urlencode(substr($msg, 0, 160));
$pharr = explode(",", $phone);
$refurl = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL);
curl_setopt($curl, CURLOPT_REFERER, $refurl);
curl_setopt($curl, CURLOPT_URL,
"http://site".$autobalancer.".way2sms.com/jsp/InstantSMS.jsp");
$text = curl_exec($curl);
preg_match_all('/<input[\s]*type="hidden"[\s]*name="Action"[\s]*id="Action"[\s]*value="?([^>]*)?"/si', $text, $match);
$action = $match[1][0]; // get custid from the form fro the Action field in the post form
foreach ($pharr as $p)
{
if (strlen($p) != 10 || !is_numeric($p) || strpos($p, ".") != false)
{
$result[] = array('phone' => $p, 'msg' => urldecode($msg), 'result' => "invalid number");
continue;
}
$p = urlencode($p);
// Send SMS
curl_setopt($curl, CURLOPT_URL, 'http://site'.$autobalancer.'.way2sms.com/quicksms.action');
curl_setopt($curl, CURLOPT_REFERER, curl_getinfo($curl, CURLINFO_EFFECTIVE_URL));
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS,
"HiddenAction=instantsms&bulidgpwd=*******&bulidguid=username&catnamedis=Birthday&chkall=on&gpwd1=*******&guid1=username&ypwd1=*******&yuid1=username&Action=".
$action."&MobNo=".$p."&textArea=".$msg);
$contents = curl_exec($curl);
//Check Message Status
//preg_match_all('/<span class="style1">?([^>]*)?<\/span>/si', $contents, $match);
//$out=str_replace(" ","",$match[1][0]);
$pos = strpos($contents, 'Message has been submitted successfully');
$res = ($pos !== false) ? true : false;
$result[] = array('phone' => $p, 'msg' => urldecode($msg), 'result' => $res);
}
//echo $text;
// Logout
curl_setopt($curl, CURLOPT_URL, "http://site".$autobalancer.".way2sms.com/LogOut");
curl_setopt($curl, CURLOPT_REFERER, $refurl);
$text = curl_exec($curl);
curl_close($curl);
return $result;
}
?>
TRY THIS CODE:
<?php
/**
* https://github.com/kingster/Way2SMS-API/blob/master/way2sms-api.php
*
* Please use this code on your own risk. The author is no way responsible for the outcome arising out of this
* Good Luck!
**/
class WAY2SMSClient
{
var $curl;
var $timeout = 30;
var $jsToken;
var $way2smsHost;
var $refurl;
/**
* #param $username
* #param $password
* #return bool|string
*/
function login($username, $password)
{
$this->curl = curl_init();
$uid = urlencode($username);
$pwd = urlencode($password);
// Go where the server takes you :P
curl_setopt($this->curl, CURLOPT_URL, "http://way2sms.com");
curl_setopt($this->curl, CURLOPT_HEADER, true);
curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, TRUE);
$a = curl_exec($this->curl);
if (preg_match('#Location: (.*)#', $a, $r))
$this->way2smsHost = trim($r[1]);
// Setup for login
curl_setopt($this->curl, CURLOPT_URL, $this->way2smsHost . "Login1.action");
curl_setopt($this->curl, CURLOPT_POST, 1);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, "username=" . $uid . "&password=" . $pwd . "&button=Login");
curl_setopt($this->curl, CURLOPT_COOKIESESSION, 1);
curl_setopt($this->curl, CURLOPT_COOKIEFILE, "cookie_way2sms");
curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($this->curl, CURLOPT_MAXREDIRS, 20);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5");
curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, $this->timeout);
curl_setopt($this->curl, CURLOPT_REFERER, $this->way2smsHost);
$text = curl_exec($this->curl);
// Check if any error occured
if (curl_errno($this->curl))
return "access error : " . curl_error($this->curl);
// Check for proper login
$pos = stripos(curl_getinfo($this->curl, CURLINFO_EFFECTIVE_URL), "ebrdg.action");
if ($pos === "FALSE" || $pos == 0 || $pos == "")
return "invalid login";
// Set the home page from where we can send message
$this->refurl = curl_getinfo($this->curl, CURLINFO_EFFECTIVE_URL);
$newurl = str_replace("ebrdg.action?id=", "main.action?section=s&Token=", $this->refurl);
curl_setopt($this->curl, CURLOPT_URL, $newurl);
// Extract the token from the URL
$this->jstoken = substr($newurl, 50, -41);
//Go to the homepage
$text = curl_exec($this->curl);
return true;
}
/**
* #param $phone
* #param $msg
* #return array
*/
function send($phone, $msg)
{
$result = array();
// Check the message
if (trim($msg) == "" || strlen($msg) == 0)
return "invalid message";
// Take only the first 140 characters of the message
$msg = substr($msg, 0, 140);
// Store the numbers from the string to an array
$pharr = explode(",", $phone);
// Send SMS to each number
foreach ($pharr as $p) {
// Check the mobile number
if (strlen($p) != 10 || !is_numeric($p) || strpos($p, ".") != false) {
$result[] = array('phone' => $p, 'msg' => $msg, 'result' => "invalid number");
continue;
}
// Setup to send SMS
curl_setopt($this->curl, CURLOPT_URL, $this->way2smsHost . 'smstoss.action');
curl_setopt($this->curl, CURLOPT_REFERER, curl_getinfo($this->curl, CURLINFO_EFFECTIVE_URL));
curl_setopt($this->curl, CURLOPT_POST, 1);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, "ssaction=ss&Token=" . $this->jstoken . "&mobile=" . $p . "&message=" . $msg . "&button=Login");
$contents = curl_exec($this->curl);
//Check Message Status
$pos = strpos($contents, 'Message has been submitted successfully');
$res = ($pos !== false) ? true : false;
$result[] = array('phone' => $p, 'msg' => $msg, 'result' => $res);
}
return $result;
}
/**
* logout of current session.
*/
function logout()
{
curl_setopt($this->curl, CURLOPT_URL, $this->way2smsHost . "LogOut");
curl_setopt($this->curl, CURLOPT_REFERER, $this->refurl);
$text = curl_exec($this->curl);
curl_close($this->curl);
}
}
/**
* Helper Function to send to sms to single/multiple people via way2sms
* #example sendWay2SMS ( '9000012345' , 'password' , '987654321,9876501234' , 'Hello World')
*/
function sendWay2SMS($uid, $pwd, $phone, $msg)
{
$client = new WAY2SMSClient();
$client->login($uid, $pwd);
$result = $client->send($phone, $msg);
$client->logout();
return $result;
}

Retrieve Android Market mylibrary with curl

I am trying to retrieve this page using curl in php. This page of course requires you to log in because it displays different apps for each user. I have been following the work done on this page, however am not having much success.
So far, in his example I am able to successfully populate the auth variable with the auth token. In the next step however (Below the comment for logging into Android Market) I run into troubles. The output variable that he says should have a 302 code results in a "The document has moved" page which links me back to the Google log in page.
Here is a pastebin to show exactly what I am trying. http://pastebin.com/9Fs9GWxk
Additionally if anyone knows what steps I need to do after this to actually get the page I need that would be amazing. Thanks
Here is something I came up with today for this question that has been modified to work for you:
<?php
$USERNAME = 'you#gmail';
$PASSWORD = 'yourpasswd';
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Ubuntu; X11; Linux x86_64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR);
curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIEJAR);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_URL,
'https://accounts.google.com/ServiceLogin?hl=en&continue=https://market.android.com/mylibrary');
$data = curl_exec($ch);
$formFields = getFormFields($data);
$formFields['Email'] = $USERNAME;
$formFields['Passwd'] = $PASSWORD;
unset($formFields['PersistentCookie']);
// var_dump($formFields);
$post_string = '';
foreach($formFields as $key => $value) {
$post_string .= $key . '=' . urlencode($value) . '&';
}
$post_string = substr($post_string, 0, -1);
curl_setopt($ch, CURLOPT_URL, 'https://accounts.google.com/ServiceLoginAuth');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
$result = curl_exec($ch);
//var_dump($result);
if (preg_match('/^2\d{2}/', curl_getinfo($ch, CURLINFO_HTTP_CODE)) == false) {
die("Login failed");
var_dump(curl_getinfo($ch), $result);
} else {
curl_setopt($ch, CURLOPT_URL, 'https://market.android.com/mylibrary');
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_HTTPGET, true);
$result = curl_exec($ch);
echo $result;
}
function getFormFields($data)
{
if (preg_match('/(<form id=.?gaia_loginform.*?<\/form>)/is', $data, $matches)) {
$inputs = getInputs($matches[1]);
return $inputs;
} else {
die('didnt find login form');
}
}
function getInputs($form)
{
$inputs = array();
$elements = preg_match_all('/(<input[^>]+>)/is', $form, $matches);
if ($elements > 0) {
for($i = 0; $i < $elements; $i++) {
$el = preg_replace('/\s{2,}/', ' ', $matches[1][$i]);
if (preg_match('/name=(?:["\'])?([^"\'\s]*)/i', $el, $name)) {
$name = $name[1];
$value = '';
if (preg_match('/value=(?:["\'])?([^"\'\s]*)/i', $el, $value)) {
$value = $value[1];
}
$inputs[$name] = $value;
}
}
}
return $inputs;
}

Login validation error on website (cURL)

I tried to create a login validation linked to the website, but is not working.
Here's my code:
public function login() {
$username = $this->input->post('login', 'true');
$password = $this->input->post('password', 'true');
if($this->ci_curl->loginIdws($username, $password) == false) {
echo "<script> alert('Invalid Username & Password');
window.location.href = 'index';
< / script>";
}
else
{
$this->load->view('curl/thread');
}
}
When the login fails, everything is okay, but when login is successful, the page will show alert('Invalid Username & Password') ... How can I fix this?
Here's my library:
public function loginIdws($username, $password) {
$url = 'http://forum.idws.id/login/login';
$data = '_xfToken=&cookie_check=1&login='.$username.'&password='.$password.'&redirect=http://forum.idws.id/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_TIMEOUT, 40000);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_NOBODY, FALSE);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; rv:45.0) Gecko/20100101 Firefox/45.0");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $_SERVER['REQUEST_URI']);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$baca = curl_exec($ch);
if($baca == = FALSE) {
die(curl_error($ch));
}
$info = curl_getinfo($ch);
curl_setopt($ch, CURLOPT_URL, "http://forum.idws.id/account/");
curl_setopt($ch, CURLOPT_POST, 0);
$baca2 = curl_exec($ch);
curl_close($ch);
$pecah1 = explode('<fieldset>', $baca2);
$pecah2 = explode('</fieldset>', $pecah1[1]);
echo "<fieldset>";
echo $pecah2[0];
echo "</fieldset>";
}
and this is my view:
< ? php echo form_open('curly/login'); ? >
<label>Username :< / label>
<input id = "name" name = "login" type = "text">
<label>Password : < / label>
<input id = "password" name = "password" type = "password">
<input type = "submit" name = "submit" id = "submit" value = "Login">
Your current problem is caused by this:
if($this->ci_curl->loginIdws($username,$password)==false){
You are comparing the return value of your method to false. As you do not return anything from your function, that condition will always match.
You need to add some logic to the end of your method to return the correct value:
public function loginIdws($username ,$password){
// your code
...
// written out for clarity
if (check_if_login_successful) {
return true;
else {
return false;
}
}

Categories