PHP用户验证和标签推荐的简单使用(2)

<?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><?php echo $title;?></title> <style> 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> <body> <img src="https://www.jb51.net/005.png" alt="PHPbookmark logo" valign="bottom" /> <h1>PHPbookmark</h1> <hr /> <?php if($title) { do_html_heading($title); } } function do_html_footer() { // print an HTML footer ?> </body> </html> <?php } function do_html_heading($heading) { // print heading ?> <h2><?php echo $heading;?></h2> <?php } function do_html_URL($url, $name) { // output URL as link and br ?> <br /><a href="<?php echo $url;?>"><?php echo $name;?></a><br /> <?php } function display_site_info() { // display some marketing info ?> <ul> <li>Store your bookmarks online with us!</li> <li>See what other users use!</li> <li>Share your favorite links with others!</li> </ul> <?php } function display_login_form() { ?> <p><a href="https://www.jb51.net/register_form.php">Not a member?</a></p> <form method="post" action="https://www.jb51.net/member.php"> <table bgcolor="#cccccc"> <tr> <td colspan="2">Members log in here:</td> <tr> <td>Username:</td> <td><input type="text"/></td></tr> <tr> <td>Password:</td> <td><input type="password"/></td></tr> <tr> <td colspan="2"> <input type="submit" value="Log in"/></td></tr> <tr> <td colspan="2"><a href="https://www.jb51.net/forgot_form.php">Forgot your password?</a></td> </tr> </table></form> <?php } function display_registration_form() { ?> <form method="post" action="register_new.php"> <table bgcolor="#cccccc"> <tr> <td>Email address:</td> <td><input type="text" size="30" maxlength="100"/></td></tr> <tr> <td>Preferred username <br />(max 16 chars):</td> <td valign="top"><input type="text" size="16" maxlength="16"/></td></tr> <tr> <td>Password <br />(between 6 and 16 chars):</td> <td valign="top"><input type="password" size="16" maxlength="16"/></td></tr> <tr> <td>Confirm password:</td> <td><input type="password" size="16" maxlength="16"/></td></tr> <tr> <td colspan=2> <input type="submit" value="Register"></td></tr> </table></form> <?php } function display_user_urls($url_array) { // display the table of URLs // set global variable, so we can test later if this is on the page global $bm_table; $bm_table = true; ?> <br /> <form action="delete_bms.php" method="post"> <table cellpadding="2" cellspacing="0"> <?php $color = "#cccccc"; echo "<tr bgcolor=https://www.jb51.net/article/\"".$color."https://www.jb51.net/article/\"><td><strong>Bookmark</strong></td>"; echo "<td><strong>Delete?</strong></td></tr>"; if ((is_array($url_array)) && (count($url_array) > 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 "<tr bgcolor=https://www.jb51.net/article/\"".$color."https://www.jb51.net/article/\"><td><a href=https://www.jb51.net/article/\"".$url."https://www.jb51.net/article/\">".htmlspecialchars($url)."</a></td> <td><input type=https://www.jb51.net/article/\"checkbox\" name=https://www.jb51.net/article/\"del_me[]\" value=https://www.jb51.net/article/\"".$url."https://www.jb51.net/article/\"/></td> </tr>"; } } else { echo "<tr><td>No bookmarks on record</td></tr>"; } ?> </table> </form> <?php } function display_user_menu() { // display the menu options on this page ?> <hr /> <a href="https://www.jb51.net/member.php">Home</a> &nbsp;|&nbsp; <a href="https://www.jb51.net/add_bm_form.php">Add BM</a> &nbsp;|&nbsp; <?php // only offer the delete option if bookmark table is on this page global $bm_table; if ($bm_table == true) { echo "<a href=https://www.jb51.net/article/\"#\" onClick=https://www.jb51.net/article/\"bm_table.submit();\">Delete BM</a> &nbsp;|&nbsp;"; } else { echo "<span style=https://www.jb51.net/article/\"color: #cccccc\">Delete BM</span> &nbsp;|&nbsp;"; } ?> <a href="https://www.jb51.net/change_passwd_form.php">Change password</a> <br /> <a href="https://www.jb51.net/recommend.php">Recommend URLs to me</a> &nbsp;|&nbsp; <a href="https://www.jb51.net/logout.php">Logout</a> <hr /> <?php } function display_add_bm_form() { // display the form for people to ener a new bookmark in ?> <form action="add_bms.php" method="post"> <table cellpadding="2" cellspacing="0" bgcolor="#cccccc"> <tr><td>New BM:</td> <td><input type="text" value="http://" size="30" maxlength="255"/></td></tr> <tr><td colspan="2"> <input type="submit" value="Add bookmark"/></td></tr> </table> </form> <?php } function display_password_form() { // display html change password form ?> <br /> <form action="change_passwd.php" method="post"> <table cellpadding="2" cellspacing="0" bgcolor="#cccccc"> <tr><td>Old password:</td> <td><input type="password" size="16" maxlength="16"/></td> </tr> <tr><td>New password:</td> <td><input type="password" size="16" maxlength="16"/></td> </tr> <tr><td>Repeat new password:</td> <td><input type="password" size="16" maxlength="16"/></td> </tr> <tr><td colspan="2"> <input type="submit" value="Change password"/> </td></tr> </table> <br /> <?php } function display_forgot_form() { // display HTML form to reset and email password ?> <br /> <form action="forgot_passwd.php" method="post"> <table cellpadding="2" cellspacing="0" bgcolor="#cccccc"> <tr><td>Enter your username</td> <td><input type="text" size="16" maxlength="16"/></td> </tr> <tr><td colspan=2> <input type="submit" value="Change password"/> </td></tr> </table> <br /> <?php } function display_recommended_urls($url_array) { // similar output to display_user_urls // instead of displaying the users bookmarks, display recomendation ?> <br /> <table cellpadding="2" cellspacing="0"> <?php $color = "#cccccc"; echo "<tr bgcolor=https://www.jb51.net/article/\"".$color."https://www.jb51.net/article/\"> <td><strong>Recommendations</strong></td></tr>"; if ((is_array($url_array)) && (count($url_array)>0)) { foreach ($url_array as $url) { if ($color == "#cccccc") { $color = "#ffffff"; } else { $color = "#cccccc"; } echo "<tr bgcolor=https://www.jb51.net/article/\"".$color."https://www.jb51.net/article/\"> <td><a href=https://www.jb51.net/article/\"".$url."https://www.jb51.net/article/\">".htmlspecialchars($url)."</a></td></tr>"; } } else { echo "<tr><td>No recommendations for you today.</td></tr>"; } ?> </table> <?php } ?> login.php <?php require_once('bookmark_fns.php'); do_html_header(''); display_site_info(); display_login_form(); do_html_footer(); ?> logout.php <?php

require_once('bookmark_fns.php');

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/e491488caa6d95c5c9352240082a9d5a.html