时间:2021-05-02
本文给大家讲解一些最简单的验证知识。大家可以先看下效果图,如果大家感觉还不错,请参考实现代码。
效果图
bookmark_fns.php
? 1 2 3 4 5 6 7 <?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
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <?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; }else { return false; } } ?>db_fns.php
? 1 2 3 4 5 6 7 8 9 10 11 <?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); }else { return $db; } } ?>user_auth_fns.php
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 <?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; }else { 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 />"; }else { 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
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 <?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
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 <?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="005.png" alt="phpbookmark logo" border="0" align="left" valign="bottom" height="55" width="57" /> <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="register_form.php">not a member?</a></p> <form method="post" action="member.php"> <table bgcolor="#cccccc"> <tr> <td colspan="2">members log in here:</td> <tr> <td>username:</td> <td><input type="text" name="username"/></td></tr> <tr> <td>password:</td> <td><input type="password" name="passwd"/></td></tr> <tr> <td colspan="2" align="center"> <input type="submit" value="log in"/></td></tr> <tr> <td colspan="2"><a href="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" name="email" size="30" maxlength="100"/></td></tr> <tr> <td>preferred username <br />(max 16 chars):</td> <td valign="top"><input type="text" name="username" size="16" maxlength="16"/></td></tr> <tr> <td>password <br />(between 6 and 16 chars):</td> <td valign="top"><input type="password" name="passwd" size="16" maxlength="16"/></td></tr> <tr> <td>confirm password:</td> <td><input type="password" name="passwd2" size="16" maxlength="16"/></td></tr> <tr> <td colspan=2 align="center"> <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 name="bm_table" action="delete_bms.php" method="post"> <table width="300" cellpadding="2" cellspacing="0"> <?php $color = "#cccccc"; echo "<tr bgcolor=\"".$color."\"><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=\"".$color."\"><td><a href=\"".$url."\">".htmlspecialchars($url)."</a></td> <td><input type=\"checkbox\" name=\"del_me[]\" value=\"".$url."\"/></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="member.php">home</a> | <a href="add_bm_form.php">add bm</a> | <?php // only offer the delete option if bookmark table is on this page global $bm_table; if ($bm_table == true) { echo "<a href=\"#\" onclick=\"bm_table.submit();\">delete bm</a> |"; } else { echo "<span style=\"color: #cccccc\">delete bm</span> |"; } ?> <a href="change_passwd_form.php">change password</a> <br /> <a href="recommend.php">recommend urls to me</a> | <a href="logout.php">logout</a> <hr /> <?php } function display_add_bm_form() { // display the form for people to ener a new bookmark in ?> <form name="bm_table" action="add_bms.php" method="post"> <table width="250" cellpadding="2" cellspacing="0" bgcolor="#cccccc"> <tr><td>new bm:</td> <td><input type="text" name="new_url" value="http://" size="30" maxlength="255"/></td></tr> <tr><td colspan="2" align="center"> <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 width="250" cellpadding="2" cellspacing="0" bgcolor="#cccccc"> <tr><td>old password:</td> <td><input type="password" name="old_passwd" size="16" maxlength="16"/></td> </tr> <tr><td>new password:</td> <td><input type="password" name="new_passwd" size="16" maxlength="16"/></td> </tr> <tr><td>repeat new password:</td> <td><input type="password" name="new_passwd2" size="16" maxlength="16"/></td> </tr> <tr><td colspan="2" align="center"> <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 width="250" cellpadding="2" cellspacing="0" bgcolor="#c声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
自己编写了一个表单验证插件,支持ajax验证,使用起来很简单。每个需要验证的表单元素下面有一个span标签,这个标签的class有一个valid表示需要验证,如
PHP过滤器用于验证和过滤来自非安全来源的数据,比如用户的输入。什么是PHP过滤器?PHP过滤器用于验证和过滤来自非安全来源的数据。验证和过滤用户输入或自定义数
本文实例讲述了php使用Header函数,PHP_AUTH_PW和PHP_AUTH_USER做用户验证的方法。分享给大家供大家参考,具体如下:在php中,可以使
PHP过滤器用于验证和过滤来自非安全来源的数据,比如用户的输入。什么是PHP过滤器?PHP过滤器用于验证和过滤来自非安全来源的数据。验证和过滤用户输入或自定义数
本文为大家分享了一个表单验证插件,支持ajax验证,使用起来很简单。每个需要验证的表单元素下面有一个span标签,这个标签的class有一个valid表示需要验