Old but Gold · 20/12/2021

How to Password Protect a Web Page – euforya.net

How to Password Protect a Web Page

How to Password Protect a Web Page
  1.  Create a demo.php page in your text editor software like: note pad ++ and insert this code:

<?php require("protect_page.php"); ?>

This is my page!

2. Create a protect_page.php  page in your text editor software like: note pad ++ and insert this code:

<?php

// Configuration
// your password. replace password with your password
$mypassword = "demo";
// your domain (without www. and http://), replace domain.com with your domain
$mydomain = "domeniu.com";

$mypassword = sha1($mypassword); // password is encrypted using sha1
if(sha1($_POST['password'])==$mypassword && $_COOKIE["pass"] != $mypassword){

setcookie("pass", sha1($_POST['password']), time()+315569250, "/", ".$mydomain"); // set the cookie
$_COOKIE["pass"] = sha1($_POST['password']);
}

if($_COOKIE["pass"]!=$mypassword){
?>

<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN"
"http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head>
<title>Please login</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />

<style type="text/css">
html{
text-align: center;
}
</style>

</head>

<body>
<h1>Password required!</h1>
<p>In order to access this page, you need to type your password.</p>
<p>(For this demo, the password is demo).</p>

<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input type="password" name="password" />
<input name="pass" type="submit" class="submit" id="submit" value="Submit" />
</form>

</body>

</html>

<?php
exit();
}

?>

3.  Test this script on your server by uploading the files using you favorite ftp program