如何使用curl和PHP登录网站? [英] How to login to a website using curl and PHP?

查看:107
本文介绍了如何使用curl和PHP登录网站?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试远程登录站点.我已成功完成此操作,登录页面会正确打开,我可以从中提取所有信息.问题是,当我单击任何链接时,该网站将我注销!之前的链接是相对于我的域的,现在我已将其更改为网站域,但问题仍然存在.我为您附上代码,以查看我如何发送Cookie.

I was trying to login to a site remotely. I have successfully done that and the landing page opens properly and I can extract all info from it. The problem is, when I click any link, the website logs me out! Earlier the link was relative to my domain, now I've changed it to the website domain but still the problem persists. I'm attaching the code for you to see how I've sent cookies.

我已经对站点内的另一个URL执行了另一个curl_exec,但是该网站使我注销了!

I have executed another curl_exec to another URL within the site, but the website logs me off!

<html> 
<head> 
<base href="http://example.com/" /> 
<body> 
<?php 
$username = $_POST["uname"]; 
$password = $_POST["pass"]; 
$fields = "username=" . $username . "&password=" . $password . "&Login.x=61&Login.y=15"; 
$ch=curl_init(); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 

curl_setopt($ch, CURLOPT_URL, 'http://www.SOMESITE.net/amizone/default.aspx'); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_COOKIEJAR, "ASPSESSIONIDSQADDRBQ=OCALKIGABGLPCJLKMGIKJLLF"); //I got this exact cookiename from LiveHTTPheaders (Firefox extension)
curl_setopt($ch, CURLOPT_COOKIEFILE, "ASPSESSIONIDSQADDRBQ=OCALKIGABGLPCJLKMGIKJLLF"); 
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1000 ); 
curl_setopt($ch, CURLOPT_TIMEOUT, 10000 ); 
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.8) Gecko/20061025 Firefox/1.5.0.8' ); 
curl_setopt($ch, CURLOPT_AUTOREFERER, 1 ); 
curl_setopt ($ch, CURLOPT_REFERER, "http://www.example.com/"); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0 ); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0 ); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); 
ob_start(); //the issue with the script remains with/without ob_start/ob_end_clean
$raw_data=curl_exec($ch); 
curl_setopt($ch, CURLOPT_POST, 0); //at this point im trying to get another page using curl. since i will not be using post i have set it to 0.
curl_setopt($ch, CURLOPT_URL, 'http:/site.net/amizone/WebForms/TimeTable/StudentTimeTableForWeek.aspx'); 
$ttable = curl_exec($ch); 
ob_end_clean(); 
echo $ttable; 
//curl_close($ch); 
//$raw_data = preg_replace('/\s(1,)/',' ',$raw_data); //clean it up 
//echo $raw_data; 
//$raw_data = stripslashes($raw_data); 
//$raw_data = explode("<strong>",$raw_data); 
//2 3 and 4 
//echo htmlentities($raw_data[4]); 
?> 
</body> 
</html> 

注意:我已经从Firefox的扩展名'LiveHttpHeaders'中获取了cookie名称.我想念什么?

Note: I have taken the cookie name from 'LiveHttpHeaders', an extension for Firefox. What am I missing?

推荐答案

您正在使用cookiejar和cookiefile做什么?这些不是用于设置cookie的,会话变量不应进行硬编码,而应从对ASP Web服务器的第一个请求中获取.

What are you doing with the cookiejar and cookiefile? Those are not for setting cookies, and a session variable shouldn't be hardcoded, but be obtained from the first request to the ASP web server.

您要根据文档将cookiejar和cookiefile设置为一个值:

You want to set the cookiejar and cookiefile to a value as per the documentation:

CURLOPT_COOKIEFILE 包含cookie数据的文件的名称. Cookie文件可以是Netscape格式,也可以是纯HTTP样式 标头转储到文件中.
CURLOPT_COOKIEJAR 的文件名 将所有内部Cookie保存到关闭手柄时,例如之后 调用curl_close.

CURLOPT_COOKIEFILE The name of the file containing the cookie data. The cookie file can be in Netscape format, or just plain HTTP-style headers dumped into a file.
CURLOPT_COOKIEJAR The name of a file to save all internal cookies to when the handle is closed, e.g. after a call to curl_close.

因此,没有cookie数据,而是文件的路径.这样,会话cookie(和其他cookie)将被存储并在后续请求中使用.

So, no cookie data, but a path to a file. This way the session cookie (and other cookies) will be stored and used in successive requests.

这篇关于如何使用curl和PHP登录网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆