然后加载页面否则重定向页面

然后加载页面否则重定向页面

本文介绍了检查Cookie是否已设置,然后加载页面否则重定向页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站有一个初始页面,在进入网站之前应该要求用户确认。
我实现了一个记住我复选框,如果用户检查它将设置一个cookie,所以在下一个访问页面将被自动重定向。



我刚刚遇到的问题是我想在我的标题页面中设置一个条件,所以它应该检查用户是否已经有Cookie它应该显示页面内容,如果不是它应该重定向到初始页面。 / p>

使用PHP或JS有任何方式



这里是Splash页面代码

 <!doctype html> 
< html>
< head>
< meta charset =UTF-8>
< title>无标题文档< / 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页面< / h1>
< form>
< input type =checkboxid =remember>记住我
< br>
< input type =buttonvalue =EnteronClick =goTo();>
< / form>
< / body>
< / html>

@totallytotallyamazing

解决方案

我使用JS,Jquery和(PHP仅用于测试)。



这里是。 (我稍微改变了以前的Splash页面Demo以适应这个Demo。)



这里是代码:

 <!doctype html> 
< html>
< head>
< meta charset =UTF-8>
< title>无标题文档< / 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>我的网站< / h1>
< p>< div id ='x'>< / div>< / p>
<?php
session_start();
if(isset($ _ COOKIE ['expires'])){
echo这是你的到期日期。 $ _COOKIE ['expires']。 !< br>;
}
?>
< / body>
< / html>

页面完成重定向后,重新加载页面一次,



我希望这对您有效,请让我知道。


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.

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.

Is there any way either with PHP or 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

解决方案

I used JS, Jquery, and (PHP just for testing), for this solution.

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.

I hope this works for you, please let me know.

这篇关于检查Cookie是否已设置,然后加载页面否则重定向页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 14:57