3

Is there a way to detect if a user is visiting my website for the first time, and if so, redirect them to a page, for example, index-first-time-visitor.php - and if they are not a first time visitor, it sends them to index.php

If there is a way to do this, please demonstrate how.

Zach Nicodemous
  • 9,097
  • 9
  • 45
  • 68
  • 1
    Cookies, IP address storage, sessions, etc... You need to define your architecture a little more. This is overly broad to get a concrete answer. – George Johnston Oct 10 '11 at 14:49
  • 1
    set a cookie to never expire when they visit (give it 9999999999999), if they have the cookie on the second visit, then redirect them to the not-first-time-index.php its not perfect, because people like to clear their cashes and cookies all the time, but its the best your going to get. – Waltzy Oct 10 '11 at 14:54

1 Answers1

12

Put this at the top of your index.php

<?php
if ($_COOKIE['iwashere'] != "yes") { 
  setcookie("iwashere", "yes", time()+315360000);  
  header("Location: http://example.com/index-first-time-visitor.php"); 
}
?>

The cookie will live for 10 years, after that, a user will be a "new" visitor again.

Christian Bock
  • 441
  • 3
  • 6