So, put the first chapter, reusable class of code to post, easy to look at later and provide the necessary friends.
: User class, including reading and setting databases, and saving change interactions
Copy Code code as follows:
<?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 a 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 a 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 a 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 ();
}
}
}
}
?>
How to use:
Copy Code code as follows:
<?php
Create User Instance
$u =new User ();
$u->username= ' Jack ';
$u->password=sha1 (' Gogo ');
$u->emailaddr= ' zjczoo@gmail.com ';
$u->save ();//save this user
?>
Copy Code code as follows:
<?php
$u =user::getbyusername (' Jack ');//update User (' Jack ')
$u->password=sha1 (' Newgogo ');
$u->save ()//save New Jack
?>
: Verification code class: This is relatively simple, you can add a picture = =
Copy Code code as follows:
<?php
Must start or continue session and save CAPTCHA string in $_session for
It to being available to the 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);
?>
In addition, the class uses the Random_text () function with the following code:
Copy Code code as follows:
<?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;
}
?>
To connect to a database class:
Copy Code code as follows:
<?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 statement:
Copy Code code as follows:
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 () is not NULL,
PASSWORD CHAR NOT NULL,
Email_addr VARCHAR (m) not NULL,
Is_active TINYINT (1) DEFAULT 0,
PRIM ARY 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 (a) 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;