简体   繁体   English

为什么我的函数登录Curl总是返回true? CodeIgniter

[英]why does my function login Curl always return true? CodeIgniter

i try to login through website using cURL, i want to give validation login error. 我尝试使用cURL通过网站登录,我想给出验证登录错误。 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;    
                }
            }

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 curl_exec()还可根据您的配置返回不同的结果。阅读更多Anthony Hatzopoulos答案

I suggest use Requests for web requests in php 我建议使用php中的Web请求使用Requests

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' . 首先检查响应,返回的是什么,我猜是返回'true'/'false',所以$response==false与说的是$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. 并且应该如预期的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM