search
HomeBackend DevelopmentPHP TutorialPHP bookmark system case

PHP bookmark system case

Jun 05, 2018 pm 02:45 PM
php

This article mainly introduces the case of PHP bookmark system. Interested friends can refer to it. I hope it will be helpful to everyone.

1. Requirements analysis

First, each user needs to be identified. There should be a verification mechanism.
Secondly, you need to save a single user's bookmarks. Users should be able to add and delete bookmarks.
Again, it is necessary to recommend sites to users that may be of interest to them based on what is known about them.

2. Solution2.1 System flow chart

##2.2 File list in PHPbookmark

3. Implement database

create database bookmarks; 
use bookmarks; 
 
create table user ( 
 username varchar(16) primary key, 
 passwd char(40) not null, 
 email varchar(100) not null 
); 
 
create table bookmark ( 
 username varchar(16) not null, 
 bm_URL varchar(255) not null, 
 index (username), 
 index (bm_URL) 
); 
 
grant select, insert, update, delete 
on bookmarks.* 
to bm_user@localhost identified by 'password';

4. Implement basic website4.1 login.php

<?php 
 
/** 
 * 包含系统登录表单的页面 
 */ 
  //require_once语句和require语句完全相同,唯一区别是PHP会检查该文件是否已经被包含过,如果是则不会再次包含。 
  require_once(&#39;bookmark_fns.php&#39;);  //应用程序的包含文件集合 
   
  do_html_header(&#39;&#39;); //HTML标题 
   
  display_site_info();//HTML站点信息 
  display_login_form();//HTML登录信息 
   
  do_html_footer();  //HTML页脚 
?>

4.2 bookmark_fns.php

<?php 
 
/** 
 * 应用程序的包含文件集合 
 */ 
  //require_once语句和require语句完全相同,唯一区别是PHP会检查该文件是否已经被包含过,如果是则不会再次包含。 
  require_once(&#39;data_valid_fns.php&#39;); //确认用户输入数据有效的函数 
  require_once(&#39;db_fns.php&#39;); // 连接数据库的函数 
  require_once(&#39;user_auth_fns.php&#39;); //用户身份验证的函数 
  require_once(&#39;output_fns.php&#39;); //以HTML形式格式化输出的函数 
  require_once(&#39;url_fns.php&#39;);  //增加和删除书签的函数 
?>

5. Implement user authentication5.1 register_form.php

<?php 
 
/** 
 * 系统中用户注册表单 
 */ 
  //require_once语句和require语句完全相同,唯一区别是PHP会检查该文件是否已经被包含过,如果是则不会再次包含。 
  require_once(&#39;bookmark_fns.php&#39;); 
  do_html_header(&#39;User Registration&#39;);  //HTML标题 
   
  display_registeration_form();  //输出注册表单 
   
  do_html_footer();  //HTML页脚 
?>

5.2 register_new.php

<?php 
 
/** 
 * 处理新注册信息的脚本 
 */ 
  //require_once语句和require语句完全相同,唯一区别是PHP会检查该文件是否已经被包含过,如果是则不会再次包含。 
  require_once(&#39;bookmark_fns.php&#39;); 
   
  //创建变量 
  $email = $_POST[&#39;email&#39;]; 
  $username = $_POST[&#39;username&#39;]; 
  $passwd = $_POST[&#39;passwd&#39;]; 
  $passwd2 = $_POST[&#39;passwd2&#39;]; 
 
  //开启会话 
  session_start(); 
   
  try 
  { 
    //检查表单是否填写满 
    if(!filled_out($_POST)) 
    { 
      throw new exception(&#39;You have not filled the form out correctly - please go back and try again.&#39;); 
    } 
     
    //检查邮件地址是否有效 
    if(!valid_email($email)) 
    { 
      throw new exception(&#39;That is not a vald email address. Please go back try again.&#39;); 
    } 
     
    //检查两次输入密码是否相同 
    if($passwd != $passwd2) 
    { 
      throw new exception(&#39;The passwords you entered do not match - please go back try again.&#39;); 
    } 
     
    //检查密码长度是否合格 
    if((strlen($passwd) < 6) || (strlen($passwd) > 16)) 
    { 
      throw new exception(&#39;Your password must be between 6 and 16 characters Please go back and try again.&#39;); 
    } 
     
    //尝试注册 
    register($username,$email,$passwd); 
     
    //注册会话变量 
    $_SESSION[&#39;valid_user&#39;] = $username; 
     
    //提供成员页面链接 
    do_html_header(&#39;Registration successful&#39;); //HTML标题 
    echo &#39;Your registration was successful.Go to the members page to start setting up your bookmarks!&#39;; //输出URL 
    do_html_URL(&#39;member.php&#39;,&#39;Go to members page&#39;); //HTML页脚 
    do_html_footer();  //HTML页脚 
  } 
  catch(exception $e) 
  { 
    do_html_header(&#39;Problem:&#39;); 
    echo $e->getMessage(); 
    do_html_footer(); 
    exit; 
  } 
?>

5.3 member.php

<?php 
 
/** 
 * 用户的主页面,包含该用户所有的当前书签 
 */ 
  //require_once语句和require语句完全相同,唯一区别是PHP会检查该文件是否已经被包含过,如果是则不会再次包含。 
  require_once(&#39;bookmark_fns.php&#39;); 
  session_start(); 
   
  //创建变量 
  $username = @$_POST[&#39;username&#39;]; 
  $passwd = @$_POST[&#39;passwd&#39;]; 
   
  if($username && $passwd) 
  { 
    try 
    { 
      login($username,$passwd); 
      //如果该用户在数据库中,则注册会话变量 
      $_SESSION[&#39;valid_user&#39;] = $username; 
    } 
    catch(exception $e) 
    { 
      //登录不成功 
      do_html_header(&#39;Problem:&#39;); 
      echo &#39;You could not be logged in. You must be logged in to view this page.&#39;; 
      do_html_URL(&#39;login.php&#39;,&#39;Login&#39;); 
      do_html_footer(); 
      exit; 
    } 
  } 
   
  do_html_header(&#39;Home&#39;); 
  check_valid_user(); 
   
  //获取用户的书签 
  if($url_array = get_user_urls($_SESSION[&#39;valid_user&#39;])) 
    display_user_urls($url_array); 
  //获取用户菜单选项 
  display_user_menu(); 
 
  do_html_footer(); 
?>

5.4 logout.php

<?php 
 
/** 
 * 将用户注销的脚本 
 */ 
  //require_once语句和require语句完全相同,唯一区别是PHP会检查该文件是否已经被包含过,如果是则不会再次包含。 
  require_once(&#39;bookmark_fns.php&#39;); 
  session_start(); 
  $old_user = $_SESSION[&#39;valid_user&#39;]; 
   
  //注销会话变量 
  unset($_SESSION[&#39;valid_user&#39;]); 
  $result_dest = session_destroy(); 
   
  do_html_header(&#39;Logging Out&#39;); 
   
  if(!empty($old_user)) 
  { 
    if($result_dest)  //登出成功 
    { 
      echo &#39;Logged out.<br />&#39;; 
      do_html_URL(&#39;login.php&#39;,&#39;Login&#39;); 
    } 
    else  //不成功 
    { 
      echo &#39;Could not log you out.<br />&#39;; 
    } 
  } 
  else 
  { 
    echo &#39;You were not logged in, and so have not been logged ot.<br />&#39;; 
    do_html_URL(&#39;login.php&#39;,&#39;Login&#39;); 
  } 
  do_html_footer(); 
?>

5.5 change_passwd.php

<?php 
 
/** 
 * 修改数据库中用户密码的表单 
 */ 
  //require_once语句和require语句完全相同,唯一区别是PHP会检查该文件是否已经被包含过,如果是则不会再次包含。 
  require_once(&#39;bookmark_fns.php&#39;); 
  session_start(); 
  do_html_header(&#39;Changing password&#39;); 
   
  //创建变量 
  $old_passwd = $_POST[&#39;old_passwd&#39;]; 
  $new_passwd = $_POST[&#39;new_passwd&#39;]; 
  $new_passwd2 = $_POST[&#39;new_passwd2&#39;]; 
   
  try 
  { 
    check_valid_user(); 
    if(!filled_out($_POST)) 
      throw new exception(&#39;You have not filled out the form completely.Please try again.&#39;); 
     
    if($new_passwd != $new_passwd2) 
      throw new exception(&#39;Passwords entered were not the same. Not changed.&#39;); 
       
    if((strlen($new_passwd) > 16) || (strlen($new_passwd) < 6)) 
    { 
      throw new exception(&#39;New password must be between 6 and 16 characters. Try again.&#39;); 
    } 
     
    //尝试修改 
    change_password($_SESSION[&#39;valid_user&#39;],$old_passwd,$new_passwd); 
    echo &#39;Password changed.&#39;; 
  } 
  catch(exception $e) 
  { 
    echo $e ->getMessage(); 
  } 
  display_user_menu(); 
  do_html_footer(); 
?>

5.6 forgot_paswd.php

<?php 
 
/** 
 * 重新设置遗忘密码的脚本 
 */ 
  //require_once语句和require语句完全相同,唯一区别是PHP会检查该文件是否已经被包含过,如果是则不会再次包含。 
  require_once(&#39;bookmark_fns.php&#39;); 
  do_html_header("Resetting password"); 
   
  //创建变量 
  $username = $_POST[&#39;username&#39;]; 
   
  try 
  { 
    $passwd = reset_password($username); 
    notify_password($username,$passwd); 
    echo &#39;Your new password has been emailed to you.<br />&#39;; 
  } 
  catch(exception $e) 
  { 
    echo &#39;Your password could not be reset - please try again later.&#39;; 
  } 
  do_html_URL(&#39;login.php&#39;,&#39;Login&#39;); 
  do_html_footer(); 
?>

6. Implement bookmark storage and retrieval6.1 add_bms.php

<?php 
 
/** 
 * 添加书签的表单 
 */ 
  //require_once语句和require语句完全相同,唯一区别是PHP会检查该文件是否已经被包含过,如果是则不会再次包含。 
  require_once(&#39;bookmark_fns.php&#39;); 
  session_start(); 
   
  //创建变量 
  $new_url = $_POST[&#39;new_url&#39;]; 
   
  do_html_header(&#39;Adding bookmarks&#39;); 
   
  try 
  { 
    check_valid_user(); //检查用户有效性 
    if(!filled_out($new_url))  //检查表单是否填写 
      throw new exception(&#39;Form not completely filled out.&#39;); 
    if(strstr($new_url,&#39;http://&#39;) === false) 
      $new_url = &#39;http://&#39;. $new_url; 
    if(!(@fopen($new_url,&#39;r&#39;))) //可以调用fopen()函数打开URL,如果能打开这个文件,则假定URL是有效的 
      throw new exception(&#39;Not a valid URL.&#39;); 
    add_bm($new_url);  //将URL添加到数据库中 
    echo &#39;Bookmark added.&#39;; 
    if($url_array = get_user_urls($_SESSION[&#39;valid_user&#39;])) 
      display_user_urls($url_array); 
  } 
  catch(exception $e) 
  { 
    echo $e ->getMessage(); 
  } 
  display_user_menu(); 
  do_html_footer(); 
?>

6.2 delete_bms.php

<?php 
 
/** 
 * 从用户的书签列表中删除选定书签的脚本呢 
 */ 
  //require_once语句和require语句完全相同,唯一区别是PHP会检查该文件是否已经被包含过,如果是则不会再次包含。 
  require_once(&#39;bookmark_fns.php&#39;); 
  session_start(); 
   
  //创建变量 
  $del_me = @$_POST[&#39;del_me&#39;]; 
  $valid_user = $_SESSION[&#39;valid_user&#39;]; 
   
  do_html_header(&#39;Deleting bookmarks&#39;); 
  check_valid_user(); 
   
  if(!filled_out($del_me))  // 
  { 
    echo &#39;<p>You have not chosen any bookmarks to delete.<br />Please try again.</p>&#39;; 
    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 &#39;Deleted &#39;. htmlspecialchars($url) .&#39;.<br />&#39;; 
        } 
        else 
        { 
          echo &#39;Could not delete &#39;. htmlspecialchars($url) .&#39;.<br />&#39;; 
        } 
      } 
    } 
    else 
    { 
      echo &#39;No bookmarks selected for deletion&#39;; 
    } 
  } 
  if($url_array = get_user_urls($valid_user)) 
  { 
    display_user_urls($url_array); 
  } 
  display_user_menu(); 
  do_html_footer(); 
?>

6.3 recommend.php

<?php 
 
/** 
 * 基于用户以前的操作,推荐用户可能感兴趣的书签 
 */ 
  //require_once语句和require语句完全相同,唯一区别是PHP会检查该文件是否已经被包含过,如果是则不会再次包含。 
  require_once(&#39;bookmark_fns.php&#39;); 
  session_start(); 
  do_html_header(&#39;Recommending URLs&#39;); 
  try 
  { 
    check_valid_user(); 
    $urls = recommend_urls($_SESSION[&#39;valid_user&#39;]); 
    display_recommended_urls($urls); 
  } 
  catch(exception $e) 
  { 
    echo $e ->getMessage(); 
  } 
  display_user_menu(); 
  do_html_footer(); 
?>

Summary: That’s it The entire content of the article is hoped to be helpful to everyone's study.

Related recommendations:

Examples of access methods for class attributes and class static variables in PHP

php cookie working principle and detailed explanation of examples

PHP uses socket to simulate POST method

The above is the detailed content of PHP bookmark system case. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software