复制代码 代码如下
分类:美高梅网上注册平台

总共需两个页面,register.php 和 verify.php

所以,把第一章,可重用类的代码贴出来,便于以后查阅以及供给有需要的朋友。 :User类,包括读取和设置数据库,以及保存更改交互 复制代码 代码如下: uid=null; $this->fields=array('username'=>'','password'=>'','emailAddr'=>'','isActive'=>false); } public function __get{ if{ return $this->uid; }else{ return $this->fields[$field]; } } public function __set{ if(array_key_exists){ $this->fields[$field]=$value; } } //return if username is valid format public static function validateUsername{ return preg_match('/^[A-Z0-9]{2,20}$/i',$username); } //return if email address is valid format public static function validateEmailAddr{ return filter_var($email,FILTER_VALIDATE_EMAIL); } //return an object populated based on the record‘s user id public static function getById{ $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{ $row=mysql_fetch_assoc; $user->username=$row['USERNAME']; $user->password=$row['PASSWORD']; $user->emailAddr=$row['EMAIL_ADDR']; $user->isActive=$row['IS_ACTIVE']; ChromePhp::log; $user->uid=$user_id; } mysql_free_result; return $user; } //return an object populated based on the record's username public static function getByUsername{ $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{ $row=mysql_fetch_assoc; $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; return $user; } //save the record to the database public function save(){ //update existing user's information if{ $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 ', 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; 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; $query=sprintf('INSERT INTO %sPENDING ' . 'VALUES ',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{ $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{ mysql_free_result; return false; }else{ mysql_free_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(); } } } } ?> 如何使用: 复制代码 代码如下: username='jack'; $u->password=sha1; $u->emailAddr='zjczoo@gmail.com'; $u->save();//save this user ?> 复制代码 代码如下: password=sha1; $u->save();//save new jack ?> :验证码类:这个比较简单,你可以自己加个图片== 复制代码 代码如下: 另外,该类用到了random_text()函数,代码如下: 复制代码 代码如下: 连接数据库类: 复制代码 代码如下: 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 NOT NULL, PASSWORD CHAR NOT NULL, EMAIL_ADDR VARCHAR NOT NULL, IS_ACTIVE TINYINT DEFAULT 0, PRIMARY KEY 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 NOT NULL, CREATED_DATE TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY REFERENCES WROX_USER ENGINE=MyISAM DEFAULT CHARACTER SET gb2312 COLLATE gb2312_chinese_ci;

  1. 用户注册表格 register.php

美高梅网上注册平台,复制代码 代码如下:

  1. 创建用户数据表格 Users复制代码 代码如下:CREATE TABLE IF NOT EXISTS `users` NOT NULL auto_increment,

`status` varchar NOT NULL,

`username` varchar NOT NULL,

`password` varchar NOT NULL,

`email` varchar NOT NULL,

`activationkey` varchar NOT NULL,

PRIMARY KEY ,

UNIQUE KEY `username` ,

UNIQUE KEY `email` ,

UNIQUE KEY `activationkey`

) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;

  1. 创建验证码 用户注册信息存入数据表我们使用状态‘verify' 来表示尚未激活的用户。

复制代码 代码如下:$activationKey = mt_rand . mt_rand . mt_rand();

$username = mysql_real_escape_string;

$password = mysql_real_escape_string;

$email = mysql_real_escape_string;

$sql="INSERT INTO users (username, password, email, activationkey, status) VALUES ('$username', '$password', '$email', '$activationKey', 'verify')";

  1. 发送验证码复制代码 代码如下:echo "An email has been sent to $_POST[email] with an activation key. Please check your mail to complete registration.";

##Send activation Email

本文由美高梅网上注册平台发布于美高梅网上注册平台,转载请注明出处:复制代码 代码如下

上一篇:首先建立数据库cake,除上诉字段数据库与上例一 下一篇:没有了
猜你喜欢
热门排行
精彩图文