Jamers Posted May 19, 2017 Report Share Posted May 19, 2017 废话不多讲,知道它的功能的自然知道重要性。 首先,我们建一个登录页面(设定环境为用户登录界面),点击测试,提交数据至testcookie.php <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <script type="text/javascript" src="/jq/jquery-1.12.4.min.js"></script> <script type="text/javascript" src="/jq/jquery.cookie.js"></script> <title>Cross Cookie Test</title> <script> function test() { $.ajax({ url: 'testcookie.php', type: 'GET', async: false, dataType: 'jsonp', crossDomain : true, success: function(d) { alert(d.msg); }, error: function() { alert('error'); } }); } </script> </head> <body> <button onclick='test()'>测试</button> </body> </html> 接收表单数据代码 testcookie.php:(环境设定:本地用户身份验证,生成相应的cookie密钥,当前域名设置cookie,并提交给其它站点这里是 t1.zomew.com) <?php //登录过程 $msg = 'ok_'.date('Ymd_His'); setcookie('MSG',$msg); $callback = ''; if (isset($_REQUEST['callback'])) { $callback = $_REQUEST['callback']; } header('Location: http://t1.zomew.com:8100/test/recvcookie.php?callback='.$callback.'&m='.$msg); t1.zomew.com上接收对应cookie值 recvcookie.php代码:(环境设定,第三方服务器接收到Cookie并写入浏览器,返回相应jsonp信息) <?php header('P3P:CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"'); $msg = 'empty'; if (isset($_REQUEST['m'])) { $msg = $_REQUEST['m']; } $callback = ''; if (isset($_REQUEST['callback'])) { $callback = $_REQUEST['callback']; } setcookie('MSG',$msg); $data = array(); $data['msg'] = $msg; $data['code'] = date('YmdHis'); $ret = json_encode($data); if ($callback) { $ret = "{$callback}({$ret});"; } exit($ret); 至此:点击一个测试,二个站点上均有相应cookie存在,如果存在多个域名共用的话,可能需要多次调用,也可根据实际需要将Cookie进行差异化处理,以保证信息安全。 Link to comment Share on other sites More sharing options...
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now