How can I restrict access to a .php page?

Posted on 2006-03-29
Last Modified: 2010-04-20
I have PHP5 running on Windows 2003/IIS6. I need to restrict access to a certain .php page to a single user.  How can I best accomplish this task?
Question by:avsc
    LVL 3

    Accepted Solution

    Dear Avsc.

    There is a really simple work around to this problem.

    Basically, if you only have one user, you can do this:

          if ($_POST['action'] == "login"){
                if ($_POST['password'] == "PASSWORD"){
                $icp = true;

    Then at the top of your page body (Just below <BODY> tag), add this:

    <? if($_COOKIE['UserLoggedIn'] != "235c7n8ob72n45v8n2mc8nc2p96by2cc025y8n75c0bnv3860c80"){ ?>
    <form id="login" name="login" method="post" action="">
            <input name="action" type="hidden" id="action" value="submit" />
          Login to access this page:
          <input name="password" type="password" class="text" id="password" />
             <input name="Submit" type="submit" class="text" value="Login" />
            <? if ($icp == true){ ?>
            <? } ?>
    <? }else{ ?>

    Then you put your body here, then lastly, end the page with <? } ?> to end the if fucntion started earlier on.
    LVL 16

    Expert Comment

    Another solution without cookie

    include this php code at the beginning of your php file. Just change the the password to whatever you want!!

    // Authenticate                                                            //


    if (!isset($PHP_AUTH_USER) && !isset($PHP_AUTH_PW)) {
       Header("WWW-Authenticate: Basic realm=Authenticate");
       echo "Authentication required!";  
    if ($PHP_AUTH_USER == '') {
       Header("WWW-Authenticate: Basic realm=Authenticate");
       echo "Authentication required!";
    if ($PHP_AUTH_USER != 'yourUserid' || $PHP_AUTH_PW != 'yourPassword') {
       Header("WWW-Authenticate: Basic realm=Authenticate");
       echo "Authentication required!";    
    LVL 51

    Expert Comment

    AFAIK $_SERVER["PHP_AUTH_PW"] is not guarateed to be set by the server, depends on the web server

    Featured Post

    Network it in WD Red

    There's an industry-leading WD Red drive for every compatible NAS system to help fulfill your data storage needs. With drives up to 8TB, WD Red offers a wide array of solutions for customers looking to build the biggest, best-performing NAS storage solution.  

    Join & Write a Comment

    This is a guide to setting up a new WHM/cPanel Server to be used for web hosting accounts. It is intended for web hosting company administrators and dedicated server owners. For under $99 per month (considering normal rate of Big Data Cetnters like …
    Have you ever sent email via ColdFusion and thought of tracking this mail to capture the exact date and time when the message was opened ?  If yes, then this article is for you ! First we need a table user_email with columns user_id , email , sub…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now