0

i try to login through website using cURL, i want to give validation login error. when i fail to login the page will show alert, if succesful to login the page still show alert. . what's wrong with my code, any solution? thanks Here's my library code:

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){
                    return false;
                }else 
                {
                    return true;
                }
                $info = curl_getinfo($ch);
                curl_setopt($ch,CURLOPT_URL,"http://forum.idws.id/account/");
                curl_setopt($ch,CURLOPT_POST, 0);
                $baca2 = curl_exec($ch);
                return false;

                curl_close($ch);
                $pecah1 = explode('<fieldset>',$baca2);
                $pecah2 = explode('</fieldset>',$pecah1[1]);
                echo "<fieldset>";
                echo $pecah2[0];
                echo "</fieldset>";
                                }

this is my controller code :

public function loginn(){
                $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');
                    return true;    
                }
            }
Bobby Pratama
  • 96
  • 1
  • 2
  • 13

2 Answers2

1

codes after never executed,

 $baca = curl_exec($ch);
                if ($baca=== false){
                    return false;
                }else 
                {
                    return true;
                }

because you return result anywhere
also curl_exec() can return different result dependent on your configuration read more Anthony Hatzopoulos answer

I suggest use Requests for web requests in php

Community
  • 1
  • 1
Amir Khorsandi
  • 3,542
  • 1
  • 34
  • 38
0

First of all check the response, what's it is returning, my guess, it is returning 'true'/'false', so $response==false is not same as say, $response=='false'. So, you either need to check the value or convert it to boolean before doing a strict check, something like:

$baca = curl_exec($ch);
$loginOk = $baca === 'true'? true: false;
return $loginOk;

OR do:

    $baca = curl_exec($ch);

    if ($baca == 'false'){
        return false;
    } else {
        return true;
    }

and it should as expected.

Ravish
  • 2,383
  • 4
  • 37
  • 55