Password-protected web page

Extra300S Posted messages 1 Status Member -  
jordane45 Posted messages 30426 Registration date   Status Moderator Last intervention   -
Hello,
How to protect a web page with a password that I can change at will? JavaScript preferred, not too much PHP
Thanks

Configuration: Windows / Firefox 64.0

5 answers

jordane45 Posted messages 30426 Registration date   Status Moderator Last intervention   4 830
 
Hello,
No matter if you are an amateur or a beginner... this kind of question is addressed millions of times on the internet...
A simple search gives the answer without difficulty...

Anyway,

On EACH file, you place (at the beginning of the file....) the line of code
 session_start(); 


You then create an HTML form to login:
 <?php //login page.php //start of sessions session_start(); //display PHP errors error_reporting(E_ALL); ini_set('display_errors', TRUE); ini_set('display_startup_errors', TRUE); //proper retrieval of variables BEFORE using them $login = !empty($_POST['login']) ? $_POST['login'] : NULL; $pwd = !empty($_POST['pwd']) ? $_POST['pwd'] : NULL; //login processing : $error = ""; if($login && $pwd ){ if($login =="theloginyouwant" && $pwd == "thepassword"){ //if the credentials are correct... //fill a session variable $_SESSION['connexion'] = true; //redirect to the desired page : header('location : url/of/the/page/you-want.php'); exit(); }else{ unset($_SESSION['connexion']); // destroy the session variable $error = "Incorrect credentials!"; } } ?> <div> <form method="post" action=""> <input type="text" name="login"> <input type="password" name="pwd"> <input type="submit" value="login"> </form> </div> <div> <?php echo $error; ?> </div> 


and on pages that must be secured.. you add the code:
 if(empty($_SESSION['connexion']) && $_SESSION['connexion'] != true ){ header('location : url/page/connexion.php'); exit(); } 
3