代码之家  ›  专栏  ›  技术社区  ›  Banjer_HD

你能用密码保护html页面吗?[已关闭]

  •  -1
  • Banjer_HD  · 技术社区  · 9 年前

    例如:

    • PrivatePage1.html
    • PrivatePage2.html

    我在“Login.html”上的代码

    https://hastebin.com/egehaboxig.js

    password();
    
    function password() {
        var userPassword;
        var myPassword = "a";
    
        userPassword = prompt("Enter the password:");
    
        if(userPassword == myPasswordfun) {
            alert('Correct, press ok to enter the site.');
        }else if(userPassword == null) {
            window.location ="http://www.google.com";
        }else {
            alert('Incorrect');
            password();
        }
    }
    

    谢谢你的阅读!

    2 回复  |  直到 9 年前
        1
  •  1
  •   Brandon C    9 年前

    我会避免使用 localStorage

    PHP有一个内置变量 $_SESSION 用于处理此问题。首先,您必须在服务器上用 session_start() 命令用户输入密码后,您可以对其进行验证,然后设置 $_SESSION['valid'] = TRUE 。我们可以检查用户是否使用以下功能进行了验证。

    public function is_valid() {
        //Returns true if session is valid, otherwise returns false.
        if( isset( $_SESSION['valid'] ) ) {
          return $_SESSION['valid'];
        }
        else {
          return FALSE;
        }
    }
    

    当用户关闭浏览器窗口时,会话结束。

        2
  •  0
  •   Scott Marcus    9 年前

    您需要将其成功登录存储在某个位置(可能是最安全的服务器)或客户端(不太安全)。

    有几种机制可以工作,但在客户端, localStorage

    成功登录后,将值设置为 本地存储

    localStorage.setItem("loggedIn", "true");
    

    并且,在您想要检查是否登录的每个页面上,您会写下:

    if(localStorage.getItem("loggedIn") === "true"){
        // user is logged in
    } else {
        // user is not logged in
    }