加入收藏 | 设为首页 | 会员中心 | 我要投稿 宁德站长网 (https://www.0593zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2016-11-27 09:24:05 所属栏目:大数据 来源:站长网
导读:本文给大家讲解一些最简单的验证知识。大家可以先看下效果图,如果大家感觉还不错,请参考实现代码。 效果图 bookmark_fns.php lt;#63;phprequire_once('output_fns.php');require_once('db_fns.php');require_once('data_valid_fns.php');require_once('u

本文给大家讲解一些最简单的验证知识。大家可以先看下效果图,如果大家感觉还不错,请参考实现代码。

效果图

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

bookmark_fns.php

lt;#63;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');
#63;gt;

data_valid_fns.php

lt;#63;php
// Test that each variable has a value
function filled_out($form_vars) {
foreach ($form_vars as $key =gt; $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;
}
}
#63;gt;

db_fns.php

lt;#63;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;
}
}
#63;gt;

user_auth_fns.php

lt;#63;php
require_once('db_fns.php');
// register 
function register($username, $email, $password) {
$conn = db_connect();
$results = $conn -gt; query("select * from user where username = '".$username."'");
if (!$results) {
throw new Exception("Could not execute query", 1);
}
if ($results -gt; num_rows gt; 0) {
throw new Exception("That username is taken - go back and choose another one.", 1);
} 
$results = $conn -gt; 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 -gt; query("select * from user where username = '".$username."' and passwd = sha1('".$password."')");
if (!$results) {
throw new Exception('Could not log you in.');
}
if ($results -gt; num_rows gt; 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'].".lt;br /gt;";
}else {
do_html_header('Problem:');
echo "You are not logged in.lt;br /gt;";
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 -gt; 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) lt; $min_length) || (strlen($word)gt;$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-gt;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-gt;query("select email from user
where username='".$username."'");
if (!$result) {
throw new Exception('Could not find email address.');
} else if ($result-gt;num_rows == 0) {
throw new Exception('Could not find email address.');
// username not in db
} else {
$row = $result-gt;fetch_object();
$email = $row-gt;email;
$from = "From: support@phpbookmark rn";
$mesg = "Your PHPBookmark password has been changed to ".$password."rn"
."Please change it next time you log in.rn";
if (mail($email, 'PHPBookmark login information', $mesg, $from)) {
return true;
} else {
throw new Exception('Could not send email.');
}
}
}
#63;gt;

(编辑:宁德站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!