View Full Version : PHP Password Script

11-04-2009, 06:29 AM
I have a password script that I would like to embed php in. Ex:


$myusername = "usar";
$mypassword = "passward";
$areaname = "Area";

if ($_SERVER["PHP_AUTH_USER"] == "" || $_SERVER["PHP_AUTH_PW"] == "" || $_SERVER["PHP_AUTH_USER"] != $myusername || $_SERVER["PHP_AUTH_PW"] != $mypassword) {
header("HTTP/1.0 401 Unauthorized");
header("WWW-Authenticate: Basic realm=\"$areaname\"");
echo "<h1>Authorization Required</h1>";


I'm wondering two things. First is only if option 2 is not available, which is... If the password is incorrect how can I redirect? I found:

header('Location: http://www.example.com/');

But it's not working.

Option 2. I was hoping to display a custom error in php, but anything after the code above is password protected... So is it possible to get PHP inside there to use it as a custom message or would I have to redirect to a php page for this?

11-04-2009, 08:27 AM
See this page for http authentication: http://ca.php.net/manual/en/features.http-auth.php

It should be noted that this will only run on builds for Apache module php, not on IIS or CGI based versions.