I have the following php code that logs in to a password protected page and grabs the protected page.The script is working fine but i want to use the login function only once, if i want to grab another protected page within same domain .
我有以下php代码登录到密码保护页面并获取受保护页面。脚本运行良好,但是如果我想在相同的域中获取另一个受保护的页面,我只希望使用登录函数一次。
i want to use cookie file to open the next protected page instead of using login function again !in another word i just want to bypass the login step for grabbing other protected pages.
我想使用cookie文件来打开下一个受保护的页面,而不是再次使用登录功能!换句话说,我只是想绕过登录步骤,来抓取其他受保护的页面。
Could any one show me how this can be done?
谁能告诉我这是怎么做的吗?
Note: My login function doesnt create any cookies i dont see it in same folder as the script!could any one tell me why?
注意:我的登录功能没有创建任何cookie,我没有看到它在同一文件夹与脚本!谁能告诉我为什么吗?
<?
$ch=login();
$html=downloadUrl('http://www.example.com/page1.asp', $ch);
////echo $html;
function downloadUrl($Url, $ch){
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_REFERER, "http://www.google.com/");
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
return $output;
}
function login()
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/login.asp'); //login URL
curl_setopt ($ch, CURLOPT_POST, 1);
$post_array = array(
'txtUserName'=>'brad',
'txtPassword'=>'bradpassword',
);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $post_array);
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$store = curl_exec ($ch);
return $ch;
}
?>
<html>
<br>
<textarea rows="30" cols="150"><?PHP print_r($html); ?></textarea>
</html>
2 个解决方案
#1
2
Use
使用
curl_setopt($ch,CURLOPT_COOKIEJAR, $cookieFileLocation);
curl_setopt($ch,CURLOPT_COOKIEFILE, $cookieFileLocation);
In second request where $cookieFileLocation is the location of your cookie file.
在第二个请求中,$cookieFileLocation是您的cookie文件的位置。
You have to have 2 requests. First is login request which fills the cookie file.
你必须有两个请求。首先是填充cookie文件的登录请求。
You have to check if your cookie file exists is_file($cookieFileLocation)
, and if it does you can perform second request for download protected content bypassing the login process.
您必须检查您的cookie文件是否存在is_file($cookieFileLocation),如果存在,您可以对下载受保护的内容执行第二个请求,绕过登录过程。
You should note that most of the systems has session expire time, so you have to make login after period of time. I would check html of returned page for login error as mark that i have to login again.
您应该注意到,大多数系统都有会话过期时间,因此必须在一段时间后登录。我会检查返回页面的html,检查是否有登录错误,标记我必须再次登录。
#2
2
You need to login first time and then reference cookie file path in subsequent request.
您需要第一次登录,然后在后续请求中引用cookie文件路径。
function curlPost($url,$postData){
$ch= curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postData,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_CONNECTTIMEOUT=>30,
CURLOPT_SSL_VERIFYPEER=>false,
CURLOPT_USERAGENT=>"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)",
CURLOPT_COOKIESESSION => true,
CURLOPT_COOKIEFILE => 'cookie.txt',
CURLOPT_COOKIEJAR => 'cookie.txt'
));
$output = curl_exec($ch);
curl_close( $ch );
return $output;
}
$postData = array(
'email' => 'aryan022@gmail.com',
'password' => 'aryan022',
'redirect_to' => 'http://localhost/cakephp/account '
);
$output=curlPost("http://localhost/cakephp/login",$postData);
/*use for subsequest request without passing all postData once login
$postData = array();
$output=curlPost("http://localhost/cakephp/account",$postData);
*/
echo $output;
#1
2
Use
使用
curl_setopt($ch,CURLOPT_COOKIEJAR, $cookieFileLocation);
curl_setopt($ch,CURLOPT_COOKIEFILE, $cookieFileLocation);
In second request where $cookieFileLocation is the location of your cookie file.
在第二个请求中,$cookieFileLocation是您的cookie文件的位置。
You have to have 2 requests. First is login request which fills the cookie file.
你必须有两个请求。首先是填充cookie文件的登录请求。
You have to check if your cookie file exists is_file($cookieFileLocation)
, and if it does you can perform second request for download protected content bypassing the login process.
您必须检查您的cookie文件是否存在is_file($cookieFileLocation),如果存在,您可以对下载受保护的内容执行第二个请求,绕过登录过程。
You should note that most of the systems has session expire time, so you have to make login after period of time. I would check html of returned page for login error as mark that i have to login again.
您应该注意到,大多数系统都有会话过期时间,因此必须在一段时间后登录。我会检查返回页面的html,检查是否有登录错误,标记我必须再次登录。
#2
2
You need to login first time and then reference cookie file path in subsequent request.
您需要第一次登录,然后在后续请求中引用cookie文件路径。
function curlPost($url,$postData){
$ch= curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postData,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_CONNECTTIMEOUT=>30,
CURLOPT_SSL_VERIFYPEER=>false,
CURLOPT_USERAGENT=>"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)",
CURLOPT_COOKIESESSION => true,
CURLOPT_COOKIEFILE => 'cookie.txt',
CURLOPT_COOKIEJAR => 'cookie.txt'
));
$output = curl_exec($ch);
curl_close( $ch );
return $output;
}
$postData = array(
'email' => 'aryan022@gmail.com',
'password' => 'aryan022',
'redirect_to' => 'http://localhost/cakephp/account '
);
$output=curlPost("http://localhost/cakephp/login",$postData);
/*use for subsequest request without passing all postData once login
$postData = array();
$output=curlPost("http://localhost/cakephp/account",$postData);
*/
echo $output;