Home > Backend Development > PHP Tutorial > After php uses curl to simulate login, if there are multiple users, how to save their login cookies? Without writing files

After php uses curl to simulate login, if there are multiple users, how to save their login cookies? Without writing files

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-08-04 09:19:20
Original
901 people have browsed it

Recently, Tefaqi wants to make a curl web application. If it is operated by multiple users, without writing the cookies to a file, how to save their login cookies and obtain their cookies for the next curl operation?
I have thought about the following solution:
Save it in memcache and use sessionid as the key of memcache (I don’t know if this solution is feasible)

Reply content:

Recently, Tefaqi wants to make a curl web application. If it is operated by multiple users, without writing the cookies to a file, how to save their login cookies and obtain their cookies for the next curl operation?
I have thought about the following solution:
Save it in memcache and use sessionid as the key of memcache (I don’t know if this solution is feasible)

I guess it’s because the user’s session state cannot be saved after simulated login. There are actually two solutions. One is to save it in a file. This will perform two IO operations, which is not particularly good for performance requirements. The second is Save its cookie value in the session or other cache. But the requirement is that the response header returned by the original website's response must have set-cookie. Without further ado, let's go directly to the code.

<code>function curlFetch($url, $data = null ,$referer = ""){
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 返回字符串,而非直接输出
    curl_setopt($ch, CURLOPT_HEADER, 1);   // 返回header部分
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);   // 设置socket连接超时时间
    if (!empty($referer))
    {
        curl_setopt($ch, CURLOPT_REFERER, $referer);   // 设置引用网址
    }
    $cookie_str=session('servlet');
    if(!empty($cookie_str)){
        curl_setopt($ch, CURLOPT_COOKIE , $cookie_str);
    }

    if (is_null($data))
    {
        // GET
    }
    else if (is_string($data))
    {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        // POST
    }
    else if (is_array($data))
    {
        // POST
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    }
    set_time_limit(120); // 设置自己服务器超时时间
    $str = curl_exec($ch);
    curl_close($ch);

    list($header, $body) = explode("\r\n\r\n", $str);
    // 解析COOKIE
    preg_match("/Set-Cookie:(.*);/iU", $header, $matches);

    if(!empty($matches) && empty($cookie_str)){
        $cookies=$matches[1];    //这里是你需要保存cookie
        session('servlet',$cookies);    //我这里将其保存在session中
    }
    return $body;    //页面所得到的数据
}</code>
Copy after login
Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template