在使用PHP編程的時候,我有一個習慣,不太喜歡使用現成的庫檔案,例如PHPLib或者其它類似的庫,在這個系統中,我也打算自己寫一個庫檔案,它需要處理認證、確認email,更新帳號(密碼,email)等事情。
<?php
function user_change_email ($password1,$new_email,$user_name) {
global $feedback,$hidden_hash_var;
if (validate_email($new_email)) {
$hash=md5($new_email.$hidden_hash_var);
file://改變資料庫中確認用的無序碼值,但不改變email
file://發出一個帶有新認證碼的確認email
$user_name=strtolower($user_name);
$password1=strtolower($password1);
$sql="UPDATE user SET confirm_hash=''$hash'' WHERE user_name=''$user_name'' AND password=''". md5($password1) ."''";
$result=db_query($sql);
if (!$result || db_affected_rows($result) < 1) {
$feedback .= '' ERROR - Incorrect User Name Or Password '';
return false;
} else {
$feedback .= '' Confirmation Sent '';
user_send_confirm_email($new_email,$hash);
return true;
}
} else {
$feedback .= '' New Email Address Appears Invalid '';
return false;
}
}
function user_confirm($hash,$email) {
/*
使用者點擊認證email的相關串連時,連到一個確認的頁面,該頁面會調用這個函數,
*/
global $feedback,$hidden_hash_var;
file://verify that they didn''t tamper with the email address
$new_hash=md5($email.$hidden_hash_var);
if ($new_hash && ($new_hash==$hash)) {
file://在資料庫中找出這個記錄
$sql="SELECT * FROM user WHERE confirm_hash=''$hash''";
$result=db_query($sql);
if (!$result || db_numrows($result) < 1) {
$feedback .= '' ERROR - Hash Not Found '';
return false;
} else {
file://確認email,並且設定帳號為已經啟用
$feedback .= '' User Account Updated - You Are Now Logged In '';
user_set_tokens(db_result($result,0,''user_name''));
$sql="UPDATE user SET email=''$email'',is_confirmed=''1'' WHERE confirm_hash=''$hash''";
$result=db_query($sql);
<