Setting a cookie in php

I am trying to set a cookie which is wrong on this as I am getting an error.

Warning: setcookie () expects parameter 3 to be long, the line is specified in /home/admin/domains/domain.com.au/public_html/setcookie.php on line 6

<?php
$cookie_name = "test_cookie";
$cookie_value = "test_string";
$cookie_expire = "time()+86400";
$cookie_domain = "localhost";
setcookie($cookie_name, $cookis_value, $cookie_expire, "/", $cookie_domain, 0);
?>
<HTM>
<HEAD>
</HEAD>
<BODY>
<h1>cookie mmmmmmm</h1>
</BODY>
</HTML>

      

+2


a source to share


3 answers


You are passing the value time()+86400

as a string. This is because you enclosed it with quotes.

Perhaps what you wanted to do:



$cookie_expire = time()+86400;

      

This will cause the value to be evaluated as a number, not a string.

+6


a source


Both of the hints you gave above are correct; you were actually trying to pass a string to the setcookie function.

Just drop the "s" and you should be doing it right.



By the way, if you have to run into similar situations again, try using PHP's var_dump function (... or a debugger for that matter) which can tell you what (data and type) a variable contains.Then you can determine what is the exact problem and you can go back to the source to fix it quickly ...

+2


a source


$cookie_expire = time()+86400;

See PHP manual for variable types:

http://us3.php.net/manual/en/language.types.intro.php

+1


a source







All Articles