Link to home
Start Free TrialLog in
Avatar of avsc
avscFlag for United States of America

asked on

How can I restrict access to a .php page?

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?
ASKER CERTIFIED SOLUTION
Avatar of gileze33
gileze33

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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                                                            //
//============================================================================//

$PHP_AUTH_USER=$_SERVER["PHP_AUTH_USER"];
$PHP_AUTH_PW=$_SERVER["PHP_AUTH_PW"];

if (!isset($PHP_AUTH_USER) && !isset($PHP_AUTH_PW)) {
   Header("WWW-Authenticate: Basic realm=Authenticate");
   echo "Authentication required!";  
   exit;
   }
   
if ($PHP_AUTH_USER == '') {
   Header("WWW-Authenticate: Basic realm=Authenticate");
   echo "Authentication required!";
   exit;
   }
                    
if ($PHP_AUTH_USER != 'yourUserid' || $PHP_AUTH_PW != 'yourPassword') {
   Header("WWW-Authenticate: Basic realm=Authenticate");
   echo "Authentication required!";    
   exit;
   }
AFAIK $_SERVER["PHP_AUTH_PW"] is not guarateed to be set by the server, depends on the web server