当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP学习笔记 用户注册模块用户类以及验证码类

PHP学习笔记 用户注册模块用户类以及验证码类

2019年04月19日  | 移动技术网IT编程  | 我要评论
所以,把第一章,可重用类的代码贴出来,便于以后查阅以及供给有需要的朋友。
:user类,包括读取和设置数据库,以及保存更改交互
复制代码 代码如下:

<?php
class user{
private $uid;
private $fields;
public function __construct(){
$this->uid=null;
$this->fields=array('username'=>'','password'=>'','emailaddr'=>'','isactive'=>false);
}
public function __get($field){
if($field=='userid'){
return $this->uid;
}else{
return $this->fields[$field];
}
}
public function __set($field,$value){
if(array_key_exists($field,$this->fields)){
$this->fields[$field]=$value;
}
}
//return if username is valid format
public static function validateusername($username){
return preg_match('/^[a-z0-9]{2,20}$/i',$username);
}
//return if email address is valid format
public static function validateemailaddr($email){
return filter_var($email,filter_validate_email);
}
//return an object populated based on the record‘s user id
public static function getbyid($user_id){
$user=new user();
$query=sprintf('select username,password,email_addr,is_active '.
'from %suser where user_id=%d',db_tbl_prefix,$user_id);
$result=mysql_query($query,$globals['db']);
if(mysql_num_rows($result)){
$row=mysql_fetch_assoc($result);
$user->username=$row['username'];
$user->password=$row['password'];
$user->emailaddr=$row['email_addr'];
$user->isactive=$row['is_active'];
chromephp::log($user_id);
$user->uid=$user_id;
}
mysql_free_result($result);
return $user;
}
//return an object populated based on the record's username
public static function getbyusername($username){
$user=new user();
$query=sprintf('select user_id,password,email_addr,is_active '.
'from %suser where username="%s"',db_tbl_prefix,mysql_real_escape_string($username,$globals['db']));
$result=mysql_query($query,$globals['db']);
if(mysql_num_rows($result)){
$row=mysql_fetch_assoc($result);
$user->username=$username;
$user->password=$row['password'];
$user->emailaddr=$row['email_addr'];
$user->isactive=$row['is_active'];
$user->uid=$row['user_id'];
}
mysql_free_result($result);
return $user;
}
//save the record to the database
public function save(){
//update existing user's information
if($this->uid){
$query = sprintf('update %suser set username = "%s", ' .
'password = "%s", email_addr = "%s", is_active = %d ' .
'where user_id = %d',
db_tbl_prefix,
mysql_real_escape_string($this->username, $globals['db']),
mysql_real_escape_string($this->password, $globals['db']),
mysql_real_escape_string($this->emailaddr, $globals['db']),
$this->isactive,
$this->userid);
return mysql_query($query, $globals['db']);
}else{
//create a new user
$query=sprintf('insert into %suser(username,password,' .
'email_addr,is_active) values ("%s","%s","%s",%d)',
db_tbl_prefix,
mysql_real_escape_string($this->username,$globals['db']),
mysql_real_escape_string($this->password,$globals['db']),
mysql_real_escape_string($this->emailaddr,$globals['db']),
$this->isactive);
if(mysql_query($query,$globals['db'])){
$this->uid=mysql_insert_id($globals['db']);
return true;
}else{
return false;
}
}
}
//set the record as inactive and return an activation token
public function setinactive(){
$this->isactive=false;
$this->save();
$token=random_text(5);
$query=sprintf('insert into %spending (user_id,token)' .
'values (%d,"%s")',db_tbl_prefix,$this->uid,$token);
return (mysql_query($query,$globals['db']))?$token:false;
}
//clear the user's pending status and set the record as active
public function setactive($token){
$query=sprintf('select token from %spending where user_id=%d ' .
'and token="%s"',db_tbl_prefix,$this->uid,mysql_real_escape_string($token,$globals['db']));
$result=mysql_query($query,$globals['db']);
if(!mysql_num_rows(($result))){
mysql_free_result($result);
return false;
}else{
mysql_free_result($result);
$query=sprintf('delete from %spending where user_id=%d ' .
'and token="%s"',db_tbl_prefix,$this->uid,mysql_real_escape_string($token,$globals['db']));
if(!mysql_query($query,$globals['db'])){
return false;
}else{
$this->isactive=true;
return $this->save();
}
}
}
}
?>

如何使用:
复制代码 代码如下:

<?php
//create user instance
$u=new user();
$u->username='jack';
$u->password=sha1('gogo');
$u->emailaddr='zjczoo@gmail.com';
$u->save();//save this user
?>

复制代码 代码如下:

<?php
$u=user::getbyusername('jack');//update user('jack')
$u->password=sha1('newgogo');
$u->save();//save new jack
?>

:验证码类:这个比较简单,你可以自己加个图片==
复制代码 代码如下:

<?php
//must start or continue session and save captcha string in $_session for
//it to be available to other requests
if(!isset($_session)){
session_start();
header('cache-control:private');
}
//create a 65*20 pixel image
$width=65;
$height=20;
$image=imagecreate(65,20);
//fill the image background color
$bg_color=imagecolorallocate($image,0x33,0x66,0xff);
imagefilledrectangle($image,0,0,$width,$height,$bg_color);
//fetch random text
$text=random_text(5);
//determine x and y coordinates for centering text
$font=5;
$x=imagesx($image)/2-strlen($text)*imagefontwidth($font)/2;
$y=imagesy($image)/2-imagefontheight($font)/2;
//write text on image
$fg_color=imagecolorallocate($image,0xff,0xff,0xff);
imagestring($image,$font,$x,$y,$text,$fg_color);
//save the captcha string for later comparison
$_session['captcha']=$text;
//output the image
header('content-type:image/png');
imagepng($image);
imagedestroy($image);
?>

另外,该类用到了random_text()函数,代码如下:
复制代码 代码如下:

<?php
function random_text($count,$rm_similar=false){
$chars=array_flip(array_merge(range(0,9),range('a','z')));
if($rm_similar){
unset($chars[0],$chars[1],$chars[2],$chars[5],$chars[8],$chars['b'],$chars['i'],$chars['o'],$chars['q'],$chars['s'],$chars['v'],$chars['z']);
}
for($i=0,$text='';$i<$count;$i++){
$text.=array_rand($chars);
}
return $text;
}
?>

连接数据库类:
复制代码 代码如下:

<?php
// database connection and schema constants
define('db_host', 'localhost');
define('db_user', 'username');
define('db_password', 'yourpassword');
define('db_schema', 'wrox_database');
define('db_tbl_prefix', 'wrox_');
// establish a connection to the database server
if (!$globals['db'] = mysql_connect(db_host, db_user, db_password))
{
die('error: unable to connect to database server.');
}
if (!mysql_select_db(db_schema, $globals['db']))
{
mysql_close($globals['db']);
die('error: unable to select database schema.');
}
?>

sql语句:
复制代码 代码如下:

drop table if exists wrox_pending;
drop table if exists wrox_user;
create table wrox_user (
user_id integer unsigned not null auto_increment,
username varchar(20) not null,
password char(40) not null,
email_addr varchar(100) not null,
is_active tinyint(1) default 0,
primary key (user_id)
)
engine=myisam default character set gb2312
collate gb2312_chinese_ci auto_increment=0;
create table wrox_pending (
user_id integer unsigned primary key not null,
token char(10) not null,
created_date timestamp default current_timestamp,
foreign key (user_id)
references wrox_user(user_id)
)
engine=myisam default character set gb2312
collate gb2312_chinese_ci;

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网