加入收藏 | 设为首页 | 会员中心 | 我要投稿 宁德站长网 (https://www.0593zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2016-11-27 09:24:05 所属栏目:大数据 来源:站长网
导读:本文给大家讲解一些最简单的验证知识。大家可以先看下效果图,如果大家感觉还不错,请参考实现代码。 效果图 bookmark_fns.php lt;#63;phprequire_once('output_fns.php');require_once('db_fns.php');require_once('data_valid_fns.php');require_once('u

url_fns.php

lt;#63;php
require_once('db_fns.php');
// Get user urls
function get_user_urls($username) {
$conn = db_connect();
$results = $conn -gt; query("select bm_URL 
from bookmark 
where username = '" . $username . "'");
if (!$results) {
return false;
}
$url_array = array();
for ($i = 1;$row = $results -gt; 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)."lt;br /gt;";
$valid_user = $_SESSION['valid_user'];
$conn = db_connect();
$results = $conn -gt; query(" select * from bookmark 
where username = '".$valid_user."' 
and bm_URL = '".$new_url."'");
if ($results  ($results -gt; num_rows gt; 0)) {
throw new Exception("Bookmark already exists.", 1); 
}
$insert_result = $conn -gt; 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 -gt; 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)gt;".$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)gt;".$popularity;
if (!($result = $conn-gt;query($query))) {
throw new Exception('Could not find any bookmarks to recommend.');
}
if ($result-gt;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-gt;fetch_object(); $count++) {
$urls[$count] = $row-gt;bm_URL;
}
return $urls;
}
#63;gt;

output_fns.php

lt;#63;php
function do_html_header($title) {
// print an HTML header
#63;gt;
lt;htmlgt;
lt;headgt;
lt;titlegt;lt;#63;php echo $title;#63;gt;lt;/titlegt;
lt;stylegt;
body { font-family: Arial, Helvetica, sans-serif; font-size: 13px }
li, td { font-family: Arial, Helvetica, sans-serif; font-size: 13px }
hr { color: #3333cc; width=300; text-align=left}
a { color: #000000 }
lt;/stylegt;
lt;/headgt;
lt;bodygt;
lt;img src="005.png" alt="PHPbookmark logo" border="0"
align="left" valign="bottom" height="55" width="57" /gt;
lt;h1gt;PHPbookmarklt;/h1gt;
lt;hr /gt;
lt;#63;php
if($title) {
do_html_heading($title);
}
}
function do_html_footer() {
// print an HTML footer
#63;gt;
lt;/bodygt;
lt;/htmlgt;
lt;#63;php
}
function do_html_heading($heading) {
// print heading
#63;gt;
lt;h2gt;lt;#63;php echo $heading;#63;gt;lt;/h2gt;
lt;#63;php
}
function do_html_URL($url, $name) {
// output URL as link and br
#63;gt;
lt;br /gt;lt;a href="lt;#63;php echo $url;#63;gt;"gt;lt;#63;php echo $name;#63;gt;lt;/agt;lt;br /gt;
lt;#63;php
}
function display_site_info() {
// display some marketing info
#63;gt;
lt;ulgt;
lt;ligt;Store your bookmarks online with us!lt;/ligt;
lt;ligt;See what other users use!lt;/ligt;
lt;ligt;Share your favorite links with others!lt;/ligt;
lt;/ulgt;
lt;#63;php
}
function display_login_form() {
#63;gt;
lt;pgt;lt;a href="register_form.php"gt;Not a member#63;lt;/agt;lt;/pgt;
lt;form method="post" action="member.php"gt;
lt;table bgcolor="#cccccc"gt;
lt;trgt;
lt;td colspan="2"gt;Members log in here:lt;/tdgt;
lt;trgt;
lt;tdgt;Username:lt;/tdgt;
lt;tdgt;lt;input type="text" name="username"/gt;lt;/tdgt;lt;/trgt;
lt;trgt;
lt;tdgt;Password:lt;/tdgt;
lt;tdgt;lt;input type="password" name="passwd"/gt;lt;/tdgt;lt;/trgt;
lt;trgt;
lt;td colspan="2" align="center"gt;
lt;input type="submit" value="Log in"/gt;lt;/tdgt;lt;/trgt;
lt;trgt;
lt;td colspan="2"gt;lt;a href="forgot_form.php"gt;Forgot your password#63;lt;/agt;lt;/tdgt;
lt;/trgt;
lt;/tablegt;lt;/formgt;
lt;#63;php
}
function display_registration_form() {
#63;gt;
lt;form method="post" action="register_new.php"gt;
lt;table bgcolor="#cccccc"gt;
lt;trgt;
lt;tdgt;Email address:lt;/tdgt;
lt;tdgt;lt;input type="text" name="email" size="30" maxlength="100"/gt;lt;/tdgt;lt;/trgt;
lt;trgt;
lt;tdgt;Preferred username lt;br /gt;(max 16 chars):lt;/tdgt;
lt;td valign="top"gt;lt;input type="text" name="username"
size="16" maxlength="16"/gt;lt;/tdgt;lt;/trgt;
lt;trgt;
lt;tdgt;Password lt;br /gt;(between 6 and 16 chars):lt;/tdgt;
lt;td valign="top"gt;lt;input type="password" name="passwd"
size="16" maxlength="16"/gt;lt;/tdgt;lt;/trgt;
lt;trgt;
lt;tdgt;Confirm password:lt;/tdgt;
lt;tdgt;lt;input type="password" name="passwd2" size="16" maxlength="16"/gt;lt;/tdgt;lt;/trgt;
lt;trgt;
lt;td colspan=2 align="center"gt;
lt;input type="submit" value="Register"gt;lt;/tdgt;lt;/trgt;
lt;/tablegt;lt;/formgt;
lt;#63;php
}
function display_user_urls($url_array) {
// display the table of URLs
// set global variable, so we can test later if this is on the page
global $bm_table;
$bm_table = true;
#63;gt;
lt;br /gt;
lt;form name="bm_table" action="delete_bms.php" method="post"gt;
lt;table width="300" cellpadding="2" cellspacing="0"gt;
lt;#63;php
$color = "#cccccc";
echo "lt;tr bgcolor="".$color.""gt;lt;tdgt;lt;stronggt;Bookmarklt;/stronggt;lt;/tdgt;";
echo "lt;tdgt;lt;stronggt;Delete#63;lt;/stronggt;lt;/tdgt;lt;/trgt;";
if ((is_array($url_array))  (count($url_array) gt; 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 "lt;tr bgcolor="".$color.""gt;lt;tdgt;lt;a href="".$url.""gt;".htmlspecialchars($url)."lt;/agt;lt;/tdgt;
lt;tdgt;lt;input type="checkbox" name="del_me[]"
value="".$url.""/gt;lt;/tdgt;
lt;/trgt;";
}
} else {
echo "lt;trgt;lt;tdgt;No bookmarks on recordlt;/tdgt;lt;/trgt;";
}
#63;gt;
lt;/tablegt;
lt;/formgt;
lt;#63;php
}
function display_user_menu() {
// display the menu options on this page
#63;gt;
lt;hr /gt;
lt;a href="member.php"gt;Homelt;/agt; nbsp;|nbsp;
lt;a href="add_bm_form.php"gt;Add BMlt;/agt; nbsp;|nbsp;
lt;#63;php
// only offer the delete option if bookmark table is on this page
global $bm_table;
if ($bm_table == true) {
echo "lt;a href="#" onClick="bm_table.submit();"gt;Delete BMlt;/agt; nbsp;|nbsp;";
} else {
echo "lt;span style="color: #cccccc"gt;Delete BMlt;/spangt; nbsp;|nbsp;";
}
#63;gt;
lt;a href="change_passwd_form.php"gt;Change passwordlt;/agt;
lt;br /gt;
lt;a href="recommend.php"gt;Recommend URLs to melt;/agt; nbsp;|nbsp;
lt;a href="logout.php"gt;Logoutlt;/agt;
lt;hr /gt;
lt;#63;php
}
function display_add_bm_form() {
// display the form for people to ener a new bookmark in
#63;gt;
lt;form name="bm_table" action="add_bms.php" method="post"gt;
lt;table width="250" cellpadding="2" cellspacing="0" bgcolor="#cccccc"gt;
lt;trgt;lt;tdgt;New BM:lt;/tdgt;
lt;tdgt;lt;input type="text" name="new_url" value="http://"
size="30" maxlength="255"/gt;lt;/tdgt;lt;/trgt;
lt;trgt;lt;td colspan="2" align="center"gt;
lt;input type="submit" value="Add bookmark"/gt;lt;/tdgt;lt;/trgt;
lt;/tablegt;
lt;/formgt;
lt;#63;php
}
function display_password_form() {
// display html change password form
#63;gt;
lt;br /gt;
lt;form action="change_passwd.php" method="post"gt;
lt;table width="250" cellpadding="2" cellspacing="0" bgcolor="#cccccc"gt;
lt;trgt;lt;tdgt;Old password:lt;/tdgt;
lt;tdgt;lt;input type="password" name="old_passwd"
size="16" maxlength="16"/gt;lt;/tdgt;
lt;/trgt;
lt;trgt;lt;tdgt;New password:lt;/tdgt;
lt;tdgt;lt;input type="password" name="new_passwd"
size="16" maxlength="16"/gt;lt;/tdgt;
lt;/trgt;
lt;trgt;lt;tdgt;Repeat new password:lt;/tdgt;
lt;tdgt;lt;input type="password" name="new_passwd2"
size="16" maxlength="16"/gt;lt;/tdgt;
lt;/trgt;
lt;trgt;lt;td colspan="2" align="center"gt;
lt;input type="submit" value="Change password"/gt;
lt;/tdgt;lt;/trgt;
lt;/tablegt;
lt;br /gt;
lt;#63;php
}
function display_forgot_form() {
// display HTML form to reset and email password
#63;gt;
lt;br /gt;
lt;form action="forgot_passwd.php" method="post"gt;
lt;table width="250" cellpadding="2" cellspacing="0" bgcolor="#cccccc"gt;
lt;trgt;lt;tdgt;Enter your usernamelt;/tdgt;
lt;tdgt;lt;input type="text" name="username" size="16" maxlength="16"/gt;lt;/tdgt;
lt;/trgt;
lt;trgt;lt;td colspan=2 align="center"gt;
lt;input type="submit" value="Change password"/gt;
lt;/tdgt;lt;/trgt;
lt;/tablegt;
lt;br /gt;
lt;#63;php
}
function display_recommended_urls($url_array) {
// similar output to display_user_urls
// instead of displaying the users bookmarks, display recomendation
#63;gt;
lt;br /gt;
lt;table width="300" cellpadding="2" cellspacing="0"gt;
lt;#63;php
$color = "#cccccc";
echo "lt;tr bgcolor="".$color.""gt;
lt;tdgt;lt;stronggt;Recommendationslt;/stronggt;lt;/tdgt;lt;/trgt;";
if ((is_array($url_array))  (count($url_array)gt;0)) {
foreach ($url_array as $url) {
if ($color == "#cccccc") {
$color = "#ffffff";
} else {
$color = "#cccccc";
}
echo "lt;tr bgcolor="".$color.""gt;
lt;tdgt;lt;a href="".$url.""gt;".htmlspecialchars($url)."lt;/agt;lt;/tdgt;lt;/trgt;";
}
} else {
echo "lt;trgt;lt;tdgt;No recommendations for you today.lt;/tdgt;lt;/trgt;";
}
#63;gt;
lt;/tablegt;
lt;#63;php
}
#63;gt;
login.php
lt;#63;php
require_once('bookmark_fns.php');
do_html_header('');
display_site_info();
display_login_form();
do_html_footer();
#63;gt;
logout.php
lt;#63;php

(编辑:宁德站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!