通用用户管理类

通用用户管理类

<?php
/**
 +----------------------------------------------------------
 * 兄 弟 你 该 努 力 了!- 西 木
 +----------------------------------------------------------
 * 用 途 管理员模型
 +----------------------------------------------------------
 * 文 件 名  Admin.class.php
 +----------------------------------------------------------
 * 时 间 2010-12-29
 +----------------------------------------------------------
 */
class M_admin extends Model{

    //默认用户表名
	private $table_name="#@__admin";

    //默认id字段名称
	private $table_user_uid="id";

    //默认用户名字段名称
	private $table_user_name="name";

    //默认密码字段名称
	private $table_user_password="password";

    //默认session加密字符截
	private $session_str="good";

    //默认登录超时时间
	private $outtime='3600';
	/**
	 * 构造函数
	 */
	function __construct(){

		parent::__construct();//使用父类构造函数

		session_start();//启用session
	}
	/**
	 * 访问器
	 */
	function __get($name){
		return $this->$name;
	}
	/**
	 * 设置器
	 */
	function __set($name,$value)
	{
		$this->$name=$value;
	}
	/**
	 * 设置会员表信息(表名,ID字段,用户名字段名,密码字段名,session附加字符,登录超时时间)
	 */
	public function config($table_name,$table_user_uid,$table_user_name,$table_user_password,$session_str,$outtime){

		$this->table_name=$table_name;

		$this->table_user_uid=$table_user_uid;

		$this->table_user_name=$table_user_name;

		$this->table_user_password=$table_user_password;

		$this->session_str=$outtime;

		$this->outtime=$outtime;
	}
    /**
     * 登录函数,成功返回true
     */
    public function user_login($user_name,$user_password){

    	$user_name=str_replace(" ","",$user_name);//删除字符串中的空格

    	$user_name=$this->checksql($user_name);

    	$user_password=$this->checksql($user_password);//防止登录sql注入

    	$user_password=md5($user_password);//md5加密

    	$this->select("$this->table_name","$this->table_user_name='$user_name' and $this->table_user_password='$user_password'");

    	if($this->rows()){

    		is_array($row=$this->fetch_array());

    		$_SESSION[uid]=$row[$this->table_user_uid];

    		$_SESSION[shell]=md5($user_password.$this->session_str);

            $_SESSION[outtime]=mktime();

    		return true;
    	}
    	else{

    		session_destroy();

    		return false;
    	}
    }
    /**
     * 按照条件返回用户
     * @param string condition
     * @return rs
     */
    public function user_get($condition=''){

        if(empty($condition)){

        	return $this->findall("$this->table_name");
        }
        else{

        	return $this->select("$this->table_name",$condition);
        }
    }
    /**
     * 添加管理员,成功返回true
     * (字段数组,数据数组)
     */
    public function user_add($user){

    	foreach($user as $key=>$value){

    		if($key==$this->table_user_name){$user_name=$value;}

    		if($key==$this->table_user_password){$user_password=$value;continue;}

    	}
    	if($this->check_name($user_name)){

    	  if($user[$this->table_user_password]=md5($user_password)){

    	      if($this->insert_array("$this->table_name",$user))

    	          return true;

    	       else

    	          return false;//插入失败
    	   }
    	   else{

    		   return false;//md5加密失败
    	   }
    	}
    	else{

    		return false;//用户名重复
    	}
    }
    /**
     * 检测用户名是否存在,存在返回假
     */
    public function check_name($user_name){

       $this->select("$this->table_name","$this->table_user_name='$user_name'");

       if($this->rows()){

       	  return false;
       }
       else{

       	  return true;
       }
    }
    /**
     *在线状态,返回在用户名,不存在返回false
     */
    public function check_login(){

       $this->select("$this->table_name","$this->table_user_uid='$_SESSION[uid]'");

       if($this->rows() and is_array($row=$this->fetch_array())){

       	 if($_SESSION[shell]==md5($row[$this->table_user_password].$this->session_str))

       	     return $this->check_outtime()?$row[$this->table_user_name]:false;

       	 else

       	     return false;
       }
       else{

       	 return false;
       }
    }
   /**
    * 按照用户名查找用户信息,返回一维数组,查询失败返回falsh
    */
   public function get_user_byname($user_name){

   	   $this->select("$this->table_name","$this->table_user_name='$user_name'");

   	   if($this->rows()){

   	   	return is_array($row=$this->fetch_array())?$row:false;

   	   }
   	   else{

   	   	 return false;
   	   }
   }
   /**
    * 按照用户名ID查找用户信息,返回一维数组,查询失败返回falsh
    */
   public function get_user_byid($uid){

   	   $this->select("$this->table_name","$this->table_user_uid='$uid'");

   	   if($this->rows()){

   	   	 return $this->fetch_array();

   	   }
   	   else{

   	   	 return false;
   	   }
   }

   /**
    * 按照用户名修改用户信息,成功返回true
    * (用户名,数据库字段(数组),数据(数组))
    */
   public function user_update_byname($user_name){

   	  $this->update();
   }
   /**按照id修改用户
    * @param int id
    * @param array
    * @return int rows
    */
   public function user_update_byid($id,$array){

   	 $condition="`".$this->table_user_uid."`=".$id;

     if (!empty($array[$this->table_user_password])){

     	 $array[$this->table_user_password]=md5($array[$this->table_user_password]);//密码加密
     }

   	 return  $this->update_array("$this->table_name",$array,$condition);

   }
   /**
    * 按照用户ID删除用户,成功返回true
    */
   public function user_delete_byid($uid){

   	  if($this->delete("$this->table_name","$this->table_user_uid='$uid'")){

   	  	return true;
   	  }
   	  else{

   	  	return false;
   	  }
   }
   /**
    * 按照用户名删除用户,成功返回true
    */
   public function user_delete_byname($user_name){

   	  if($this->delete("$this->table_name","$this->table_user_name='$user_name'")){

   	  	return true;
   	  }
   	  else{

   	  	return false;
   	  }
   }
   /**
    * 检测登录超时,未超时返回true
    */
   public function check_outtime(){

   	 $newtime=mktime();

   	 $onletime=$_SESSION[outtime];

   	 if($newtime-$onletime>$this->outtime){

   	 	return false;
   	 }
   	 else{

   	 	$_SESSION[outtime]=mktime();

   	 	return true;
   	 }
   }
    /**
     * 用户登出成功返回true
     */
    public function user_exit(){

    	if(session_destroy())

    	   return true;

    	else

    	   return false;
    }
}

posted on 2011-01-12 15:19  justup  阅读(353)  评论(0)    收藏  举报

导航