Forum Moderators: coopster
Thanks for the opportunity to ask help on your forum. I hope you can help me solve the following:
I'd like visitors to my website's initial page (index.html) to be redirected to a different page (welcome.html) if a cookie is not found.
I was able to accomplish this with the following script, but unfortunately the process is too slow and the initial page (index.html) gets half way rendered before the redirection takes place.
I was advised to use PHP to accellerate the process, but I have no idea how to do it. Would you please show me the equivalent of the following code in PHP?
Thanks,
Billy
<script type="text/javascript" src="scripts/jquery-1.4.min"></script>
<script type="text/javascript" src="scripts/jquery.cookie.js"></script>
<script type="text/javascript">
$(function() {
var COOKIE_NAME = 'splash-page-cookie';
$go = $.cookie(COOKIE_NAME);
if ($go == null) {
$.cookie(COOKIE_NAME, 'test', { path: '/', expires: 1 });
window.location = "welcome.html"
}
else {
}
});
</script>
At the start of your page before anything else try to put the following code:
<?PHP
$cookie_name = 'cookie_name'; // name of cookie
$cookie_value = 'redirection'; // what we gonna store in cookie
$cookie_expire = time() + 28800; // expiration date of cookie 8 hours from now
$cookie_domain = '.example.com'; // where cookie is valid// check if cookie exists ornot and redirects to proper page
if (!isset($_COOKIE[$cookie_name]))
{
// create cookie and redirect
setcookie($cookie_name, $cookie_value, $cookie_expire, '/', $cookie_domain);
header("Location: page-one.htm");
}
else
{
header("Location: page-two.htm");
}
?>
- anything after [//] is comment... you can ignore these. I put them here for easy refernce tor you
The above code creates and stores a cookie
Again thank you for your help.
While trying your code it was pointed out to me that if someone has cookies disabled, then they would be redirected from the index page (index.php) to the welcome page (welcome.php) in an infinite loop.
In view of that, if you get the opportunity, would you please add some code for the index page to test if cookies are enabled (maybe by setting a test cookie and then deleting it)? Then I suppose the rest of the code you wrote above would need to go into action redirecting only if cookies were found to be enabled.
Thanks,
Billy