Maison >développement back-end >tutoriel php > PHP 用户登录范例
PHP 用户登录实例
create database PHPDemo01 use PHPDemo01 create table worx_user( user_id integer unsigned not null auto_increment, username varchar(20) not null, password char(40) not null, email_addr varchar(100), is_acvtive tinyint(1) default 0, primary key(user_id) ) engine=myisam default character set latin1 collate latin1_general_cs auto_increment=0; create table worx_pending( user_id integer unsigned not null, token char(10) not null, create_date timestamp default current_timestamp, foreign key(user_id) references worx_user(user_id) ) engine=myisam default character set latin1 collate latin1_general_cs;?
<?php define('IS_ENV_PRODUCTION',true); error_reporting(E_ALL | E_STRICT); ini_set('display_errors', !IS_ENV_PRODUCTION); ini_set('error_log' ,'log/phperror.txt'); date_default_timezone_set('America/New_York'); if(get_magic_quotes_gpc()) { function _stripslashes_rcurs($variable,$top = true) { $clean_data = array(); foreach($variable as $key => $value) { $key = ($top)?$key : stripslashes($key); $clean_data[$key] = (is_array($value))? stripslashes_rcurs($value,false) : stripslashes($value); } return $clean_data; } $_GET = _stripslashes_rcurs($_GET); $_POST = _stripslashes_rcurs($_POST); } ?>?
<?php define('DB_HOST', 'localhost'); define('DB_USER', 'root'); define('DB_PASSWORD', 'chenhailong'); define('DB_SCHEMA', 'PHPDemo01'); define('DB_TEL_PERFIX', 'WORX_'); 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($CLOBALS['DB']); die('Error :Unable to select database schema.'); } echo 'chenhailong ' ?>?
<?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['U'],$chars['V'],$chars['Z']); } for($i = 0 , $text = '';$i<$count;$i++) { $text = array_rand($chars); } return $text; } ?>?
<?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; } } public static function validateUserName($username) { return preg_match('/^[A-Z0-9]{2,20}$/i', $username); } public static function validateEmailAddr($email) { return filter_var($email, FILTER_VALIDATE_EMAIL); } 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_TEL_PERFIX, $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']; $user->uid = $user_id; } mysql_free_result($result); return $user; } public static function getByUsername($username) { $user = new User(); $query = sprintf('select user_id,password,email_addr,is_active' . 'from %sUser where username = "$s"', DB_TEL_PERFIX, 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 = $row['USERNAME']; $user->password = $row['PASSWORD']; $user->emailAddr = $row['EMAIL_ADDR']; $user->isactive = $row['IS_ACTIVE']; $user->uid = $user_id; } mysql_free_result($result); return $user; } public function save() { if ($this->uid) { $query = sprintf('update %sUser set username="%s",password="%s",email_addr="%s",is_active=%d' . 'where user_id = %d', DB_TEL_PERFIX, 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 { $query = sprintf('insert into %sUser (username,password,email_addr,is_active) values ("%s","%s","%s","%d")', DB_TEL_PERFIX, 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; } } } public function setInactive() { $this->isActive = false; $this->save(); $token = random_text(5); $query = sprintf('insert into %sPending (user_id,token) values (%d,"%s")', DB_TEL_PERFIX, $this - uid, $token); return (mysql_query($query, $GLOBALS['DB'])) ? $token : false; } public function setActive($token) { $query = sprintf('select token from %sPending where user_id = %d'. 'And token ="%s"',DB_TEL_PERFIX,$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); } } } ?>?