html页面不通过libcurl获取cookie [英] html page not getting cookies through libcurl

查看:155
本文介绍了html页面不通过libcurl获取cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我修改了我之前的代码。您可以查看我的上一篇文章,如果您的intersted 通过curl设置cookie
但是这里是一个新的开始我的新代码看起来linke这个
i有一个php文件使用curl像这样

i modified my previous code. you can see my previous post if your intersted setting cookie through curl But here is a fresh beginning my new code looks linke this i have a php file using curl like this

<?php
$ch=curl_init();
$url="http://localhost/javascript%20cookies/test_cookies.php";
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_COOKIEFILE,dirname(__FILE__) . "/cookie.txt");
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_exec($ch);
curl_close($ch);
?>

文件 * test_cookies.php * 如下

<?php
if($_COOKIE['user']==1)
{
header("Set-Cookie:user=1; color=blue");
header("Location:http://localhost/javascript%20cookies/test_cookies.html");
}
?>

文件 * test_cookies.html * 有一些javascript检查Cookie并且如果它找到那些cookie,则它相应地显示该文本。
具有curl代码的php文件正在发送Cookie,并且 * test_cookies.php * 正在设置Cookie并重定向到页面 * test_cookies.html * ,但此页面未收到Cookie,因此不会相应地显示内容。
现在有人能告诉我现在的问题了吗?

the file *test_cookies.html* has some javascript that checks for cookies and if it finds those cookies then it displays the text accordingly. the php file with curl code is sending the cookies and the *test_cookies.php* is setting the cookie and redirecting to the page *test_cookies.html* but this page is not receiving the cookies and thus it is not not showing the content accordingly. can somebody tell me whats the problem now?

这里是我在firefox中显示的标题设置CURLOPT_HEADER为true

here are the headers i get displayed in firefox on setting CURLOPT_HEADER to true

HTTP/1.1 302 Found Date: Mon, 16 May 2011 15:03:59 GMT Server: Apache/2.2.14 (Win32) DAV/2 mod_ssl/2.2.14 OpenSSL/0.9.8l mod_autoindex_color PHP/5.3.1 mod_apreq2-20090110/2.7.1 mod_perl/2.0.4 Perl/v5.10.1 X-Powered-By: PHP/5.3.1 Set-Cookie: user=1; color=blue Location: http://localhost/javascript%20cookies/test_cookies.html Content-Length: 0 Content-Type: text/html HTTP/1.1 200 OK Date: Mon, 16 May 2011 15:03:59 GMT Server: Apache/2.2.14 (Win32) DAV/2 mod_ssl/2.2.14 OpenSSL/0.9.8l mod_autoindex_color PHP/5.3.1 mod_apreq2-20090110/2.7.1 mod_perl/2.0.4 Perl/v5.10.1 Last-Modified: Mon, 16 May 2011 12:13:24 GMT ETag: "11000000013d0c-493-4a363950a70f3" Accept-Ranges: bytes Content-Length: 1171 Content-Type: text/html

你可以看到有两组头显示。这是因为我对头调用了2次?

you can see that there are two set of headers displayed.is this because i am making 2 calls to header?

推荐答案

我现在将解释我想做什么。
我有一个html页面,要求一些验证码,然后它发送该验证码到一个php脚本,验证该代码设置cookie并重定向到同一个html页面。
html页面有一些外部链接的javascript,在检查cookie值显示页面的内容与一些调整。
i现在正在为此

i will now explain what i was trying to do. i had an html page that asked for some verification code and then it sent that verification code to a php script which on verifying the code set a cookie and redirected to the same html page. the html page had some externally linked javascript which on checking the cookie value displayed the content of the page with some tweaking. i am now writing the code for this

包含内容和格式的html文件

the html file with content and form


test_cookies.html

test_cookies.html



    //some css,javascript and html and then a form
<form method="post" action="http://localhost/javascript%20cookies/test_cookies.php">

验证代码的php文件


test_cookies.php

test_cookies.php



if($_POST['value']=="code")
setcookie("user",1);
if($_POST['value']!="code")
setcookie("user",1,time()-1);
header("Location:http://localhost/javascript%20cookies/test_cookies.html");

现在是带有curl代码的php文件

and now the php file with curl code

curl_cookies.php

curl_cookies.php



<?php
$ch=curl_init();
$url="http://localhost/javascript%20cookies/test_cookies.php";
$post="value=code";     //here i have hard-coded the pst value for a demo but i could have got this from the user
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
curl_setopt($ch,CURLOPT_HEADER,1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$res=curl_exec($ch);
curl_close($ch);
preg_match('/Location: (.*)\s/',$res,$location);
preg_match('/Set-Cookie: (.*)\s/',$res,$cookie);
$cookie=rtrim($cookie[0])."; path=/ ";         //path needed to be changed because curl_cookies.php and test_cookies.html are in different directories. 
header($cookie);
header($location[0]);
?>

这最后工作,浏览器显示调整的内容。这个东西教会了我很多关于http。
感谢所有在黑暗中帮助我的人。

this finally worked and the browser is displaying the tweaked content. this thing taught me a lot about http. Thanks to all who helped me when i was in dark.

这篇关于html页面不通过libcurl获取cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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