HTTP authentication with PHP for specific pages

I have been trying to fix one issue for the last two weeks but didn't find any solution to my problem. So, I am trying to add HTTP authentication with PHP 7.4.

The main issue

I have to add restrictions for specific URLs ( For example, https://example.com/photos ) with PHP, and I am using the below code. But the problem is I have to show logged-in users on other pages, too. For example, if I didn't log in, then anything to show, but if I am logged in need to show username and password on all pages, but again I have to restrict only specific pages.

PHP Code:

Header("WWW-Authenticate: Basic realm='Members Only' valid-user");
http_response_code(401);
echo "<script> window.location.replace( 'https://example.com/sign-up' ) </script>";
exit;

.htaccess Code:

AuthUserfile PATH_TO_THE_FILE/.htpasswd
AuthName "Members Only"
AuthType Basic
AuthBasicProvider file

I hope someone will help me soon.

1 answer

  • answered 2022-05-07 02:52 Drew Jones

    This is what I use

    Put these 2 files in a folder...change the password fields and the refresh redirect code once correct password is entered.

    index.php

        <meta name='robots' content='noindex, nofollow' />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=5.0, minimum-scale=1">
    
    <?php
    session_start();
    
    if(isset($_POST['submit_pass']) && $_POST['pass'])
    {
     $pass=$_POST['pass'];
     if($pass=="12345678")
     {
      $_SESSION['password']=$pass;
     }
     else
     {
      $error="Incorrect Pssword";
     }
    }
    
    if(isset($_POST['page_logout']))
    {
     unset($_SESSION['password']);
    }
    ?>
    
    <html>
    <head>
    <link rel="stylesheet" type="text/css" href="password_style.css">
    </head>
    <body>
    <div id="wrapper">
    
    <?php
    if($_SESSION['password']=="12345678")
    {
     ?>
     
     <meta http-equiv="refresh" content="0;url=https://example.com">
    
     <?php
    }
    else
    {
     ?>
     <form method="post" action="" id="login_form">
      <h1>LOGIN TO PROCEED</h1>
      <input type="password" name="pass" placeholder="*******">
      <input type="submit" name="submit_pass" value="SUBMIT">
      <p><font style="color:red;"><?php echo $error;?></font></p>
     </form>
     <?php  
    }
    ?>
    
    </div>
    </body>
    </html>
    

    password_style.css

    body
    {
     margin:0 auto;
     padding:0px;
     text-align:center;
     width:100%;
     font-family: "Myriad Pro","Helvetica Neue",Helvetica,Arial,Sans-Serif;
     background-color:#8A4B08;
    }
    #wrapper
    {
     margin:0 auto;
     padding:0px;
     text-align:center;
     width:995px;
    }
    #wrapper h1
    {
     margin-top:50px;
     font-size:45px;
     color:white;
    }
    #wrapper p
    {
     font-size:16px;
    }
    #logout_form input[type="submit"]
    {
     width:250px;
     margin-top:10px;
     height:40px;
     font-size:16px;
     background:none;
     border:2px solid white;
     color:white;
    }
    #login_form
    {
     margin-top:200px;
     background-color:white;
     width:350px;
     margin-left:310px;
     padding:20px;
     box-sizing:border-box;
     box-shadow:0px 0px 10px 0px #3B240B;
    }
    #login_form h1
    {
     margin:0px;
     font-size:25px;
     color:#8A4B08;
    }
    #login_form input[type="password"]
    {
     width:250px;
     margin-top:10px;
     height:40px;
     padding-left:10px;
     font-size:16px;
    }
    #login_form input[type="submit"]
    {
     width:250px;
     margin-top:10px;
     height:40px;
     font-size:16px;
     background-color:#8A4B08;
     border:none;
     box-shadow:0px 4px 0px 0px #61380B;
     color:white;
     border-radius:3px;
    }
    #login_form p
    {
     margin:0px;
     margin-top:15px;
     color:#8A4B08;
     font-size:17px;
     font-weight:bold;
    }
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum