网站上的登录验证错误(cURL)

时间:2016-04-28 08:25:47

标签: php codeigniter validation curl login

我尝试创建链接到网站的登录验证,但无效。 这是我的代码:

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');
    }
}

当登录失败时,一切正常,但登录成功后,页面将显示alert('Invalid Username & Password') ...如何解决此问题?

这是我的图书馆:

    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>";
}

这是我的观点:

< ? 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">

1 个答案:

答案 0 :(得分:0)

您当前的问题是由此引起的:

if($this->ci_curl->loginIdws($username,$password)==false){

您正在将方法的返回值与false进行比较。由于您没有从函数返回任何内容,因此该条件将始终匹配。

您需要在方法的末尾添加一些逻辑以返回正确的值:

public function loginIdws($username ,$password){

    // your code

    ...

    // written out for clarity
    if (check_if_login_successful) {
        return true;
    else {
        return false;
    }
}