dongyinju5977 2015-06-10 23:18
浏览 11
已采纳

在php [CURL]中返回登录状态[关闭]

how are you?
i have a non expected result ,
it should return "1" when login successfuly , and "0" when login error,

and it always return "0"

<?php
$username = 'admin';
$password = 'admin';
$loginUrl = 'http://localhost/wordpress/wp-login.php';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $loginUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'log='.$username.'&pwd='.$password);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$store = curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, 'http://localhost/wordpress/wp-admin/index.php');
$content = curl_exec($ch);
curl_close($ch);
file_put_contents('3.php', $content);

// if "Password" appear then login error;
echo preg_match("/Password/",file_get_contents("3.php"));

?>

and Thank you :)

  • 写回答

1条回答 默认 最新

  • dpjo15650 2015-06-11 00:04
    关注

    Writing the output to a file is not necessary for this and will waste I/O. The second request to the index of the admincp can also saved for checking the login only because wordpress will print the loginform again when the login failed. So I modified your example like the following:

    $username = 'admin';
    $password = 'admin';
    $loginUrl = 'http://localhost/wordpress/wp-login.php';
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $loginUrl);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, 'log=' . $username . '&pwd=' . $password);
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $store = curl_exec($ch);
    curl_close($ch);
    
    if(strpos($store, 'loginform') !== false) {
        echo 'Login not correct';
    }else {
        echo 'Login ok';
    }
    

    But for me this seems not to be a good way of doing this. Depending on where you want to integrate this checks it will be a better solution to write a small plugin for the check or maybe integratin wordpress: http://codex.wordpress.org/Integrating_WordPress_with_Your_Website Then you can use the wordpress-functions to check the credentials like https://codex.wordpress.org/Function_Reference/wp_check_password

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记