Home  >  Article  >  Backend Development  >  php CURL simulates cookie login code

php CURL simulates cookie login code

WBOY
WBOYOriginal
2016-07-25 08:56:34831browse
This article introduces an example code that uses curl to simulate cookie login in PHP. Friends in need can refer to it.

Code:

<?php
/**
* curl 模拟cookie登录
* by bbs.it-home.org
*/
//1,提交数据,生成cookie,将cookie保存在临时目录下  
//在指定目录中建立一个具有唯一文件名的文件。
//如果该目录不存在,tempnam() 会在系统临时目录中生成一个文件,并返回其文件名  
$cookie_file=tempnam('./temp','cookie');  
$ch=curl_init();  
$login_url="http://127.0.0.1/cms/ptjzy/admin_ys/checklogin.php";  
$curlPost="admin_name=admin&admin_pwd=admin&action=login";  
curl_setopt($ch,CURLOPT_URL,$login_url);  
//启用时会将头文件的信息作为数据流输出  
curl_setopt($ch,CURLOPT_HEADER,0);   
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);  
curl_setopt($ch,CURLOPT_POST,1);  
curl_setopt($ch,CURLOPT_POSTFIELDS,$curlPost);  
//设置连接结束后保存cookie信息的文件  
curl_setopt($ch,CURLOPT_COOKIEJAR,$cookie_file);  
curl_exec($ch);  
curl_close($ch);  
  
//2,通过读取cookie来访问页面  
$ch=curl_init();  
$login_url2="http://127.0.0.1/cms/ptjzy/admin_ys/content/addinfo.php";  
curl_setopt($ch,CURLOPT_URL,$login_url2);  
curl_setopt($ch,CURLOPT_HEADER,0);   
curl_setopt($ch,CURLOPT_RETURNTRANSFER,0);  
//设置连接结束后保存cookie信息的文件  
curl_setopt($ch,CURLOPT_COOKIEJAR,$cookie_file);   
curl_exec($ch);  
curl_close($ch);  
?>


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