curl实现模拟登陆

分类: 源代码 > PHP
user.php

<?php
class User
{
    public function __construct()
    {
        session_start();
    }

    public function actionIndex()
    {
        if (isset($_SESSION['user_id'])) {
            echo 'index success';
        } else {
            echo 'index no login';
        }
    }

    public function actionLogin()
    {
        if (isset($_SESSION['user_id'])) {
            $this->actionIndex();
            return;
        }
        if (!empty($_GET['username']) && !empty($_GET['password'])) {
            $user = $this->getUserByUsername($_GET['username']);
            if ($user && $user['password'] == $_GET['password']) {
                $_SESSION['user_id'] = $user['user_id'];
                echo 'login success';
                return;
            }
        }
        echo 'login failure';
    }

    public function actionLogout()
    {
        if (isset($_SESSION['user_id'])) {
            unset($_SESSION['user_id']);
        }
        echo 'logout success';
    }

    public function getUserByUsername($username) {
        $user_config = array(
            1 => array('user_id' => 1, 'username' => 'guibin1', 'password' => '123456'),
            2 => array('user_id' => 2, 'username' => 'guibin2', 'password' => '123456'),
        );
        foreach ($user_config as $val) {
            if ($val['username'] == $username) {
                return $val;
            }
        }
        return array();
    }

    public function __destruct()
    {
        session_write_close();
    }
}

$user = new User();
$actions = array();
$methods = get_class_methods($user);
foreach ($methods as $m) {
    if (strstr($m, 'action')) {
        $actions[] = $m;
    }
}
$action = !empty($_GET['method']) ? ('action' . ucfirst($_GET['method'])) : '';
$action = in_array($action, $actions) ? $action : $action[0];
$user->$action();

 

login.php

<?php
class loginCurl
{
    public $login_url = 'http://tool.gb/curl/user.php?method=login';
    public $index_url = 'http://tool.gb/curl/user.php?method=index';

    public function __construct()
    {
        session_start();
        $_SESSION['cookie_file'] = tempnam('./temp','cookie');
    }

    public function login($username, $password)
    {
        $url = $this->login_url . '&username=' . $username . '&password=' . $password;
        $response = $this->curl($url, true);
        dump($response);
        return;
    }

    public function exec()
    {
        $response = $this->curl($this->index_url, true);
        dump($response);
        return;
    }

    public function curl($url, $cookie = null)
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5');
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        if ($cookie) {
            curl_setopt($ch, CURLOPT_COOKIEJAR, $_SESSION['cookie_file']);
            curl_setopt($ch, CURLOPT_COOKIEFILE, $_SESSION['cookie_file']);
        }
        $response = curl_exec($ch);
        curl_close($ch);
        return $response;
    }
}

$loginCurl = new loginCurl();
$loginCurl->login('guibin1', '123456');
for ($i = 0; $i < 10; $i++) {
    $loginCurl->exec();
}

来源:原创 发布时间:2020-07-14 21:20:49