What Is the Common Mistake When Setting Path and Domain on Temporary Cookies

Q

What Is the Common Mistake When Setting Path and Domain on Temporary Cookies? - PHP Script Tips - Understanding and Managing Cookies

✍: FYIcenter.com

A

A common mistake made by many PHP developers is using an empty string for the expiration time parameter when setting path and domain for temporary cookies. The PHP script below shows an example of this mistake:

<?php
  # Incorrect use of setcookie()
  setcookie("LoginName","FYICenter", "", "/", ".fyicenter.com");

  # Correct use of setcookie()
  setcookie("PreferredColor","Blue", NULL, "/", ".fyicenter.com");
?>

If you run this script, you will get an error:

PHP Warning:  setcookie() expects parameter 3 to be long,
  string given in \php_working_with_cookies.php on line 3

2007-04-24, 4933👍, 0💬