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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失