English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Simple Usage of PHP User Verification and Tag Recommendation

This article explains some of the simplest verification knowledge. You can first take a look at the effect diagram, and if you feel it's good, please refer to the implementation code.

Effect image

bookmark_fns.php

<?php
require_once('output_fns.php');
require_once('db_fns.php');
require_once('data_valid_fns.php');
require_once('url_fns.php');
require_once('user_auth_fns.php');
?>

data_valid_fns.php

<?php
// Test that each variable has a value
function filled_out($form_vars) {
foreach ($form_vars as $key => $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;
}
return false;
}
}
?>

db_fns.php

<?php
//Conncet to db 
function db_connect() {
$db = new mysqli('127.0.0.1', 'bm_user', 'password', 'bookmarks');
if (!$db) {
throw new Exception("Could not connect to database server", 1);
}
return $db;
}
}
?>

user_auth_fns.php

<?php
require_once('db_fns.php');
// register 
function register($username, $email, $password) {
$conn = db_connect();
$results = $conn -> 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;
}
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 />";
}
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.');
}
}
}
?>

url_fns.php

<?php
require_once('db_fns.php');
// Get user urls
function get_user_urls($username) {
$conn = db_connect();
$results = $conn -> 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)."<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;
}
?>

output_fns.php

<?php
function do_html_header($title) {
// print an HTML header
?>
<html>
<head>
<title><63;php echo $title;63;>
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 }
</style>
</head>

[#0#]"><?php echo $name;?>
  • Store your bookmarks online with us!

    require_once('bookmark_fns.php');

    // 开始会话
    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');
    }
    echo 'Could not log you out.<br' />
    }
    }
     />
    do_html_url('login.php', 'Login');
    }
    do_html_footer();
    ?>

    register_form.php

    <?php
    require_once('bookmark_fns.php');
    do_html_header('用户注册');
    display_registration_form();
    do_html_footer();
    ?>
    register_new.php
    <?php
    require_once('bookmark_fns.php');
    // 变量
    $email = $_POST['email'];
    $username = $_POST['username'];
    $passwd = $_POST['passwd'];
    $passwd2 = $_POST['passwd2'];
    // 开始会话
    session_start();
    // 有效数据 
    try {
    if (!filled_out($_POST)) {
    throw new Exception("You have not filled out the form correctly - please go back and try again. 1);
    }
    if (!valid_email($email)) { 
    throw new Exception("这不是一个有效的电子邮件地址 - 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('注册成功');
    do_html_url('member.php', '转到会员页面');
    do_html_footer();
    } catch (Exception $e) {
    do_html_header('Problem: ');
    echo $e -> getMessage();
    do_html_footer();
    exit();
    }
    ?>

    forgot_form.php

    <?php
    require_once('bookmark_fns.php');
    do_html_header('重置密码');
    display_forgot_form();
    do_html_footer();
    ?>
    forgot_passwd.php
    <?php
    require_once('bookmark_fns.php');
    do_html_header('正在重置密码');
    $username = $_POST['username'];
    try {
    // 获取随机密码 
    $password = reset_password($username);
    notify_password($username, $password);
    echo "您的新密码已通过电子邮件发送给您。<br />";
    }catch(Exception $e){
    echo "您的密码无法重置 - 请稍后再试。";
    }
    do_html_url('login.php', 'Login');
    do_html_footer();
    ?>
    change_passwd_form.php
    <?php
    require_once('bookmark_fns.php');
    session_start();
    do_html_header('修改密码');
    check_valid_user();
    display_password_form();}}
    display_user_menu(); 
    do_html_footer();
    ?>
    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 out the form 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
    <?php
    // include function files for this application
    require_once('bookmark_fns.php');
    session_start();
    // start output HTML
    do_html_header('Add Bookmarks');
    check_valid_user();
    display_add_bm_form();
    display_user_menu();
    do_html_footer();
    ?>

    add_bms.php

    <?php
    require_once('bookmark_fns.php');
    session_start();
    $new_url = $_POST['new_url'];
    do_html_header('Adding bookmarks');
    try {
    check_valid_user();
    if (!filled_out($_POST)) {
    throw new Exception('The form is not completely filled out.');
    } 
    if (strstr($new_url, 'http://') === false)
    $new_url = 'http://'.$new_url;
    } 
    // check if the URL is valid
    if (!@fopen($new_url, 'r')) {
    throw new Exception('Not a valid URL.');
    } 
    add_bm($new_url);
    echo "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();
    ?>

    delete_bms.php

    <?php
    require_once('bookmark_fns.php');
    session_start();
    $del_me = $_POST['del_me'];
    $valid_user = $_SESSION['valid_user'];
    do_html_header('Deleting bookmarks');
    check_valid_user();
    if (!filled_out($_POST)) {
    echo "<p>You have not chosen any bookmarks to delete.<br />
    Please try again.</p>";
    display_user_menu();
    do_html_footer();
    exit;
    }
    if (count($del_me) > 0) {
    foreach ($del_me as $url) {
    if (delete_bm($valid_user, $url)) {
    echo "Deleted ".htmlspecialchars($url)."<br />";
    }
    echo "Could not deleted ".htmlspecialchars($url)."<br />";
    }
    }
    }
    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

    <?php
    require_once('bookmark_fns.php');
    session_start();
    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
    require_once('bookmark_fns.php');
    session_start();
    @$username = $_POST['username'];
    @$passwd = $_POST['passwd'];
    if ($username && $passwd) {
    try {
    // Log in 
    login($username, $passwd);
    $_SESSION['valid_user'] = $username;
    }catch(Exception $e) {
    do_html_header('Problem: ');
    echo "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();
    ?>

    The above-mentioned is the simple use of PHP user verification and tag recommendation introduced by the editor to everyone. I hope it will be helpful to everyone. If you have any questions, please leave a message, and the editor will reply to everyone in time. Here, I also want to express my sincere gratitude to everyone for supporting the Yelling Tutorial website!

    Statement: The content of this article is from the network, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been edited by humans, and does not assume any relevant legal liability. If you find any copyright-infringing content, please send an email to: notice#oldtoolbag.com (When sending an email, please replace # with @ to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)

  • You may also like