Mega Code Archive

 
Categories / Php / MySQL Database
 

PHP4 AND MySQL Authentication

<?php require ("auth.php"); if(!isset($PHP_AUTH_USER)) { Header("WWW-Authenticate: Basic realm=\"User Login\""); Header( "HTTP/1.0 401 Unauthorized"); echo "You failed to provide the correct password....\n"; exit; } else { $con = mysql_pconnect ("$host", "$user", "$pass") or die("Error: " . mysql_error()); mysql_select_db ("$db"); $user_id = strtolower($PHP_AUTH_USER); $result = mysql_query("SELECT password FROM signup " . "Where username = '$user_id'"); $row = mysql_fetch_array($result); if ($PHP_AUTH_PW != $row["password"]) { Header( "WWW-Authenticate: Basic realm=\"Login failed please try again!\""); Header( "HTTP/1.0 401 Unauthorized"); echo "You failed to provide the correct password....\n"; exit; } } ?> Make a simple php file called auth.php with the variables $host $user $pass $db to connect to your MySQL server. The is a table called signup and a field named username & password.