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


returning login status in php [CURL]

你好吗?
我有一个非预期的结果,
登录成功时应返回"1",登录错误时应返回"0",

它总是返回"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"));
?>

谢谢你:)

为此不需要

将输出写入文件,并且会浪费 I/O。对 admincp 索引的第二个请求也可以保存以检查登录,因为 wordpress 会在登录失败时再次打印 log通知。所以我修改了你的示例,如下所示:

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

但对我来说,这似乎不是一个好方法。根据您要集成此检查的位置,编写一个小插件进行检查或集成wordpress将是一个更好的解决方案: http://codex.wordpress.org/Integrating_WordPress_with_Your_Website 然后您可以使用wordpress函数来检查凭据,例如 https://codex.wordpress.org/Function_Reference/wp_check_password