Ask Login for Password

Asked

Viewed 2,122 times

1

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

Here’s the code I already got on 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 whole directory. I believe the password needs to be stored in a PHP file so no one can find it.

Thank you.

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>
  • How to set the password and the correct user?

  • I updated take a look now...

  • In case I set the user as Hugo and password as 1234

  • Thank you :) I will test.

  • I tested it, but here’s the thing:

  • Strict Standards: Non-static method Authenticator::check() should not be called statically in /home/u146117321/public_html/news/index.php on line 25 Strict Standards: Non-static method Authenticator:::check() should not be called statically in /home/u146117321/public_html/news/index.php on line 40

  • Worked??????

  • I tested it here and it worked out good :)

  • I’m trying to fix the code

  • 1

    Get my answer! I already updated and corrected for you!

  • Thanks :) It seems that fixed even.

  • You can learn more about this here: http://php.net/manual/en/features.http-auth.php

  • Just be careful in using these headers because depending on the browser are ignored and the user can access freely. In some cases remains blocked even by entering correct login and password.

  • Yep and can use hash functions in tbm passwords.

Show 9 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.