1

I want to set cookies in safari browser.. i using php code as below.. which work fine in mozila and chroome. but i cant understand what wrong in safari browser.

here is simple code of setcookies

<!DOCTYPE html>
<?php
    $cookie_name = "user";
    $cookie_value = "John Doe";

    setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day

?>
<html>
    <body>

        <?php
        if (!isset($_COOKIE[$cookie_name])) {
            echo "Cookie named '" . $cookie_name . "' is not set!";
        } else {
            echo "Cookie '" . $cookie_name . "' is set!<br>";
            echo "Value is: " . $_COOKIE[$cookie_name];
        }
        ?>

    </body>
</html>
Nitesh Pawar
  • 435
  • 2
  • 11

2 Answers2

0

It should not work in any browser because you can't send anything before you call setcookie because you need to send it in the header before the content is send, if you enable errors you should get error that headers already sent. Try this:

<?php
    $cookie_name = "user";
    $cookie_value = "John Doe";

    setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day

?>
<!DOCTYPE html>
<html>
    <body>

ensure that you don't have any whitespace before <?php

jcubic
  • 61,973
  • 54
  • 229
  • 402
  • I checked it there is not have any whitespace.. I have enable all php errors. i cant get any errror... I have check 2 time again in mozila and chroom.. I saw cookies variable and value in inspect. i can show u screenshot also. but not need.. – Nitesh Pawar Jan 06 '16 at 11:56
  • please you need to run above code in mozila.. you will see cookies in inspect element.. but u run it using safari. cant see anything. if u find cookies variable and value.. pls send me screenshot.. i trying to solve it form 2 das. – Nitesh Pawar Jan 06 '16 at 11:58
  • @NiteshPawar turn out that you don't need this if you have output_buffering enabled http://stackoverflow.com/a/34633085/387194 – jcubic Jan 06 '16 at 12:39
0

try this

header('Set-Cookie:testcookie=testval; expires=Sat, 23-Sep-2014 11:23:02 GMT; path=/');
devpro
  • 16,184
  • 3
  • 27
  • 38