2

I am setting a cookie with javascript and trying to read it with PHP, but php is not able to read it. I have checked that the cookie is really set with a tool called Cookies Manager.

Code(JS):

<script>
document.cookie="encrIv=" + ivB64;
</script>

Code(PHP):

<?php
$encrIv = $_COOKIE['encrIv'];
echo $encriv;
?>

I get

Notice: Undefined index: encrIv in C:\Users\joonas\Desktop\Webon cms\root\readCookie.php on line 1

Screen shot of cookie:

Cookies Manager screenshot

1
  • Check if value of ivB64 is undefined. Also at line 2 replace $encriv by $encrIv. Commented Jul 28, 2015 at 9:48

3 Answers 3

3
<!DOCTYPE html>
<html>
  <head>
    <title>example</title>
    <script type="text/javascript">
       document.cookie = 'name=David' ;
    </script>
   </head>
   <body>
    <?php
       var_dump($_COOKIE['name']);
    ?>
   </body>
 </html>

with this the cookie is set. Did you correct your Typo? You wrote:

<?php
$encrIv = $_COOKIE['encrIv'];
echo $encriv;
?>

the correct way is to change the echo to

echo $encrIv;

or to change your variable to

$encriv = $_COOKIE['encrIv'];

EDIT:

Maybe your Problem is the not defined Path. define a cookie like this:

document.cookie = 'sconName='+changedName+'; path=/'
Sign up to request clarification or add additional context in comments.

4 Comments

I want to put a javascript variable as the cookie's content
thats no Problem. then document.cookie = "name= " +user; should work.
Did not change anything
No Problem :) I had the same issue some time ago.
1

Your code works fine for me, if I refresh the page after it has been loaded.

Your javascript code will be run after the php code (when the php interpreter has processed the html+php code and the browser interprets the processed html code), which means that when you try to access it using php it is not set yet. But when you reload the page the cookie will be there and php can access it.

4 Comments

The javascript code is not on the same page with the php code.
Ok, then answering your question becomes much more difficult. Have you printed all cookies with print_r($_COOKIE). If you have checked that the cookie is set but php does not list it then you probably have a domain issue, check out this question: stackoverflow.com/questions/5045053/….
I am running this on localhost
I tested the code that you pasted in two php+html pages with no other code on the same path on the same http server and it works. That's really all I can do with the information that you provided. The only thing else is that the cookie name in your Cookies Manager has a lowercase L while the javascript cookie name has an uppercase i and the echo has a lowercase i, but the others suggested that already.
1

change your code specially the echo part from $encriv to $encrIv like this simple example

say this is index.php which you need to visit first.The file that will set a value of encrIv cookie

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript">
        document.cookie="encrIv=samplecookie";
    </script>
</head>
<body>
    <?php 

        $encrIv = isset($_COOKIE['encrIv'])?$_COOKIE['encrIv']:'';
        echo $encrIv;
    ?>
</body>
</html>

then this your readCookie.php which you will visit after index.php has been loaded.

 <?php 

            $encrIv = isset($_COOKIE['encrIv'])?$_COOKIE['encrIv']:'';
            echo $encrIv;
        ?>

That should clearly help you myfriend

8 Comments

i did some edits to my answer please notice the isset, and try it .. then tell me what happen after you try it
No, because the problem still isn't solved. It still can't read the cookie i set on another page!
that should solve your problem . maybe your accessing other page before the actual setting of cookie..
is ivB64 variable does have a value?
|

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.