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

TRiG
  • 10,148
  • 7
  • 57
  • 107
Jojo01
  • 1,269
  • 4
  • 14
  • 35

3 Answers3

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=/'
Nico
  • 1,727
  • 1
  • 24
  • 42
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

jameshwart lopez
  • 2,993
  • 6
  • 35
  • 65
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.

Samuel Lindblom
  • 812
  • 1
  • 6
  • 22
  • The javascript code is not on the same page with the php code. – Jojo01 Jul 28 '15 at 09:58
  • 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: http://stackoverflow.com/questions/5045053/set-cookie-wih-js-read-with-php-problem. – Samuel Lindblom Jul 28 '15 at 10:05
  • I am running this on localhost – Jojo01 Jul 28 '15 at 10:09
  • 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. – Samuel Lindblom Jul 28 '15 at 10:21