时时商务社区

标题: PHP用户验证和标签推荐的简单使用 [打印本页]

作者: 阿情    时间: 2018-2-14 05:31

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

bookmark_fns.php
data_valid_fns.php
$value) {
if ((!isset($key)) || ($value == '')) {
return false;
}
}
return true;
}
// Valid email
function valid_email($address) {
if (ereg('^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$', $address)) {
return true;
}else {
return false;
}
}
?>
db_fns.php
user_auth_fns.php

query("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'].".
";
}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) $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"
."lease 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

query("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)."
";
$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
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 }


PHPbookmark

[url=][/url]
  • Store your bookmarks online with us!
  • See what other users use!
  • Share your favorite links with others!

    Not a member?
    Members log in here:[/td]
    Username:
    Password:

    Forgot your password?

    Email address:
    Preferred username
    (max 16 chars):
    Password
    (between 6 and 16 chars):
    Confirm password:


    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 "[url=]".htmlspecialchars($url)."[/url]

    ";
    }
    } else {
    echo "No bookmarks on record";
    }
    ?>
    Home  |
    Add BM  |
    Delete BM  | ";
    }
    ?>
    Change password
    Recommend URLs to me  |
    Logout
    New BM:


    Old password:
    New password:
    Repeat new password:



    Enter your username



    0)) {
    foreach ($url_array as $url) {
    if ($color == "#cccccc") {
    $color = "#ffffff";
    } else {
    $color = "#cccccc";
    }
    echo "
    [url=]".htmlspecialchars($url)."[/url]";
    }
    } else {
    echo "No recommendations for you today.";
    }
    ?>
    login.php
    logout.php
    require_once('bookmark_fns.php');
    // 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.
    ';
    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
    register_new.php
    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();
    }
    ?>
    forgot_form.php
    forgot_passwd.php
    change_passwd_form.php
    change_passed.php
    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

    getMessage();
    }
    display_user_menu();
    do_html_footer();
    ?>
    delete_bms.php
    You have not chosen any bookmarks to delete.
    Please try again.
    ";
    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)."
    ";
    }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
    getMessage();
    }
    display_user_menu();
    do_html_footer();
    ?>
    member.php
    以上所述是小编给大家介绍的PHP用户验证和标签推荐的简单使用,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
                
                
    您可能感兴趣的文章:
  • php array_pop()数组函数将数组最后一个单元弹出(出栈)
  • php数组函数序列之array_pop() - 删除数组中的最后一个元素
  • PHPCMS忘记后台密码的解决办法
  • PHP仿微信发红包领红包效果
  • php将服务端的文件读出来显示在web页面实例
  • 利用php做服务器和web前端的界面进行交互
  • PHP实现小偷程序实例
  • php 实现一个字符串加密解密的函数实例代码
  • PHP之十六个魔术方法详细介绍
  • PHP递归获取目录内所有文件的实现方法
  • php array_pop 删除数组最后一个元素实例
            




    欢迎光临 时时商务社区 (http://bbs.4435.cn/) Powered by Discuz! X3.2