Reputation: 558
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?
Upvotes: 8
Views: 34053
Reputation: 2223
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...
EDIT 2021 Please don't use this old method and either use a framework or better code. This is 10 years old now.
Upvotes: 21
Reputation: 10830
<?php
session_start();
if (!isset($_SESSION['FirstVisit'])) {
//show site for the first time part
$_SESSION['FirstVisit'] = 1;
header("Location: http://example.com/index.php");
// Don't forget to add http colon slash slash www dot before!
} else { Show normal site }
?>
You just make an if statement to check whether there is a session set, if not, you know its there first time. Though, since it is not a cookie, anytime you quit the browser, it will assume it is the first time, even if it is never the first time.
Upvotes: 5
Reputation: 1
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-
ui.css" />
<script src="http://code.jquery.com/jquery-1.8.3.js"></script>
<script src="/resources/demos/external/jquery.bgiframe-2.1.2.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script>
$(function() {
$( "#dialog" ).dialog();
});
</script>
<?php
if (!isset($_COOKIE['firsttime']))
{
setcookie("firsttime", "no", /* EXPIRE */);
header('Location: first-time.php');
exit();
}
else
{
?>
<div id="dialog" title="Basic dialog">
<p>text</p>
</div>
<?
}
?>
@ Frederick or PeeHaa would the above script also work to bring up a window before they enter the site rather than a page.
Upvotes: 0
Reputation: 1143
<?php
@session_start();
$url = 'http://blah.com/default/';
if (!isset($_COOKIE['Visited'])) {
$_COOKIE['Visited'] = 1;
$url = 'http://blah.com/firstvisit/';
}
header("Location: {$url}");
?>
Upvotes: 0
Reputation: 72672
For more information see the docs.
<?php
if (!isset($_COOKIE['visited'])) { // no cookie, so probably the first time here
setcookie ('visited', 'yes', time() + 3600); // set visited cookie
header("Location: http://example.com/index.php");
exit(); // always use exit after redirect to prevent further loading of the page
}
?>
Upvotes: 1
Reputation: 14277
If sessions/cookies are to difficultly you can save the IP of the visitor. When IP exist show page 1 when IP is new redirect to other page?
Upvotes: 1