![](/img/trans.png)
[英]How to redirect to another page and force refresh on that page so Javascript will run
[英]how to force redirect to login page in javascript
我正在建立一個網站,希望用戶必須登錄才能查看證書頁面(在這種情況下,它稱為gallery.php)。我實現了在此網站上找到的一些代碼,該代碼將用戶重定向到單擊菜單中的圖庫鏈接時的登錄頁面。 但是,當我登錄並再次重定向到圖庫時,網站不承認我已登錄,然后再次將我重定向回登錄頁面。 我知道以前曾提出過類似的問題,但是可以提供任何幫助。 我的代碼在下面
//from login.html
<!--//
/*This Script allows people to enter by using a form that asks for a
UserID and Password*/
function pasuser(form) {
if (form.id.value=="Admin") {
if (form.pass.value=="rycbar123") {
var sessionTimeout = 1; //hours
var loginDuration = new Date();
loginDuration.setTime(loginDuration.getTime()+(sessionTimeout*60*60*1000));
document.cookie = "SistaDansenSession=Valid; "+loginDuration.toGMTString()+"; path=gallery.php";
location="gallery.php"
} else {
alert("Invalid Password")
}
} else { alert("Invalid UserID")
}
}
//from gallery.php
<!--force login code-->
<script type="text/javascript">
if (document.cookie.indexOf("SistaDansenSession=Valid") == -1) {
alert("You must be logged in to view this page")
location.href = "login.html";
}
else {
alert("login succesful")
} </script>
非常感謝
嘗試更改:
if (document.cookie.indexOf("SistaDansenSession=Valid") == -1)
至
if (document.cookie.indexOf("SistaDansenSession") < 0)
設置cookie時,將cookie屬性“ SistaDansenSession”設置為等於“有效”值。 在您的原始代碼中,您嘗試檢索cookie屬性“ SistaDansenSession = Valid”,而要檢索的屬性只是“ SistaDansenSession”。 設置cookie時,該屬性存在,因此它將具有> = 0的索引。在那種情況下,不會發生重定向到登錄名的操作-我認為這是您想要的行為。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.