1

I am attempting to store a javascript variable in a cookie so I can call it with PHP. I've never done this before, so pardon my naivety. I think the problem is that I'm trying to do this in two files (one is my tableau trusted file, the other is the actual php file).

(My problem isn't the code. I think the code works. The problem is with the procedure which isn't explained in the dupe.)

What am I doing wrong? What order should I do things? How can I get the cookie stored before I call it with PHP?

<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>
7
  • Possible duplicate of set cookie value in javascript and displaying it with php Commented May 16, 2017 at 15:18
  • Unclear issue! are you trying to read cookie from js to php ? Commented May 16, 2017 at 15:20
  • I'm trying to figure out how to do the following things in order: 1) Save javascript parameter in cookie 2) Call parameter in cookie with php 3) Pass parameter into php Commented May 16, 2017 at 15:21
  • in the right , you should use something like : return json_encode($_COOKIE['cookielogin']); but since you are echoing the iframe in same time... it fail. You must improve the request control and a minimum separation in your php outputing. This is ugly Commented May 16, 2017 at 15:30
  • I dont really need pretty. I'm not a programmer. Just need it to work. Commented May 16, 2017 at 15:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.