>  기사  >  백엔드 개발  >  PHP 북마크 시스템 케이스

PHP 북마크 시스템 케이스

墨辰丷
墨辰丷원래의
2018-06-05 14:45:262016검색

이 글은 주로 PHP 북마크 시스템의 사례를 소개합니다. 관심있는 친구들이 참고하시면 좋을 것 같습니다.

1. 수요 분석

먼저, 각 사용자를 식별해야 합니다. 검증 메커니즘이 있어야 합니다.
둘째, 개별 사용자의 북마크를 저장해야 합니다. 사용자는 북마크를 추가하고 삭제할 수 있어야 합니다.
다시 말하지만, 사용자는 자신에 대해 알려진 정보를 바탕으로 관심을 가질 수 있는 사이트에 대한 조언을 받아야 합니다.

2. 솔루션2.1 시스템 흐름도

2.2 PHPbookmark의 파일 목록

3. 기본 웹사이트 구현

4.

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.2 Bookmark_fns.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页脚 
?>

5. 사용자 인증 구현5.1register_form.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.2register_new.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.3 member.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.4 logout.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.5change_passw d.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.6 forgot_paswd.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(); 
?>

6. 북마크 저장 및 검색을 구현합니다. 는 모든 분들의 공부에 도움이 되는 글이 되었으면 좋겠습니다.

관련 권장 사항:

PHP의 클래스 속성 및 클래스 정적 변수에 대한 액세스 방법의 예

작동 원리에 대한 자세한 설명과 PHP 쿠키의 예

PHP는 소켓을 사용하여 POST를 시뮬레이션합니다. 방법

위 내용은 PHP 북마크 시스템 케이스의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.