tags:

views:

58

answers:

3

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>
+1  A: 

$cookie_expire = time()+86400;

See the PHP manual on variable types:

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

pygorex1
+6  A: 

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.

George Edison
that worked!, when I check my cokies there isnt one being set? any idea why this may be. Cookies are enbled in my browser.
Jacksta
You have to call the cookies into action for it to so. Ex: If you have a cookie for a user's first name, you need to write: echo "$_COOKIE['first_name']";.
ggfan
You may want to post this as another question.
George Edison
+2  A: 

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

maraspin