简体   繁体   English

将Cookie从页面传递到PHP(或JavaScript)中的另一个页面

[英]Passing cookies from a page to another in php (or javascript)

Hullo, I need to open a page with file_get_html featuring it with two cookies a get from another page. 嗨,我需要打开一个带有file_get_html的页面,其中包含来自另一个页面的两个cookie。 How do I collect the cookies from the former page and submit them to the latter? 我如何从前一页收集cookie并将其提交给后者?

In JavaScript first: 首先在JavaScript中:

As JavaScript is a client side language, so there is no need to pass the cookies, but if you are calling a URL of some different domain then you have to use jquery.getjson , as cross domain is not allowed in javascript 由于JavaScript是一种客户端语言,因此不需要传递cookie,但是如果您要调用其他域的URL,则必须使用jquery.getjson ,因为javascript中不允许跨域

Second in PHP, you can pass the cookies through curl, there is a operation in curl which is curl_setopt($session, CURLOPT_COOKIE, $cookie) . 其次,在PHP中,您可以通过curl传递cookie,在curl中有一个操作curl_setopt($session, CURLOPT_COOKIE, $cookie) At any time you can get the cookies by $_COOKIE . 您可以随时通过$_COOKIE获取cookie。

First of all if your working with cookies the best is to set the cookie on a Domain base: 首先,如果您使用Cookie,最好的方法是在Domain的基础上设置Cookie:

So lets say your domain is www.somesite.com 因此,假设您的域名是www.somesite.com

//This cookie will expire in one hour. //此Cookie将在一小时后过期。

$expire=time()+3600; $ expire = time()+ 3600; setcookie("user", $value, $expire, "/","somesite.com"); setcookie(“ user”,$ value,$ expire,“ /","somesite.com”);

Now to read the cookie on another page with the same domain 现在读取具有相同域的另一页上的cookie

echo $_COOKIE["user"]; 回声$ _COOKIE [“ user”];

If you want to test your cookies better in your browser i recommend you use firefox with firebug and firecookie addons in that way you will see all your cookies and you can even edit them and understand the way they work on a browser. 如果您想在浏览器中更好地测试cookie,我建议您将firefox与firebug和firecookie插件一起使用,这样您将看到所有cookie,甚至可以编辑它们并了解它们在浏览器中的工作方式。

To download firefox: http://www.mozilla.org/en-US/firefox/fx/ 要下载firefox,请访问: http : //www.mozilla.org/en-US/firefox/fx/

To get the firebug: http://getfirebug.com/ 要获取萤火虫: http : //getfirebug.com/

To get the firecookie: https://addons.mozilla.org/en-US/firefox/addon/firecookie/ 要获取firecookie: https ://addons.mozilla.org/en-US/firefox/addon/firecookie/

Better not to use cookies if the data is sensitive as the cookie can be manipulated from the client side... if you want to secure more the data you can use combination of cookie and session. 如果数据敏感,最好不要使用cookie,因为可以从客户端操作cookie。如果要保护更多数据,可以结合使用cookie和会话。

Regards, Gabriel 问候,加百利

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM