Search code examples
phpcookiesipsetcookie

Failing to set cookies in PHP


I have written a PHP script which takes information about visitors IP and browser from a website and stores it in a table in my database. This script is included in the header and runs every time each page is loaded, google analytics type thing. survey.php

I want this code to run only for each new visitor of the website, so that I only log each new visitor.

I guess this is done by setting a cookie so I wrote this other code to set a cookie and then include the survey.php only when there is no cookie. I've used the ! function in isset to run the if statement if it isn't present but it is not setting the cookie and is running the script upon each load.

Based on what it says in the manual I can't see where I am going wrong.

<?php 
if(!isset($_COOKIE['Name'])){
    setcookie("Name");
    include ('survey.php');
}
?>

EDIT: I have added a value, tested it with multiple users, and the script still runs each load. No idea why.


Solution

  • RESOLVED

    I was including the code as the first piece of code but not on the first line.

    A code to set cookies has to begin before everything, on line 1.

    Posting this so others can benefit.