Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

First Time Visitor Cookie

Tags:

php

cookies

For my site, I want to create a link in the middle of the page for first time visitors that says something like "First time, click here" using PHP.

like image 535
Ryan Avatar asked Apr 19 '11 21:04

Ryan


1 Answers

You could in theory do it with cookies, but there's no guaranteed way to detect "first time visitors" other than asking them to register for an account and show them the message the first time they log in.

The reason is that cookies may get cleaned from the browser, people may change computers / browsers, cookies will eventually expire and depending on what your goal is you may end up annoying existing users assuming they're new.

Anyway, enough of that. Your code could look something like this:

<?php
    // Top of the page, before sending out ANY output to the page.
        $user_is_first_timer = !isset( $_COOKIE["FirstTimer"] );

    // Set the cookie so that the message doesn't show again
        setcookie( "FirstTimer", 1, strtotime( '+1 year' ) );
?>




<H1>hi!</h1><br>


<!-- Put this anywhere on your page. -->
<?php if( $user_is_first_timer ): ?>
    Hello there! you're a first time user!.
<?php endif; ?>

Cheers!

like image 57
0x6A75616E Avatar answered Oct 19 '22 14:10

0x6A75616E