本文给大家讲解一些最简单的验证知识。大家可以先看下效果图,如果大家感觉还不错,请参考实现代码。

效果图

PHP用户验证和标签推荐的简单使用

bookmark_fns.php

<"htmlcode">
<"htmlcode">
<"Could not connect to database server", 1);
}else {
return $db;
}
}
"htmlcode">
<"select * from user where username = '".$username."'");
if (!$results) {
throw new Exception("Could not execute query", 1);
}
if ($results -> num_rows > 0) {
throw new Exception("That username is taken - go back and choose another one.", 1);
} 
$results = $conn -> query("insert into user values ('".$username."', sha1('".$email."'), '".$password."')");
if (!$results) {
throw new Exception('Could not register you in database - please try again later.');
}
return true;
}
// Log in 
function login($username, $password) {
$conn = db_connect();
$results = $conn -> query("select * from user where username = '".$username."' and passwd = sha1('".$password."')");
if (!$results) {
throw new Exception('Could not log you in.');
}
if ($results -> num_rows > 0) {
return true;
}else {
throw new Exception('Could not log you in.');
}
}
// Check valid user 
function check_valid_user() {
if (isset($_SESSION['valid_user'])) {
echo "Logged in as ".$_SESSION['valid_user'].".<br />";
}else {
do_html_header('Problem:');
echo "You are not logged in.<br />";
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.');
}
}
}
"htmlcode">
<"select bm_URL 
from bookmark 
where username = '" . $username . "'");
if (!$results) {
return false;
}
$url_array = array();
for ($i = 1;$row = $results -> fetch_row();++$i) {
$url_array[$i] = $row[0];
}
return $url_array;
}
// Add url to db
function add_bm($new_url) {
echo "Attempting to add ".htmlspecialchars($new_url)."<br />";
$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;
}
"htmlcode">
<"005.png" alt="PHPbookmark logo" border="0"
align="left" valign="bottom" height="55" width="57" />
<h1>PHPbookmark</h1>
<hr />
<"<"><"register_form.php">Not a member"post" action="member.php">
<table bgcolor="#cccccc">
<tr>
<td colspan="2">Members log in here:</td>
<tr>
<td>Username:</td>
<td><input type="text" name="username"/></td></tr>
<tr>
<td>Password:</td>
<td><input type="password" name="passwd"/></td></tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Log in"/></td></tr>
<tr>
<td colspan="2"><a href="forgot_form.php">Forgot your password"post" action="register_new.php">
<table bgcolor="#cccccc">
<tr>
<td>Email address:</td>
<td><input type="text" name="email" size="30" maxlength="100"/></td></tr>
<tr>
<td>Preferred username <br />(max 16 chars):</td>
<td valign="top"><input type="text" name="username"
size="16" maxlength="16"/></td></tr>
<tr>
<td>Password <br />(between 6 and 16 chars):</td>
<td valign="top"><input type="password" name="passwd"
size="16" maxlength="16"/></td></tr>
<tr>
<td>Confirm password:</td>
<td><input type="password" name="passwd2" size="16" maxlength="16"/></td></tr>
<tr>
<td colspan=2 align="center">
<input type="submit" value="Register"></td></tr>
</table></form>
<"bm_table" action="delete_bms.php" method="post">
<table width="300" cellpadding="2" cellspacing="0">
<"#cccccc";
echo "<tr bgcolor=\"".$color."\"><td><strong>Bookmark</strong></td>";
echo "<td><strong>Delete";
if ((is_array($url_array)) && (count($url_array) > 0)) {
foreach ($url_array as $url) {
if ($color == "#cccccc") {
$color = "#ffffff";
} else {
$color = "#cccccc";
}
//remember to call htmlspecialchars() when we are displaying user data
echo "<tr bgcolor=\"".$color."\"><td><a href=\"".$url."\">".htmlspecialchars($url)."</a></td>
<td><input type=\"checkbox\" name=\"del_me[]\"
value=\"".$url."\"/></td>
</tr>";
}
} else {
echo "<tr><td>No bookmarks on record</td></tr>";
}
"member.php">Home</a> &nbsp;|&nbsp;
<a href="add_bm_form.php">Add BM</a> &nbsp;|&nbsp;
<"<a href=\"#\" onClick=\"bm_table.submit();\">Delete BM</a> &nbsp;|&nbsp;";
} else {
echo "<span style=\"color: #cccccc\">Delete BM</span> &nbsp;|&nbsp;";
}
"change_passwd_form.php">Change password</a>
<br />
<a href="recommend.php">Recommend URLs to me</a> &nbsp;|&nbsp;
<a href="logout.php">Logout</a>
<hr />
<"bm_table" action="add_bms.php" method="post">
<table width="250" cellpadding="2" cellspacing="0" bgcolor="#cccccc">
<tr><td>New BM:</td>
<td><input type="text" name="new_url" value="http://"
size="30" maxlength="255"/></td></tr>
<tr><td colspan="2" align="center">
<input type="submit" value="Add bookmark"/></td></tr>
</table>
</form>
<"change_passwd.php" method="post">
<table width="250" cellpadding="2" cellspacing="0" bgcolor="#cccccc">
<tr><td>Old password:</td>
<td><input type="password" name="old_passwd"
size="16" maxlength="16"/></td>
</tr>
<tr><td>New password:</td>
<td><input type="password" name="new_passwd"
size="16" maxlength="16"/></td>
</tr>
<tr><td>Repeat new password:</td>
<td><input type="password" name="new_passwd2"
size="16" maxlength="16"/></td>
</tr>
<tr><td colspan="2" align="center">
<input type="submit" value="Change password"/>
</td></tr>
</table>
<br />
<"forgot_passwd.php" method="post">
<table width="250" cellpadding="2" cellspacing="0" bgcolor="#cccccc">
<tr><td>Enter your username</td>
<td><input type="text" name="username" size="16" maxlength="16"/></td>
</tr>
<tr><td colspan=2 align="center">
<input type="submit" value="Change password"/>
</td></tr>
</table>
<br />
<"300" cellpadding="2" cellspacing="0">
<"#cccccc";
echo "<tr bgcolor=\"".$color."\">
<td><strong>Recommendations</strong></td></tr>";
if ((is_array($url_array)) && (count($url_array)>0)) {
foreach ($url_array as $url) {
if ($color == "#cccccc") {
$color = "#ffffff";
} else {
$color = "#cccccc";
}
echo "<tr bgcolor=\"".$color."\">
<td><a href=\"".$url."\">".htmlspecialchars($url)."</a></td></tr>";
}
} else {
echo "<tr><td>No recommendations for you today.</td></tr>";
}
"htmlcode">
// start session
session_start();
$old_user = $_SESSION['valid_user'];
unset($_SESSION['valid_user']);
$result_dest = session_destroy();
do_html_header('Logging out');
if (!empty($old_user)) {
if ($result_dest) {
echo 'Logged out.<br />';
do_html_url('login.php', 'Login');
}else {
echo 'Could not log you out.<br />';
}
}else {
echo 'You are not logged in ,so have not been logged out.<br />';
do_html_url('login.php', 'Login');
}
do_html_footer();
"htmlcode">
<"You have not filled the form out correctly - please go back and try again.", 1);
}
if (!valid_email($email)) { 
throw new Exception("That is not a valid email address - please go back and try again.", 1);
}
if ($passwd != $passwd2) { 
throw new Exception("The passwords you entered do not match - please go back and try again.", 1);
}
if ((strlen($passwd) < 6) || (strlen($passwd) > 16)) { 
throw new Exception("Your password must be between 6 and 16 characters - please go back and try again.", 1);
}
register($username, $passwd, $email);
$_SESSION['valid_user'] = $username;
do_html_header('Rigistration successful');
do_html_url('member.php', 'Go to members page');
do_html_footer();
} catch (Exception $e) {
do_html_header('Problem: ');
echo $e -> getMessage();
do_html_footer();
exit();
}
"htmlcode">
<"Your new password has been emailed to you.<br />";
}catch(Exception $e){
echo "Your password could not be reset - please try again later.";
}
do_html_url('login.php', 'Login');
do_html_footer();
"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();
"htmlcode">
<"Bookmark added";
if ($mks = get_user_urls($_SESSION['valid_user'])) {
display_user_urls($mks);
}
}catch(Exception $e) {
echo $e -> getMessage();
}
display_user_menu();
do_html_footer();
"htmlcode">
<"<p>You have not chosen any bookmarks to delete.<br />
Please try again.</p>";
display_user_menu();
do_html_footer();
exit;
}else {
if (count($del_me) > 0) {
foreach ($del_me as $url) {
if (delete_bm($valid_user, $url)) {
echo "Deleted ".htmlspecialchars($url)."<br />";
}else {
echo "Could not deleted ".htmlspecialchars($url)."<br />";
}
}
}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();
"htmlcode">
<"htmlcode">
<"You could not be logged in. You must be logged in to view this page.";
do_html_url('login.php', 'Login');
do_html_footer();
exit;
}
}
do_html_header('Home');
check_valid_user();
if ($url_array = get_user_urls($_SESSION['valid_user'])) {
display_user_urls($url_array);
}
display_user_menu();
do_html_footer();
?>

以上所述是小编给大家介绍的PHP用户验证和标签推荐的简单使用,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

标签:
php,验证和标签推荐

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com

评论“PHP用户验证和标签推荐的简单使用”

暂无“PHP用户验证和标签推荐的简单使用”评论...

P70系列延期,华为新旗舰将在下月发布

3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。

而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?

根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。