Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I found a snippet of code that redirects if it's the first visit, but when I tried to use it, it just stayed at that code. I don't really understand too much about the cookies and how it works, so maybe you can help! Here's the PHP code:

    <?php

    session_start();

    if (isset($_SESSION['FirstVisit'])) {

    $_SESSION['FirstVisit'] = 1;

    header("Location: http://example.com/index.php");

    // Don't forget to add http colon slash slash www dot before!

    }

?>

So how could I fix it so if it's your first visit to the site it brings you to a certain page, and if not, the default?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
278 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can use this code:

<?php
if (!isset($_COOKIE['firsttime']))
{
    setcookie("firsttime", "no", /* EXPIRE */);
    header('Location: first-time.php');
    exit();
}
else
{
    header('Location: site.php');
    exit();
}
?>

It will check if you have a cookie named "firsttime" and if not, it will create it and redirect to your FIRSTTIME page... If yes, it will just redirect you to the website...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...