What is the best way to password protect folder/page using php without a db or username

后端 未结 5 1026
温柔的废话
温柔的废话 2020-11-28 04:29

What is the best way to password protect folder using php without a database or user name but using. Basically I have a page that will list contacts for organization and ne

5条回答
  •  抹茶落季
    2020-11-28 05:03

    I doubt if this would count as the best wasy of doing it, but it would work. And since security doesn't seem to be a big issue for you, the fact that this way's as insecure as hell probably won't bother you either.

    Have a login.php page that takes a password and then sets a cookie if the login details are correct. Each php file can then check for the existence of the cookie to determine whether or not the user is "logged in" or not, and display information accordingly.

    login.php
    ...
    if(isset($_POST['password']) && $_POST['password'] == 'my_top_secret_word') {
        setcookie('loggedin', 'true', time() + 1200, '/url/');
    } else {
        setcookie('loggedin', 'false', time() - 1200, '/url/');
        // display a login form here
    }
    etc
    

    each "protected" page would then check for this cookie:

    if(isset($_COOKIE['loggedin'])) {
        if($_COOKIE['loggedin'] == 'true') {
            $showHidden = true;
        } else {
            $showHidden = false;
        }
    } else {
        $showHidden = false;
    }
    

    I'm sure you get the (highly insecure) idea ...

提交回复
热议问题