Php MySql Authen
phpAuthMySQLCheck.php
<?
session_start();
function fncAuthentication() {
header('WWW-Authenticate: Basic realm="localhost"');
header("HTTP/1.0 401 Unauthorized");
exit;
}
//*** Check user ***//
if (trim($_SERVER['PHP_AUTH_USER'])!= "" and trim($_SERVER['PHP_AUTH_PW']) != "")
{
mysql_connect("localhost","root","root");
mysql_select_db("mydatabase");
$strSQL = "SELECT * FROM user WHERE User = '".trim($_SERVER['PHP_AUTH_USER'])."'
and Password = '".trim($_SERVER['PHP_AUTH_PW'])."'";
$objQuery = mysql_query($strSQL);
if(mysql_fetch_array($objQuery))
{
$_SESSION["strUser"] = $_SERVER['PHP_AUTH_USER'];
$_SESSION["strPass"] = $_SERVER['PHP_AUTH_PW'];
}
mysql_close();
}
//*** Session ***//
if(trim($_SESSION["strUser"])=="" or trim($_SESSION["strPass"])=="")
{
fncAuthentication();
exit();
}
?>
phpAuthenticationMySQL1.php
<? require_once("phpAuthMySQLCheck.php"); ?> <html> <head> <title>ThaiCreate.Com PHP & Authentication</title> </head> <body> <?php echo "<center><h1>Welcome ".$_SESSION["strUser"]." </h1><br><br><h2>Now Page 1</h2><center>" ?> <center><a href="phpAuthenticationMySQL2.php">Page 2</a><center> </body> </html>phpAuthenticationMySQL2.php<? require_once("phpAuthMySQLCheck.php"); ?> <html> <head> <title>ThaiCreate.Com PHP & Authentication</title> </head> <body> <?php echo "<center><h1>Welcome ".$_SERVER['PHP_AUTH_USER']." </h1><br><br><h2>Now Page 2</h2><center>" ?> <center><a href="phpAuthenticationMySQL1.php">Page 1</a><center> </body> </html>

