PHP user authentication and management complete source code
Last Update:2017-02-28
Source: Internet
Author: User
<?php
$id = "xxxcom";
if (!isset ($PHP _auth_user)) {
Header ("Www-authenticate:basic realm=" "$id");
Header ("http/1.0 401 Unauthorized");
Require (' error.inc ');
Exit
}
$name = $PHP _auth_user;
$pass = $PHP _AUTH_PW;
Require ("Connect.inc");
$query = "SELECT * from auth where username= ' $name ' && realm= ' $id '";
$result = Mysql_db_query ("admin", $query);
if (mysql_num_rows ($result) = = 0) {
Header ("Www-authenticate:basic realm=" "$id");
Header ("http/1.0 401 Unauthorized");
Require (' error.inc ');
Exit
}
$active = mysql_result ($result, 0, "active");
if ($active = = ' no ') {
?>
<HTML><HEAD>
<title>404 not found</title>
</HEAD><BODY>
The requested URL
? echo $REQUEST _uri;?>
Wasn't found on this server.<p>
</BODY></HTML>
<?php
Exit
}
?>
--End Auth.inc--
--Begin Connect.inc--
<?php mysql_connect ("localhost", "User", "");?>
--End Connect.inc--
--Begin Error.inc--
This file holds error messages and returns!
--End Error.inc--
--User library structure (self-tuning)
CREATE TABLE Auth (
ID smallint (6) DEFAULT ' 0 ' not NULL auto_increment,
Username varchar DEFAULT ' not NULL,
LastName Tinyblob,
FirstName Tinyblob,
Password varchar (16),
Realm varchar (16),
Active char (3),
PRIMARY KEY (ID),
UNIQUE ID (ID),
UNIQUE username (username)
);
--end of user library structure--
--Adding user examples--
Insert into auth (username, lastname, firstname, password, realm, active) values (' admin ', ' I ', ' love ', ' Password ', ' Xxxcom ', ' yes ');
--End--
--User Management program starts usermanage.php--
<?php include ("Auth.inc");?>
<?php
if ($PHP _auth_user!= "admin") {
Header ("Www-authenticate:basic realm=" xxxcom customer Authentication ");
Header ("http/1.0 401 Unauthorized");
echo "Access denied!n";
Exit
};
if ($PHP _auth_pw!= "MyPassword") {
Header ("Www-authenticate:basic realm=" xxxcom customer Authentication ");
Header ("http/1.0 401 Unauthorized");
echo "Access denied!n";
Exit
};
if ($activate) {
Include ("Connect.inc");
$query 1 = "UPDATE auth SET active= ' yes ' where id= ' $id '";
$result 1 = mysql_db_query ("admin", $query 1);
if ($result 1) {
echo "<font size=" +1 ">n";
echo "$user Activatedn";
echo "<br>n<a href=" $PHP _self "> Return </a>n";
echo "</font>n";
Exit
} else {
echo "<font size=" +1 ">n";
echo "Error:unknown Errorn";
echo "<br>n<a href=" $PHP _self "> Return </a>n";
echo "</font>n";
Exit
}
}
if ($deactivate) {
Include ("Connect.inc");
$query 2 = "UPDATE auth SET active= ' no ' where id= ' $id '";
$result 2 = mysql_db_query ("admin", $query 2);
if ($result 2) {
echo "<font size=" +1 ">n";
echo "$user Deactivatedn";
echo "<br>n<a href=" $PHP _self "> Return </a>n";
echo "</font>n";
Exit
} else {
echo "<font size=" +1 ">n";
echo "Error:unknown Errorn";
echo "<br>n<a href=" $PHP _self "> Return </a>n";
echo "</font>n";
Exit
}
}
if ($delete) {
Include ("Connect.inc");
$query 3 = "Delete from auth where id= ' $id '";
$result 3 = mysql_db_query ("admin", $query 3);
if ($result 3) {
echo "<font size=" +1 ">n";
echo "$user deleted!n";
echo "<br>n<a href=" $PHP _self "> Return </a>n";
echo "</font>n";
Exit
} else {
echo "<font size=" +1 ">n";
echo "Error:unknown Errorn";
echo "<br>n<a href=" $PHP _self "> Return </a>n";
echo "</font>n";
Exit
}
}
echo "
echo "
echo "<title> user Management </title>n";
echo "
echo "<body>n";
echo "<form method=" post "action=" $PHP _self ">n";
echo "<table border=" 1 ">n";
echo "<tr><th><font size=" +1 ">username</font></th><th><font size=" +1 "> Real Name</font></th><th><font size= "+1" >activated</font></th></tr>n ";
Include ("Connect.inc");
$query = "SELECT * from Auth";
$result = Mysql_db_query ("admin", $query);
if ($result) {
while ($r = Mysql_fetch_array ($result)) {
$id = $r ["id"];
$username = $r ["username"];
$lastname = $r ["LastName"];
$firstname = $r ["FirstName"];
$activated = $r ["Active"];
if ($activated = = "Yes") {
echo "<tr><td><font size=" +1 "> $username </font></td><td><font size=" +1 "> $lastname, $firstname </font></td><td><font size= "+1" > $activated </font></td> <td><a href= "$PHP _self?deactivate=yes&id= $id &user= $username" >deactivate</a></td ><td><a href= "$PHP _self?delete=yes&id= $id" >delete</a></td></tr>n ";
} elseif ($activated = = "No") {
echo "<tr><td><font size=" +1 "> $username </font></td><td><font size=" +1 "> $lastname, $firstname </font></td><td><font size= "+1" > $activated </font></td> <td><a href= "$PHP _self?activate=yes&id= $id" >activate</a></td><td><a href= "$ php_self?delete=yes&id= $id ">delete</a></td></tr>n";
}
}
}
Mysql_free_result ($result);
echo "</table>n";
echo "</body>n";
echo "
?>
--usermanage.php End--