Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm attempting to set a cookie in PHP and read the cookie on a different page with JavaScript. I'm not too sure about how to go about this because of the fact that I'm creating the cookies in a PHP foreach loop and I'd like to read all the cookies in JavaScript.

My cookie creation foreach loop:

function parseSearchedTerm($searchedTerm, $bearer_token){
    $decode = json_decode($searchedTerm, true);
    foreach($decode['statuses'] as $q){
        $_COOKIE['text'] = $q['text'];
        if($q['geo']['coordinates'] != null){
        $_COOKIE['geo0'] = $q['geo']['coordinates'][0];
        $_COOKIE['geo1'] = $q['geo']['coordinates'][1];
        }   
    }
    invalidate_bearer_token($bearer_token);
}
share|improve this question
    
You are not setting cookies at all. You are simply setting values in the $_COOKIE super global. You need to use setcookie() Also each loop iteration simply overwrites the values, which is probably not what you intend. –  Mike Brant Mar 9 at 7:29

1 Answer 1

Please give a read on writing cookies using PHP, over here

Set Cookie in PHP Generic way = setcookie(name, value, expire, path, domain); Example

$expire=time()+60*60*24*30;
setcookie("user", "Alex Porter", $expire);

Read cookies from javascript, read this Example

function getCookie(cname)
{
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) 
  {
  var c = ca[i].trim();
  if (c.indexOf(name)==0) return c.substring(name.length,c.length);
  }
return "";
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.