Jacksta
Jacksta

Reputation: 1293

setting a cookie in php

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>

Upvotes: 2

Views: 308

Answers (3)

maraspin
maraspin

Reputation: 2393

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...

Upvotes: 2

Nathan Osman
Nathan Osman

Reputation: 73195

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.

Upvotes: 6

leepowers
leepowers

Reputation: 38318

$cookie_expire = time()+86400;

See the PHP manual on variable types:

https://www.php.net/manual/en/language.types.intro.php

Upvotes: 1

Related Questions