• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

模拟登陆 保存cookie 怎么让登陆网站的时候带着这个cookie访问?

php 搞代码 4年前 (2022-01-25) 14次浏览 已收录 0个评论
文章目录[隐藏]

第一步:在我自己的本地(127.0.0.1) 模拟登陆一个网站 获取到cookie 保存在本地。
第二步:当我正常访问这个网站(http://xxxxxshequ.com) 带着我保存在本地的cookie
朋友做营运 切换很多小号 手动登陆 搞得很麻烦
想让他轻松些 第一步实现起来很简单 主要是第二步有什么办法实现吗?

回复内容:

第一步:在我自己的本地(127.0.0.1) 模拟登陆一个网站 获取到cookie 保存在本地。
第二步:当我正常访问这个网站(http://xxxxxshequ.com) 带着我保存在本地的cookie
朋友做营运 切换很多小号 手动登陆 搞得很麻烦
想让他轻松些 第一步实现起来很简单 主要是第二步有什么办法实现吗?

基于 PHP/CURL 编写的类库, 使用方法见注释.
直接使用, 不用处理Cookie(程序自动会处理, Cookie信息保存于调用类时传递的参数所指定的文件里).

<code>php</code><code><?php/** * Author: dds_feng * Email: [email protected] * * Example: * $http = new HttpClient(__DIR__.'/cookie.ck'); * $http->SetReferer('http://foo.com');//set request referer * echo $http->Get('http://foo.com/');//get * $http->SetProxy('http://127.0.0.1:8888');//set http proxy * echo $http->Post('http://bar.com/xxx', array('a'=>'123', 'b'=>'456'));//post **/class HttpClient{    private $ch;    function __construct($cookieJar){        $this->ch = curl_init();        curl_setopt($this->ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36');//UA        curl_setopt($this->ch, CURLOPT_TIMEOUT, 60);//timeout        curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, TRUE);//follow redirection        curl_setopt($this->ch, CURLOPT_AUTOREFERER, true);        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, TRUE);        curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, FALSE);//ssl        curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, FALSE);        curl_setopt($this->c<div>)本文来源gaodai.ma#com搞#代!码网_</div><strong>搞代gaodaima码</strong>h, CURLOPT_COOKIEJAR, $cookieJar);//cookie jar        curl_setopt($this->ch, CURLOPT_COOKIEFILE, $cookieJar);    }    function __destruct(){        curl_close($this->ch);    }    final public function SetProxy($proxy='http://127.0.0.1:8888'){        //curl_setopt($this->ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);        curl_setopt($this->ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);//HTTP proxy        //curl_setopt($this->ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);//Socks5 proxy        curl_setopt($this->ch, CURLOPT_PROXY, $proxy);    }    final public function SetReferer($ref=''){        if($ref != ''){            curl_setopt($this->ch, CURLOPT_REFERER, $ref);//Referrer        }    }    final public function SetCookie($ck=''){        if($ck != ''){            curl_setopt($this->ch, CURLOPT_COOKIE, $ck);//Cookie        }    }    final public function Get($url, $header=false, $nobody=false){        curl_setopt($this->ch, CURLOPT_POST, false);//GET        curl_setopt($this->ch, CURLOPT_URL, $url);        curl_setopt($this->ch, CURLOPT_HEADER, $header);//Response Header        curl_setopt($this->ch, CURLOPT_NOBODY, $nobody);//Response Body        return curl_exec($this->ch);    }    final public function Post($url, $data=array(), $header=false, $nobody=false){        curl_setopt($this->ch, CURLOPT_URL, $url);        curl_setopt($this->ch, CURLOPT_HEADER, $header);//Response Header        curl_setopt($this->ch, CURLOPT_NOBODY, $nobody);//Response Body        curl_setopt($this->ch, CURLOPT_POST, true);//POST        curl_setopt($this->ch, CURLOPT_POSTFIELDS, http_build_query($data));//data        return curl_exec($this->ch);    }    final public function getError(){        return curl_error($this->ch);    }}// vim: noexpandtab tabstop=4 shiftwidth=4 softtabstop=4:</code>

参考:
http://www.piaoyi.org/php/php-curl-cookies.html
http://stackoverflow.com/questions/12885538/php-curl-and-cookies


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:模拟登陆 保存cookie 怎么让登陆网站的时候带着这个cookie访问?
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址