discuz调用函数实现同步登录与退出核心代码

Discuz同步登录与退出功能的核心代码如下:

1. 登录函数

function connect($username, $password) {
    $url = 'https://xxx.com/member.php?mod=logging&action=login&loginsubmit=yes&cookietime=2592000';
    $post_data = array(
        'fastloginfield' => 'username',
        'username' => $username,
        'password' => $password,
        'questionid' => 0,
        'answer' => ''
    );
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    
    $output = curl_exec($ch);
    $info = curl_getinfo($ch);
    
    curl_close($ch);
    if($info['http_code'] == 200 && strpos($output, '登录成功') !== false) {
        return true; 
    } else {
        return false;
    }
}

2. 退出函数

function disconnect() {
    $url = 'https://xxx.com/member.php?mod=logging&action=logout&formhash='.FORMHASH;
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
    $output = curl_exec($ch);
    $info = curl_getinfo($ch);
    
    curl_close($ch);
    if($info['http_code'] == 200 && strpos($output, '退出成功') !== false) {
        return true; 
    } else {
        return false;
    } 
}

3. FORMHASH获取

function getFormhash() {
    $url = 'https://xxx.com/';
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
    $output = curl_exec($ch);
    $formhash = '';
    
    if(preg_match('/<input type="hidden" name="formhash" value="(.*?)" \/>/', $output, $match)) {
        $formhash = $match[1];
    }
    
    curl_close($ch);
    return $formhash;
}

以上代码实现了通过CURL模拟登录与退出Discuz的功能,getFormhash()函数用于获取Discuz全局FORMHASH,用于退出时的参数。

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享
评论 抢沙发

请登录后发表评论