Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting a cookie in php

Tags:

php

cookies

I am trying to set a cookie, whas wrong with this as I am getting an error.

Warning: setcookie() expects parameter 3 to be long, string given 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>
like image 677
Jacksta Avatar asked Nov 24 '25 14:11

Jacksta


2 Answers

You are passing the value time()+86400 as a string. This is because you have enclosed it with quotes.

Probably what you wanted to do:

$cookie_expire = time()+86400;

This will cause the value to be evaluated as a number instead of a string.

like image 81
Nathan Osman Avatar answered Nov 26 '25 04:11

Nathan Osman


Both of the hints you've been given above are right; you were actually trying to pass a string to the setcookie function that way.

Just drop the "s and you should do fine.

By the way, if it happens to you again to get in similar situations, try to use var_dump PHP function (...or a debugger for that matter), which can tell you what (data and type) a variable contains. Then you can spot what the exact problem is and you can go backwards to the source of it, in order to quickly fix it...

like image 24
maraspin Avatar answered Nov 26 '25 02:11

maraspin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!