Dynadot โ€” .com Transfer

Using PHP for Password Identification!

Namecheap AuctionsNamecheap Auctions
Namecheap AuctionsNamecheap Auctions
Spacemail by SpaceshipSpacemail by Spaceship
Watch
This is an alternative to Javascript and all other ways of password identification for a page view! You can also use MySQL for multiple kinds of passwords, but if you only want a couple, this is the way to go: (This requires NO MySQL)

PHP:
<?php
// Let's define the page that we will see the login form & page title
$pagetitle = "Page Title Here";
if($page == ""){ ?>
<HTML>
<TITLE><?php echo($pagetitle); ?></TITLE>
<BODY>
<form name="login" method="post" action="?page=login">
Password: <input type=password name="password"><br>
<input type=submit value="Login"><input type=reset>
</form>
</BODY>
</HTML>
<?php
// Let's end that section with the following bracket
}
// Start the login page
if($page == "login"){
// The value '$password' comes from name="password" in the password field
    if($password !== "PasswordHere"){
         echo('Incorrect Password');
         exit;
     }
     else{
          echo('You are now logged in!!  Page content goes here!');
     }
}
?>

-Thanks

-RageD
 
Last edited:
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
Simple but effective.
 
0
•••
thats preety unsecure.

but for a small site with not much traffic its good...

btw if Globals are turned off...HIGHELY RECCOMEMDED

$page = $_GET['page'];
 
0
•••
Hey,
The script would be good for an admin panel or something similar.
I might like to make a few additions to your script :) - add cookies so you won't have to login every time. the 3600 is in seconds - that's an hour. Change if you wish. (86400 is a day).

PHP:
<?php
// Let's define the page that we will see the login form & page title
$pagetitle = "Page Title Here";

setcookie("TestCookie", "hello");
if(empty($_COOKIE['TestCookie'])) { die("Cookies disabled!"); } else { setcookie("TestCookie", "hello", time()-3600); }

if(empty($_COOKIE['adminformpassword'])){ ?>
<HTML>
<TITLE><?php echo($pagetitle); ?></TITLE>
<BODY>
<form name="login" method="post" action="?page=login">
Password: <input type=password name="password"><br>
<input type=submit value="Login"><input type=reset>
</form>
</BODY>
</HTML>
<?php
// Let's end that section with the following bracket
}
// Start the login page
if($page == "login"){
// The value '$password' comes from name="password" in the password field
    $password = $_POST['password'];
    if($password == "YourPasswordHere") {
    setcookie("adminformpassword", md5($password), time()+3600);
    }
    if(md5($password) != $_COOKIE['adminformpassword']){
         echo('Incorrect Password');
         exit;
    }
     else
    {
          echo('You are now logged in!!  Page content goes here!');
     }
}
?>
 
0
•••
Or, you could use a session cookie...
PHP:
<?php
$adminpass = "testpassword";
session_start();
if($_GET['act']=="login"){
  if($_POST['passwordx']==$adminpass){
    $_SESSION['logged_in'] = md5($adminpass);
    echo "Logged in.";
  }
}
if($_GET['act']=="logout"){
  session_unset();
  echo "Logged out.";
}
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in']==md5($adminpass)) {
//admin is logged in
//display page here
} else {
//admin is not logged in, so show login form
echo "<form action=\"admin.php?act=login\" method=\"post\"><input type=\"password\" name=\"passwordx\"><input type=\"submit\" value=\"Login\"></form>";
}
?>
 
0
•••
This sort of thing would qualify to be in the CODE section of the Programming section, but I also think it is fitting to put in the Webmaster Tutorial section.

:)

-Steve
 
0
•••
thanks a lot.. :)
 
0
•••
Another simple code

My code is reading the passwords from a simple file. The user and passwords are delimited by comas. Ex
user1,pass1
user2,pass2

PHP:
<?php
 $username=$_POST['username'];
 $password=$_POST['password'];
 $handle = fopen('/etc/php.pas',"r");

while(!feof($handle))
{
$line = fgets($handle,1024);
list($u,$p) = explode(',',$line);

$u = trim($u);
$p = trim($p);
    if ($username!="")
    if ($username==$u && $password==$p)
    {
        // just add your session cookies here and etc.
        header("Location: test.html");
        exit();
    }
}

fclose($handle);
?>
 
Last edited:
0
•••
Yep :) If you wanted to be very technical and secure you could just create a quick MySQL connect script and encrypt everything to a DB... You could use md5 encryption, or whatever you wanted..

-RageD
 
0
•••
For a simple page you dont need a mysql database for authentication ...


Regards
 
0
•••
Dynadot โ€” .com TransferDynadot โ€” .com Transfer
Truehost โ€” .com domains from $4.99, hosting includedTruehost โ€” .com domains from $4.99, hosting included
Escrow.com
Spaceship
Rexus Domain
CryptoExchange.com
Domain Recover
CatchDoms
DomDB
NameFit
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back