Zend - The PHP Company




HTML

Add Code


Authentication with PHP and MySQL  

Type: code fragment
Added by: anderson
Entered: 05/03/2000
Last modified: 08/12/1999
Rating: **** (9 votes)
Views: 15713
You may use this code to authenticate users to view some page. The password field on MySQL should be entered as password("somepasshere"), to increase the security of the datas.


<? 

/* By Christiano Anderson - anderss@zaz.com.br */
/* http://www.anderson.eti.br */

if(!isset($PHP_AUTH_USER)) { 
Header("WWW-authenticate: basic realm="Login Required""); 
Header("HTTP/1.0 401 Unauthorized"); 
$title="Login screen"
?> 
<center><font face="verdana" size="2" color="#FF0000"> 
Username and Password are required to view this page.<br>
<br>
Contact the webmaster
<br>
<br>
</font> </center>
<? 
exit; 
} else { 
mysql_pconnect("host_here","login_here","password_here") or die("Unable to connect to SQL server"); 
mysql_select_db("database_here") or die("Unable to select database"); 
$user_id=strtolower($PHP_AUTH_USER); 
$password=$PHP_AUTH_PW
$query mysql_query("select * from users where username='$user_id' and  senha=password('$password')"); 
if(!
mysql_num_rows($query)) { 
Header("WWW-authenticate: basic realm="Auth Required""); 
Header("HTTP/1.0 401 Unauthorized"); 
$title="Login Instructions"
 
?> 
<center><font face="verdana" size="2" color="#FF0000"> 
Username and Password are required to view this page.<br>
<br>
Contact the webmaster
<br>
<br>
</font> </center>
</font> </center>
<?
 
exit; 
  } 
     } 
 
?> 


Usage Example




Rate This Script





Search



This Category All Categories