 | |
03-15-2006, 01:17 PM
|
· #1 | | Senior Member Name: Dennis Location: Joliet, Illinois Join Date: Apr 2005
Posts: 1,124
NP$: 2254.10 ( Donate)
| Using PHP for Password Identification! 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 Code: <?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 by RageD : 03-15-2006 at 01:23 PM.
|
| |
03-17-2006, 03:27 AM
|
· #2 | | Senior Member Name: Bharat Balegere Location: Bangalore Join Date: Jul 2005
Posts: 1,146
NP$: 15.60 ( Donate)
| Simple but effective. |
| |
03-17-2006, 03:38 AM
|
· #3 | | NamePros Regular Join Date: Dec 2005
Posts: 206
NP$: 270.00 ( Donate)
| 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']; |
| |
03-17-2006, 12:26 PM
|
· #4 | | Senior Member Name: TheMoose Location: on a oil rig just off Ireland Join Date: Nov 2005
Posts: 1,402
NP$: 434.65 ( Donate)
| 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 Code: <?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!');
}
}
?>
__________________ You design in photoshop, I code into valid XHTML/CSS. Professional PSD, PNG or HTML to tableless XHTML/CSS designs. For more info, send me a PM. |
| |
03-17-2006, 12:48 PM
|
· #5 | | NamePros Regular Name: Jim, of course Location: Philadelphia Join Date: Aug 2005
Posts: 558
NP$: 14.30 ( Donate)
| Or, you could use a session cookie... PHP Code: <?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>";
}
?>
__________________
woop woop
|
| |
03-17-2006, 02:55 PM
|
· #6 | | A Wealth of Knowledge Join Date: Aug 2004
Posts: 3,784
NP$: 14020.20 ( Donate)
| 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 |
| |
03-22-2006, 10:15 AM
|
· #7 | | NamePros Regular Join Date: Jul 2005
Posts: 221
NP$: 328.50 ( Donate)
| thanks a lot..  |
| |
03-22-2006, 11:16 PM
|
· #8 | | NamePros Regular Join Date: Mar 2006
Posts: 394
NP$: 286.68 ( Donate)
| 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 Code: <?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 by sacx13 : 03-22-2006 at 11:17 PM.
Reason: Errata :)
|
| |
03-24-2006, 02:59 PM
|
· #9 | | Senior Member Name: Dennis Location: Joliet, Illinois Join Date: Apr 2005
Posts: 1,124
NP$: 2254.10 ( Donate)
| 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 |
| |
03-31-2006, 03:27 AM
|
· #10 | | NamePros Regular Join Date: Mar 2006
Posts: 394
NP$: 286.68 ( Donate)
| For a simple page you dont need a mysql database for authentication ...
Regards |
| |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | |