1. Home
  2. Computing & Technology
  3. PHP / MySQL

Why aren't my cookies working?

By , About.com Guide

Question: Why aren't my cookies working?

PHP cookies are a relatively simple way of tracking users by storing information in their browser. Here are the most common reasons a cookie isn't working.

Answer: If your cookies aren't being set, one very common problem is that something is being sent to the browser before the cookie. The cookies need to be in the header and come before any HTML. For example:

<html>
<head>
<title>My Page</title>
<?php
$Month = 2592000 + time();
setcookie(AboutVisit, date("F jS - g:i a"), $Month);
?>
This code won't work, because you are sending the data (the <html> and other tags) to the browser before the cookie. Instead you should use something like this:
<?php
$Month = 2592000 + time();
setcookie(AboutVisit, date("F jS - g:i a"), $Month);
?>
<html>
<head>
<title>My Page</title>
If the problem is that the cookies work for most users but some users are having problems with them, it may be that those particular users have cookies disabled in their browsers. This is not a problem with your code at all, just a user's personal preference.

More PHP / MySQL Q&A
Explore PHP / MySQL
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. Troubleshooting
  5. Troubleshooting PHP Cookies - PHP Cookies Help - PHP Cookies Not Working

©2009 About.com, a part of The New York Times Company.

All rights reserved.