如何读取从JavaScript到PHP的cookie?

时间:2017-05-16 15:13:45

标签: javascript php cookies

我试图在cookie中存储一个javascript变量,所以我可以用PHP调用它。我以前从未这样做过,所以请原谅我的天真。我认为问题在于我试图在两个文件中执行此操作(一个是我的tableau受信任文件,另一个是实际的php文件)。

(我的问题不是代码。我认为代码有效。问题在于程序没有在dupe中解释。)

我做错了什么?我应该做什么命令?在用PHP调用cookie之前如何存储cookie?

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<script>
  var settings = {
     url: "https://harmelin.okta.com/api/v1/users/me",
    type: 'GET',
    dataType: 'json',
    contentType: 'application/json',
    xhrFields: {
        withCredentials: true
    },
    success: function (data) {
       // alert(JSON.stringify(data));
    },
    error: function(err){
      //  alert(JSON.stringify(err));
    }
   }

   jQuery.ajax(settings).done(function (success)  {
     console.log(success);
 var raw = success.profile.login;
 var email = raw.toLowerCase();
 var oktaLogin = email.replace(/@[^@]+$/, '');
jQuery("#write-data").append(oktaLogin);

});

 $(document).ready(function () {
 createCookie("cookielogin", oktaLogin, "30");
 });

 function createCookie(name, value, days) {
 var expires;
 if (days) {
  var date = new Date();
  date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
  expires = "; expires=" + date.toGMTString();
 } else {
  expires = "";
 }
  document.cookie = escape(name) + "=" + escape(value) + expires + "; path=/";
 }
</script>

</head>

<body>
<?php

// Tableau-provided functions for doing trusted authentication
require_once 'tableau_trusted_new.php';

?>

<div id="write-data"></div>
<?php
$user = 'jfedorowicz';
$server = 'dashboard1.harmelin.com';
$view = 'views/PTOStuff/Dashboard1';
$thelogin = $_COOKIE["cookielogin"];

echo '<iframe src="';
echo get_trusted_url( $user,$server,$view,$thelogin);
echo '" width="1000" height="1000"> </iframe>';
?>

</body>

0 个答案:

没有答案