okay, thanks.
So, I managed to make a better and and more slim verify.php
Now, I do not think I can simplify and improve this any more 😃
Here is the new version.
For more comments, see the old version, below.
Changes:
- stripos() instead of eregi()
- require() instead of require_once() ... thanks to: Rasmus Lerdorf blog
- if($res = $tb->one){ replaces
$result = $tb->result_one;
if(!empty($result)){
- using else instead of extra variable '$found'
I must say I am surprised so many improvments can be made
in such a small piece of PHP code.
Regars 🙂 halojoy
New version 3a:
<?php
// verify.php
// This page is called from Login: require('verify.php');
if(false!==stripos($_SERVER['PHP_SELF'],'verify.php')||!defined('SECURE')){
exit('err:v');
}
require($dbdir.'class.dbtable.php');
// INDATA: $m=''; error message. $u,$p; entered_user, entered_pass
$tb = new dbtable('mydb','users');
$tb->load();
$tb->find_one('username', $u);
if($res = $tb->one){
if(sha1($p) !== $res['password']){
$m = 'bad pass';
}
}else{
$m = 'no user';
}
// OUTDATA: $m='' or $m=error-msg
?>
Old version 1:
<?php
// Protects this page from direct access
// Using 2 controls
if(eregi('security/verify.php', $_SERVER['PHP_SELF']) || !defined('SECURE')){
exit('err:v');
}
require_once($database_dir.'db_table.class.php');
// New db_table object, with arg: database-name, table-name
$tb = new db_table('my_db', 'users');
// Load the complete table specified above. Into array.
$tb->tb_load();
// INDATA: $entered_username + $entered_password
// Find first occurance of entered username in Column 'username'
$tb->tb_find_one('username', $entered_username);
// Method 'tb_find_one()' returns to its own variable: '->result_one'
// If Found store: The full row as an array,
// If Not found : array() .. an empty array
$result = $tb->result_one;
$found = false;
$message = '';
// If username match, we test for sha1(password)
if(!empty($result)){
$found = true;
if($result['password'] !== sha1($entered_password))
$message = 'bad pass';
}
if(!$found)
$message = 'no user';
// OUTDATA: $message = '' / 'no user' / 'bad pass'
// '' = verify was okay, no error message
// 'no user' = exists no such user
// 'bad pass' = password does not match user
?>