Request Password When Enter Page

1

Well, I have a Page and want to protect it, But with htaccess, everything in the directory is closed. I just want to ask for a Password and User Name to enter it, not the other things in the directory.

Here is the code I've already gotten in it:

<?php
  $pagina = "content.html";
  if(isset($_POST)){
    if($_POST["conteudo"]){
      $fopen = fopen($pagina,"w+");
      fwrite($fopen,$_POST["conteudo"]);
      fclose($fopen);
    }
  }
?>
<head>
<title>Edit Content Modal</title>
</head>
<br>
<br><br><br>
<form method="post">
  <textarea name="conteudo" style="height: 400px; width: 800px; border: none; margin-left: 27%;" placeholder="Type Here the Code..."><?= file_get_contents($pagina); ?></textarea>
  <br><br><input style="margin-left: 50%;" type="submit" value="Save Changes"/>
</form>

I want to put only one Password on this page (It will serve to edit another). But only in it, not in the entire directory. I believe that the password needs to be stored in a PHP file so no one will discover it.

Thank you.

    
asked by anonymous 09.05.2015 / 16:54

1 answer

2
<?php
class Authenticator {

  public static $username = "hugo";
  public static $password = "1234";

  public static function check() {
    if (
      isset($_SERVER['PHP_AUTH_USER']) &&
      isset($_SERVER['PHP_AUTH_PW']) &&
      $_SERVER['PHP_AUTH_USER'] == self::$username &&
      $_SERVER['PHP_AUTH_PW'] == self::$password
    ) {
      return true;
    } else {
      header('WWW-Authenticate: Basic realm="Please login."');
      header('HTTP/1.0 401 Unauthorized');
      die("Usuário ou senha incorretos!");
    }
  }

}

if(Authenticator::check()){
  echo "Você tem acesso ao conteúdo!";
  $pagina = "content.html";
  if(isset($_POST)){
    if(isset($_POST["conteudo"])){
      $fopen = fopen($pagina,"w+");
      fwrite($fopen,$_POST["conteudo"]);
      fclose($fopen);
    }
  }
}
else return null;
?>
<head>
<meta charset="utf8">
<title>Edit Content Modal</title>
</head>
<body>
<form method="post">
  <textarea name="conteudo" style="height: 400px; width: 800px; border: none; margin-left: 27%;" placeholder="Type Here the Code..."><?= file_get_contents($pagina); ?></textarea>
  <br><br><input style="margin-left: 50%;" type="submit" value="Save Changes"/>
</form>
</body>
</html>
    
09.05.2015 / 17:13