简体   繁体   English

检查是否设置了Cookie,然后加载页面,否则重定向页面

[英]Check if Cookie is set then load the page else redirect page

I do have a splash page for my website which should ask users for confirmation before entering the site. 我的网站上确实有一个启动页面,应该在进入网站之前先征求用户的确认。 I implemented a "Remember me" checkbox there, so if users check that it will set a cookie so in the next visit page will be redirected automatically. 我在那里实现了“记住我”复选框,因此,如果用户检查它会设置cookie,那么在下一个访问页面中,它将自动重定向。

The issue i've just ran into it is i want to set a conditional in my header page, so it should check if user is already have the Cookie it should show the page content and if not it should redirect to the splash page. 我刚遇到的问题是我想在我的标题页中设置一个条件,因此它应该检查用户是否已经具有Cookie,它应该显示页面内容,否则应该重定向到初始页面。

Is there any way either with PHP or JS PHP或JS有什么办法吗

Here is the Splash page code 这是启动页面代码

   <!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
<!-- JQuery -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$(document).ready(function(e) {
    $('#remember').click(function() {
        if (this.checked) {
            addCookie(30);
        } else {
            deleteAllCookies();
        }
    });
});

function deleteAllCookies() {
    var cookies = document.cookie.split(";");
    for (var i = 0; i < cookies.length; i++) {
        var cookie = cookies[i];
        var eqPos = cookie.indexOf("=");
        var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
        document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
    }
}

function addCookie(exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires=" + d.toGMTString();
    document.cookie = expires;
}

function checkCookie() {
    if (document.cookie == false) {
    } else if (document.cookie.indexOf("expires") >= 0) {
        window.location.href = "https://www.google.com";
    }
}

function goTo() {
        window.location.href = "https://www.google.com";
}
</script>
</head>
<body onLoad="checkCookie();">
<h1>Splash Page</h1>
<form>
<input type="checkbox" id="remember"> Remember me
<br>
<input type="button" value="Enter" onClick="goTo();">
</form>
</body>
</html>

@totallytotallyamazing @totallytotallyamazing

I used JS, Jquery, and (PHP just for testing), for this solution. 对于该解决方案,我使用了JS,Jquery和(仅用于测试的PHP)。

Here is the DEMO . 这是演示 (I slightly altered the previous Splash page Demo to accommodate this Demo.) (为了适应此演示,我对先前的启动页面演示进行了一些更改。)

Here is the code: 这是代码:

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
<!-- JQuery -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
function checkCookie() {
    if (document.cookie == false) {
        $('#x').text("No Cookie.");
        window.location.href = "http://www.totallytotallyamazing.com/jsFiddle/cookie/indexNoAlert.html";
    } else if (document.cookie.indexOf("expires") >= 0) {
        $('#x').text("You have Cookie.");
    }
}
</script>
</head>
<body onLoad="checkCookie();">
<h1>My Website</h1>
<p><div id='x'></div></p>
<?php
session_start();
if(isset($_COOKIE['expires'])) {
  echo "Here is your expiration date " . $_COOKIE['expires'] . "!<br>";
}
?>
</body>
</html>

After the page does it's final redirect, reload the page one more time to see the "expires" cookie display is expiration date. 页面完成最终重定向后,请重新加载页面一次,以查看“ expires” cookie显示的是过期日期。

I hope this works for you, please let me know. 希望这对您有用,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM