Add authentication to any php page the easy way

Posted by: fay

This is useful for anyone who'd like to add a login form to any php page.

< ? include "access.php"; ?>

(remove the space before the question mark)

The crentials can be edited in the access.php file, by changing the values of $ADMIN_USER and $ADMIN_PASSWORD. This useful script requires no database, and no big tweaking to the code on your pages.


Please save this as access.php

 <?
session_start();
$ADMIN_USER = "user";
$ADMIN_PASSWORD = "password";

if(!$_SESSION['authenticated'])

    if($_POST['loginbutton']) {
        $inputuser = $_POST['input_user'];
        $inputpassword = $_POST['input_password'];

        if(!strcmp($inputuser ,$ADMIN_USER) && !strcmp($inputpassword,$ADMIN_PASSWORD)) {
            $_SESSION['authenticated'] = 1;
            header("Location:".$_SERVER[PHP_SELF]);
        }
        else
            displayform(1);
    }
    else
        displayform(0);


function displayform($error) {
    echo "<html><head><title>Please login</title></head><body><style>body,td,input { font-family: verdana; font-size: 8pt; }</style>";
    if($error) echo "<p><b>Wrong credentials.</b></p>";
    echo "<form action=\"\" method=\"post\"><table width='400' border=0><tr><td width='100'>username:</td>";
    echo "<td><input type='text' name='input_user'></td></tr><tr><td>password:</td><td><input type='password' name='input_password'></td></tr>";
    echo "<tr><td colspan='2'><input type='Submit' value='Login&raquo;' name='loginbutton'></td></tr></table></form></body></html>";
    exit;
}
?>

[ Single viewed: 3490 times (counter reset 21 Oct 2008) ]
« Prev item - Next Item »
---------------------------------------------

Comments

No comments yet. You can be the first!

Leave comment

 




Soalan bonus untuk anda
Bonus Question for yah