本文為大家分享了PHP線上書籤系統,感興趣的小夥伴們可以參考一下
1、需求分析
首先,需要識別每個使用者。應該有驗證機制。
其次,需要儲存單個使用者的書籤。使用者應該能夠添加和刪除書籤。
再次,需要根據對他們的瞭解,向使用者建議他們可能感興趣的網站。
2、解決方案
2.1 系統流程圖
2.2 PHPbookmark中的檔案清單
3、實現資料庫
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、實現基本的網站
4.1 login.php
<?php /** * 包含系統登入表單的頁面 */ //require_once語句和require語句完全相同,唯一區別是PHP會檢查該檔案是否已經被包含過,如果是則不會再次包含。 require_once('bookmark_fns.php'); //應用程式的包含檔案集合 do_html_header(''); //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('data_valid_fns.php'); //確認使用者輸入資料有效函數 require_once('db_fns.php'); // 串連資料庫的函數 require_once('user_auth_fns.php'); //使用者身分識別驗證的函數 require_once('output_fns.php'); //以HTML形式格式化輸出的函數 require_once('url_fns.php'); //增加和刪除書籤的函數 ?>
5、實現使用者身分識別驗證
5.1 register_form.php
<?php /** * 系統中使用者註冊表單 */ //require_once語句和require語句完全相同,唯一區別是PHP會檢查該檔案是否已經被包含過,如果是則不會再次包含。 require_once('bookmark_fns.php'); do_html_header('User Registration'); //HTML標題 display_registeration_form(); //輸出註冊表單 do_html_footer(); //HTML頁尾 ?>
5.2 register_new.php
<?php /** * 處理新註冊資訊的指令碼 */ //require_once語句和require語句完全相同,唯一區別是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 the form out correctly - please go back and try again.'); } //檢查郵件地址是否有效 if(!valid_email($email)) { throw new exception('That is not a vald email address. Please go back try again.'); } //檢查兩次輸入密碼是否相同 if($passwd != $passwd2) { throw new exception('The passwords you entered do not match - please go back try again.'); } //檢查密碼長度是否合格 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.'); } //嘗試註冊 register($username,$email,$passwd); //註冊會話變數 $_SESSION['valid_user'] = $username; //提供成員頁面連結 do_html_header('Registration successful'); //HTML標題 echo 'Your registration was successful.Go to the members page to start setting up your bookmarks!'; //輸出URL do_html_URL('member.php','Go to members page'); //HTML頁尾 do_html_footer(); //HTML頁尾 } catch(exception $e) { do_html_header('Problem:'); echo $e->getMessage(); do_html_footer(); exit; } ?>
5.3 member.php
<?php /** * 使用者的首頁面,包含該使用者所有的當前書籤 */ //require_once語句和require語句完全相同,唯一區別是PHP會檢查該檔案是否已經被包含過,如果是則不會再次包含。 require_once('bookmark_fns.php'); session_start(); //建立變數 $username = @$_POST['username']; $passwd = @$_POST['passwd']; if($username && $passwd) { try { 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(); ?>
5.4 logout.php
<?php /** * 將使用者登出的指令碼 */ //require_once語句和require語句完全相同,唯一區別是PHP會檢查該檔案是否已經被包含過,如果是則不會再次包含。 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.
'; do_html_URL('login.php','Login'); } else //不成功 { echo 'Could not log you out.
'; } } else { echo 'You were not logged in, and so have not been logged ot.
'; do_html_URL('login.php','Login'); } do_html_footer(); ?>
5.5 change_passwd.php
<?php /** * 修改資料庫中使用者密碼的表單 */ //require_once語句和require語句完全相同,唯一區別是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 completely.Please try again.'); if($new_passwd != $new_passwd2) throw new exception('Passwords entered were not the same. Not changed.'); if((strlen($new_passwd) > 16) || (strlen($new_passwd) < 6)) { throw new exception('New password must be between 6 and 16 characters. Try again.'); } //嘗試修改 change_password($_SESSION['valid_user'],$old_passwd,$new_passwd); echo 'Password changed.'; } catch(exception $e) { echo $e ->getMessage(); } display_user_menu(); do_html_footer(); ?>
5.6 forgot_paswd.php
<?php /** * 重新設定遺忘密碼的指令碼 */ //require_once語句和require語句完全相同,唯一區別是PHP會檢查該檔案是否已經被包含過,如果是則不會再次包含。 require_once('bookmark_fns.php'); do_html_header("Resetting password"); //建立變數 $username = $_POST['username']; try { $passwd = reset_password($username); notify_password($username,$passwd); echo 'Your new password has been emailed to you.
'; } catch(exception $e) { echo 'Your password could not be reset - please try again later.'; } do_html_URL('login.php','Login'); do_html_footer(); ?>
6、實現書籤的儲存和檢索
6.1 add_bms.php
<?php /** * 添加書籤的表單 */ //require_once語句和require語句完全相同,唯一區別是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($new_url)) //檢查表單是否填寫 throw new exception('Form not completely filled out.'); if(strstr($new_url,'http://') === false) $new_url = 'http://'. $new_url; if(!(@fopen($new_url,'r'))) //可以調用fopen()函數開啟URL,如果能開啟這個檔案,則假定URL是有效 throw new exception('Not a valid URL.'); add_bm($new_url); //將URL添加到資料庫中 echo 'Bookmark added.'; if($url_array = get_user_urls($_SESSION['valid_user'])) 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('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($del_me)) // { echo '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 delete '. htmlspecialchars($url) .'.
'; } } } else { echo 'No bookmarks selected for deletion'; } } 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('bookmark_fns.php'); session_start(); do_html_header('Recommending URLs'); try { check_valid_user(); $urls = recommend_urls($_SESSION['valid_user']); display_recommended_urls($urls); } catch(exception $e) { echo $e ->getMessage(); } display_user_menu(); do_html_footer(); ?>
以上就是PHP線上書籤系統的詳細代碼,希望對大家的學習有所協助。