本文给大家讲解一些最简单的验证知识。大家可以先看下效果图,如果大家感觉还不错,请参考实现代码。
效果图
bookmark_fns.PHP
data_valid_fns.PHP
db_fns.PHP
user_auth_fns.PHP
"; }else { do_html_header('Problem:'); echo "You are not logged in.
"; do_html_url('login.PHP','Login'); do_html_foot(); exit; } } // change password function change_password($username,$old_password,$new_password) { login($username,$old_password); $conn = db_connect();
$result = $conn -> query("update user set passwd = sha1('".$new_password."') where username = '".$username."'");
if (!$result) {
throw new Exception('Password could not be changed.');
} else {
return true; // changed successfully
}
}
function get_random_word($min_length,$max_length) {
// grab a random word from dictionary between the two lengths
// and return it
// generate a random word
$word = '';
// remember to change this path to suit your system
$dictionary = '/usr/dict/words'; // the ispell dictionary
$fp = @fopen($dictionary,'r');
if(!$fp) {
return false;
}
$size = filesize($dictionary);
// go to a random location in dictionary
$rand_location = rand(0,$size);
fseek($fp,$rand_location);
// get the next whole word of the right length in the file
while ((strlen($word) < $min_length) || (strlen($word)>$max_length) || (strstr($word,"'"))) {
if (feof($fp)) {
fseek($fp,0); // if at end,go to start
}
$word = fgets($fp,80); // skip first word as it could be partial
$word = fgets($fp,80); // the potential password
}
$word = trim($word); // trim the trailing \n from fgets
return $word;
}
function reset_password($username) {
// set password for username to a random value
// return the new password or false on failure
// get a random dictionary word b/w 6 and 13 chars in length
$new_password = get_random_word(6,13);
if($new_password == false) {
throw new Exception('Could not generate new password.');
}
// add a number between 0 and 999 to it
// to make it a slightly better password
$rand_number = rand(0,999);
$new_password .= $rand_number;
// set user's password to this in database or return false
$conn = db_connect();
$result = $conn->query("update user
set passwd = sha1('".$new_password."')
where username = '".$username."'");
if (!$result) {
throw new Exception('Could not change password.'); // not changed
} else {
return $new_password; // changed successfully
}
}
function notify_password($username,$password) {
// notify the user that their password has been changed
$conn = db_connect();
$result = $conn->query("select email from user
where username='".$username."'");
if (!$result) {
throw new Exception('Could not find email address.');
} else if ($result->num_rows == 0) {
throw new Exception('Could not find email address.');
// username not in db
} else {
$row = $result->fetch_object();
$email = $row->email;
$from = "From: support@PHPbookmark \r\n";
$mesg = "Your PHPBookmark password has been changed to ".$password."\r\n"
."Please change it next time you log in.\r\n";
if (mail($email,'PHPBookmark login information',$mesg,$from)) {
return true;
} else {
throw new Exception('Could not send email.');
}
}
}
?>
url_fns.PHP
"; $valid_user = $_SESSION['valid_user']; $conn = db_connect(); $results = $conn -> query(" select * from bookmark where username = '".$valid_user."' and bm_URL = '".$new_url."'"); if ($results && ($results -> num_rows > 0)) { throw new Exception("Bookmark already exists.",1); } $insert_result = $conn -> query("insert into bookmark values ('".$valid_user."','".addslashes($new_url)."')"); if (!$insert_result) { throw new Exception("Bookmark could not be inserted.",1); } return true; } // Delete url function delete_bm($user,$url) { $conn = db_connect(); $results = $conn -> query(" delete from bookmark where username = '".$user."' and bm_URL = '".$url."'"); if (!$results) { throw new Exception("Bookmark could not be deleted.",1); } return true; } function recommend_urls($valid_user,$popularity = 1) { $conn = db_connect(); // $query = "select bm_URL // from bookmark // where username in // (select distinct(b2.username) // from bookmark b1,bookmark b2 // where b1.username='".$valid_user."' // and b1.username != b2.username // and b1.bm_URL = b2.bm_URL) // and bm_URL not in // (select bm_URL // from bookmark // where username='".$valid_user."') // group by bm_url // having count(bm_url)>".$popularity; $query = "select bm_URL from bookmark where username in (select distinct(b2.username) from bookmark b1,bookmark b2 where b1.username='".$valid_user."' and b1.username != b2.username and b1.bm_URL = b2.bm_URL) and bm_URL not in (select bm_URL from bookmark where username='".$valid_user."') group by bm_url having count(bm_url)>".$popularity; if (!($result = $conn->query($query))) { throw new Exception('Could not find any bookmarks to recommend.'); } if ($result->num_rows==0) { throw new Exception('Could not find any bookmarks to recommend.'); } $urls = array(); // build an array of the relevant urls for ($count=0; $row = $result->fetch_object(); $count++) { $urls[$count] = $row->bm_URL; } return $urls; } ?>
output_fns.PHP
PHPbookmark
- Store your bookmarks online with us!
- See what other users use!
- Share your favorite links with others!
require_once('bookmark_fns.PHP');
'; do_html_url('login.PHP','Login'); }else { echo 'Could not log you out.
'; } }else { echo 'You are not logged in,so have not been logged out.
'; do_html_url('login.PHP','Login'); } do_html_footer(); ?>
register_form.PHP
forgot_form.PHP
"; }catch(Exception $e){ echo "Your password could not be reset - please try again later."; } do_html_url('login.PHP','Login'); do_html_footer(); ?> change_passwd_form.PHP change_passed.PHP PHP require_once('bookmark_fns.PHP'); session_start(); do_html_header('Changing password'); $old_passwd = $_POST['old_passwd']; $new_passwd = $_POST['new_passwd']; $new_passwd2 = $_POST['new_passwd2']; try { check_valid_user(); if (!filled_out($_POST)) { throw new Exception("You have not filled the form out correctly - please go back and try again.",1); } if ($new_passwd != $new_passwd2) { throw new Exception("The passwords you entered do not match - please go back and try again.",1); } if ((strlen($new_passwd) < 6) || (strlen($new_passwd) > 16)) { throw new Exception("Your password must be between 6 and 16 characters - please go back and try again.",1); } change_password($_SESSION['valid_user'],$old_passwd,$new_passwd2); echo 'Password changed.'; }catch(Exception $e) { echo $e -> getMessage(); } display_user_menu(); do_html_footer(); ?> add_bm_form.PHP
add_bms.PHP
delete_bms.PHP
You have not chosen any bookmarks to delete.
Please try again.
"; }else { echo "Could not deleted ".htmlspecialchars($url)."
"; } } }else { echo "No bookmarks selected for deletion."; } } if ($mks = get_user_urls($_SESSION['valid_user'])) { display_user_urls($mks); } display_user_menu(); do_html_footer(); ?>
recommend.PHP
do_html_header('Recommending URLS');
try {
check_valid_user();
$urls = recommend_urls($_SESSION['valid_user'],1);
display_recommended_urls($urls);
}catch(Exception $e) {
echo $e -> getMessage();
}
display_user_menu();
do_html_footer();
?>
member.PHP
以上所述是小编给大家介绍的PHP用户验证和标签推荐的简单使用。编程之家 jb51.cc 收集整理的教程希望能对你有所帮助,如果觉得编程之家不错,可分享给好友!感谢支持。
原文链接:https://www.f2er.com/php/18652.html