Home  >  Article  >  Backend Development  >  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

WBOY
WBOYOriginal
2016-08-04 09:19:20789browse

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>
Statement:
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