仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 811|回复: 18
打印 上一主题 下一主题

[学习教程] PHP教程之一个用于网站用户办理的函数库(原作者:...

[复制链接]
飘灵儿 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-2-4 00:08:50 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
我的这套线路可能跟许多学习PHP的爱好者不谋而合,这也算是一个循序渐进的学习过程,不过新手不要看到上面的概括就以为学习蛮简单的,默默在此不得不对您稍微泼一下冷水,任何东西其实都不简单。函数|用户办理   <?php

$hidden_hash_var='your_password_here';

$LOGGED_IN=false;
//clear it out in case someone sets it in the URL or something
unset($LOGGED_IN);

/*

create table user (
user_id int not null auto_increment primary key,
user_name text,
real_name text,
email text,
password text,
remote_addr text,
confirm_hash text,
is_confirmed int not null default 0
);

*/

function user_isloggedin() {
global $user_name,$id_hash,$hidden_hash_var,$LOGGED_IN;
//have we already run the hash checks?
//If so, return the pre-set var
if (isset($LOGGED_IN)) {
return $LOGGED_IN;
}
if ($user_name && $id_hash) {
$hash=md5($user_name.$hidden_hash_var);
if ($hash == $id_hash) {
$LOGGED_IN=true;
return true;
} else {
$LOGGED_IN=false;
return false;
}
} else {
$LOGGED_IN=false;
return false;
}
}

function user_login($user_name,$password) {
global $feedback;
if (!$user_name || !$password) {
$feedback .= ' ERROR - Missing user name or password ';
return false;
} else {
$user_name=strtolower($user_name);
$password=strtolower($password);
$sql="SELECT * FROM user WHERE user_name='$user_name' AND password='". md5($password) ."'";
$result=db_query($sql);
if (!$result || db_numrows($result) < 1){
$feedback .= ' ERROR - User not found or password incorrect ';
return false;
} else {
if (db_result($result,0,'is_confirmed') == '1') {
user_set_tokens($user_name);
$feedback .= ' SUCCESS - You Are Now Logged In ';
return true;
} else {
$feedback .= ' ERROR - You haven\'t Confirmed Your Account Yet ';
return false;
}
}
}
}

function user_logout() {
setcookie('user_name','',(time()+2592000),'/','',0);
setcookie('id_hash','',(time()+2592000),'/','',0);
}

function user_set_tokens($user_name_in) {
global $hidden_hash_var,$user_name,$id_hash;
if (!$user_name_in) {
$feedback .= ' ERROR - User Name Missing When Setting Tokens ';
return false;
}
$user_name=strtolower($user_name_in);
$id_hash= md5($user_name.$hidden_hash_var);

setcookie('user_name',$user_name,(time()+2592000),'/','',0);
setcookie('id_hash',$id_hash,(time()+2592000),'/','',0);
}

function user_confirm($hash,$email) {
/*
Call this function on the user confirmation page,
which they arrive at when the click the link in the
account confirmation email
*/

global $feedback,$hidden_hash_var;

//verify that they didn't tamper with the email address
$new_hash=md5($email.$hidden_hash_var);
if ($new_hash && ($new_hash==$hash)) {
//find this record in the db
$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 {
//confirm the email and set account to active
$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);
return true;
}
} else {
$feedback .= ' HASH INVALID - UPDATE FAILED ';
return false;
}
}

function user_change_password ($new_password1,$new_password2,$change_user_name,$old_password) {
global $feedback;
//new passwords present and match?
if ($new_password1 && ($new_password1==$new_password2)) {
//is this password long enough?
if (account_pwvalid($new_password1)) {
//all vars are present?
if ($change_user_name && $old_password) {
//lower case everything
$change_user_name=strtolower($change_user_name);
$old_password=strtolower($old_password);
$new_password1=strtolower($new_password1);
$sql="SELECT * FROM user WHERE user_name='$change_user_name' AND password='". md5($old_password) ."'";
$result=db_query($sql);
if (!$result || db_numrows($result) < 1) {
$feedback .= ' User not found or bad password '.db_error();
return false;
} else {
$sql="UPDATE user SET password='". md5($new_password1). "' ".
"WHERE user_name='$change_user_name' AND password='". md5($old_password). "'";
$result=db_query($sql);
if (!$result || db_affected_rows($result) < 1) {
$feedback .= ' NOTHING Changed '.db_error();
return false;
} else {
$feedback .= ' Password Changed ';
return true;
}
}
} else {
$feedback .= ' Must Provide User Name And Old Password ';
return false;
}
} else {
$feedback .= ' New Passwords Doesn\'t Meet Criteria ';
return false;
}
} else {
return false;
$feedback .= ' New Passwords Must Match ';
}
}

function user_lost_password ($email,$user_name) {
global $feedback,$hidden_hash_var;
if ($email && $user_name) {
$user_name=strtolower($user_name);
$sql="SELECT * FROM user WHERE user_name='$user_name' AND email='$email'";
$result=db_query($sql);
if (!$result || db_numrows($result) < 1) {
//no matching user found
$feedback .= ' ERROR - Incorrect User Name Or Email Address ';
return false;
} else {
//create a secure, new password
$new_pass=strtolower(substr(md5(time().$user_name.$hidden_hash_var),1,14));

//update the database to include the new password
$sql="UPDATE user SET password='". md5($new_pass) ."' WHERE user_name='$user_name'";
$result=db_query($sql);

//send a simple email with the new password
mail ($email,'Password Reset','Your Password '.
'has been reset to: '.$new_pass,'From: noreply@company.com');
$feedback .= ' Your new password has been emailed to you. ';
return true;
}
} else {
$feedback .= ' ERROR - User Name and Email Address Are Required ';
return false;
}
}

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);
//change the confirm hash in the db but not the email -
//send out a new confirm email with a new hash
$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_send_confirm_email($email,$hash) {
/*
Used in the initial registration function
as well as the change email address function
*/

$message = "Thank You For Registering at PHPBuilder.com".
"\nSimply follow this link to confirm your registration: ".
"\n\nhttp://www.phpbuilder.com/account/confirm.php?hash=$hash&email=". urlencode($email).
"\n\nOnce you confirm, you can use the services on PHPBuilder.";
mail ($email,'PHPBuilder Registration Confirmation',$message,'From: noreply@phpbuilder.com');
}

function user_register($user_name,$password1,$password2,$email,$real_name) {
global $feedback,$hidden_hash_var;
//all vars present and passwords match?
if ($user_name && $password1 && $password1==$password2 && $email && validate_email($email)) {
//password and name are valid?
if (account_namevalid($user_name) && account_pwvalid($password1)) {
$user_name=strtolower($user_name);
$password1=strtolower($password1);

//does the name exist in the database?
$sql="SELECT * FROM user WHERE user_name='$user_name'";
$result=db_query($sql);
if ($result && db_numrows($result) > 0) {
$feedback .= ' ERROR - USER NAME EXISTS ';
return false;
} else {
//create a new hash to insert into the db and the confirmation email
$hash=md5($email.$hidden_hash_var);
$sql="INSERT INTO user (user_name,real_name,password,email,remote_addr,confirm_hash,is_confirmed) ".
"VALUES ('$user_name','$real_name','". md5($password1) ."','$email','$GLOBALS[REMOTE_ADDR]','$hash','0')";
$result=db_query($sql);
if (!$result) {
$feedback .= ' ERROR - '.db_error();
return false;
} else {
//send the confirm email
user_send_confirm_email($email,$hash);
$feedback .= ' Successfully Registered. You Should Have a Confirmation Email Waiting ';
return true;
}
}
} else {
$feedback .= ' Account Name or Password Invalid ';
return false;
}
} else {
$feedback .= ' ERROR - Must Fill In User Name, Matching Passwords, And Provide Valid Email Address ';
return false;
}
}

function user_getid() {
global $G_USER_RESULT;
//see if we have already fetched this user from the db, if not, fetch it
if (!$G_USER_RESULT) {
$G_USER_RESULT=db_query("SELECT * FROM user WHERE user_name='" . user_getname() . "'");
}
if ($G_USER_RESULT && db_numrows($G_USER_RESULT) > 0) {
return db_result($G_USER_RESULT,0,'user_id');
} else {
return false;
}
}

function user_getrealname() {
global $G_USER_RESULT;
//see if we have already fetched this user from the db, if not, fetch it
if (!$G_USER_RESULT) {
$G_USER_RESULT=db_query("SELECT * FROM user WHERE user_name='" . user_getname() . "'");
}
if ($G_USER_RESULT && db_numrows($G_USER_RESULT) > 0) {
return db_result($G_USER_RESULT,0,'real_name');
} else {
return false;
}
}

function user_getemail() {
global $G_USER_RESULT;
//see if we have already fetched this user from the db, if not, fetch it
if (!$G_USER_RESULT) {
$G_USER_RESULT=db_query("SELECT * FROM user WHERE user_name='" . user_getname() . "'");
}
if ($G_USER_RESULT && db_numrows($G_USER_RESULT) > 0) {
return db_result($G_USER_RESULT,0,'email');
} else {
return false;
}
}

function user_getname() {
if (user_isloggedin()) {
return $GLOBALS['user_name'];
} else {
//look up the user some day when we need it
return ' ERROR - Not Logged In ';
}
}

function account_pwvalid($pw) {
global $feedback;
if (strlen($pw) < 6) {
$feedback .= " Password must be at least 6 characters. ";
return false;
}
return true;
}

function account_namevalid($name) {
global $feedback;
// no spaces
if (strrpos($name,' ') > 0) {
$feedback .= " There cannot be any spaces in the login name. ";
return false;
}

// must have at least one character
if (strspn($name,"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") == 0) {
$feedback .= "There must be at least one character.";
return false;
}

// must contain all legal characters
if (strspn($name,"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_")
!= strlen($name)) {
$feedback .= " Illegal character in name. ";
return false;
}

// min and max length
if (strlen($name) < 5) {
$feedback .= " Name is too short. It must be at least 5 characters. ";
return false;
}
if (strlen($name) > 15) {
$feedback .= "Name is too long. It must be less than 15 characters.";
return false;
}

// illegal names
if (eregi("^((root)|(bin)|(daemon)|(adm)|(lp)|(sync)|(shutdown)|(halt)|(mail)|(news)"
. "|(uucp)|(operator)|(games)|(mysql)|(httpd)|(nobody)|(dummy)"
. "|(www)|(cvs)|(shell)|(ftp)|(irc)|(debian)|(ns)|(download))$",$name)) {
$feedback .= "Name is reserved.";
return 0;
}
if (eregi("^(anoncvs_)",$name)) {
$feedback .= "Name is reserved for CVS.";
return false;
}

return true;
}

function validate_email ($address) {
return (ereg('^-!}

?>
   小试一下身手,大概是没问题了,那么交给你个任务,做个留言本吧,这和HELLO WORLD有一比啊!^_^,同是新手面临的第一道关。
小女巫 该用户已被删除
沙发
发表于 2015-2-4 09:24:28 | 只看该作者
说点我烦的低级错误吧,曾经有次插入mysql的时间 弄了300年结果老报错,其实mysql的时间是有限制的,大概是到203X年  具体的记不清啦,囧。
山那边是海 该用户已被删除
板凳
发表于 2015-2-4 21:03:38 | 只看该作者
这些都是最基本最常用功能,我们这些菜鸟在系统学习后,可以先对这些功能深入研究。
若天明 该用户已被删除
地板
发表于 2015-2-6 00:41:30 | 只看该作者
在学习的过程中不能怕麻烦,不能有懒惰的思想。学习php首先应该搭建一个lamp环境或者是wamp环境。这是学习php开发的根本。虽然网络上有很多集成的环境,安装很方便,使用起来也很稳定、
简单生活 该用户已被删除
5#
发表于 2015-2-11 05:05:34 | 只看该作者
如果你已经到这种程度了,那么你已经可以做我的老师了。其实php也分很多的区域,
分手快乐 该用户已被删除
6#
发表于 2015-2-16 01:24:24 | 只看该作者
个人呢觉得,配wamp 最容易漏的一步就是忘了把$PHP$目录下的libmysql.dll拷贝到windows系统目录的system32目录下,还有重启apache。
只想知道 该用户已被删除
7#
发表于 2015-3-4 21:22:56 | 只看该作者
刚开始安装php的时候,我图了个省事,把php的扩展全都打开啦(就是把php.ini 那一片 extension 前面的冒号全去掉啦),这样自然有好处,以后不用再需要什么功能再来打开。
再见西城 该用户已被删除
8#
发表于 2015-3-8 15:32:55 | 只看该作者
使用 jquery 等js框架的时候,要随时注意浏览器的更新情况,不然很容易发生框架不能使用。
飘灵儿 该用户已被删除
9#
 楼主| 发表于 2015-3-16 03:13:02 | 只看该作者
至于模板嘛,各位高人一直以来就是争论不休,我一只小菜鸟就不加入战团啦,咱们新手还是多学点东西的好。
变相怪杰 该用户已被删除
10#
发表于 2015-3-17 04:07:50 | 只看该作者
兴趣是最好的老师,百度是最好的词典。
不帅 该用户已被删除
11#
发表于 2015-3-20 19:34:50 | 只看该作者
做为1门年轻的语言,php一直很努力。
12#
发表于 2015-4-1 14:12:12 | 只看该作者
你很难利用原理去编写自己的代码。对于php来说,系统的学习我认为还是很重要的,当你有一定理解后,你可你针对某种效果研究,我想那时你不会只是复制代码的水平了。
灵魂腐蚀 该用户已被删除
13#
发表于 2015-4-6 13:07:25 | 只看该作者
最后祝愿,php会给你带来快乐的同时 你也会给他带来快乐。
愤怒的大鸟 该用户已被删除
14#
发表于 2015-4-7 17:28:04 | 只看该作者
环境搭建好,当你看见你的浏览器输出“it works\\\\\\\"时你一定是喜悦的。在你解决问题的时候,我强烈建议多读php手册。
飘飘悠悠 该用户已被删除
15#
发表于 2015-4-13 17:26:22 | 只看该作者
最后祝愿,php会给你带来快乐的同时 你也会给他带来快乐。
兰色精灵 该用户已被删除
16#
发表于 2015-4-15 19:51:37 | 只看该作者
做为1门年轻的语言,php一直很努力。
冷月葬花魂 该用户已被删除
17#
发表于 2015-4-16 19:29:18 | 只看该作者
基础有没有对学习php没有太大区别,关键是兴趣。
莫相离 该用户已被删除
18#
发表于 2015-5-7 19:38:43 | 只看该作者
写js我最烦的就是 ie 和 firefox下同样的代码 结果显示的结果千差万别,还是就是最好不要用遨游去调试,因为有时候遨游是禁用js的,有可能代码是争取结果被遨游折腾的认为是代码写错。
若相依 该用户已被删除
19#
发表于 2015-5-12 12:49:39 | 只看该作者
有位前辈曾经跟我说过,phper 至少要掌握200个函数 编起程序来才能顺畅点,那些不熟悉的函数记不住也要一拿手册就能找到。所以建议新手们没事就看看php的手册(至少array函数和string函数是要记牢的)。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-11-10 21:07

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表