当前位置: 动力学知识库 > 问答 > 编程问答 >

how make directory for user when register in php class

问题描述:

Hi i want to make directory for user when they sign up i know the simple way in Mysql but i want it to work with my user class:

right know its work in simple php code like this:

if(!file_exists("user/$username"){

mkdir("user/$username");

}

but i want to add this code to this class:

<?php

//this class is for signup user

class User{

//we need private property for our connection

private $_db,

$_data,

$_sessionName,

$_cookieName,

$_isLoggedIn;

//method for construct new user

public function __construct($user = NULL)

{

//get user connection

$this->_db = DB::getInstance();

//get user session

$this->_sessionName = Config::get('session/session_name');

//get user cookie

$this->_cookieName = Config::get('remember/cookie_name');

//here we check to see if user logged in or not

//so if currect user not

if (!$user) {

//if session exists

if (Session::exists($this->_sessionName)) {

//get user session

$user = Session::get($this->_sessionName);

//and if every thing is correct we loggedin user

if ($this->find($user)) {

$this->_isLoggedIn = true;

}else {

# code...

}

}

}else {

//if user not loggedin we find info of user

$this->find($user);

}

}

//create method for create new user and it has array of fields

public function create($fields = array())

{

//using insert method from class DB($table,$fields)

if (!$this->_db->insert('users',$fields) {

throw new Exception("There was Problem Creating an Account");

}

}

//method for login

public function login($username = NULL,$password=NULL,$remember=false)

{

//chech for is username or password is not defined but we are login as

//remeber me so its login automatic

//here this exists means this user exists

if (!$username && !$password && $this->exists()) {

//so now that we have that user

//we create session automatic so name and value

Session::put($this->_sessionName , $this->data()->id);

}else {

$user = $this->find($username);

if ($user) {

//here we say if password that user insert is = to hash password

//in table

if ($this->data()->password === Hash::make($password,$this->data()->salt))

{ Session::put($this->_sessionName,$this->data()->id);

//checking for remeber me functionlaity

if ($remember) {

//first we take uniqid from Hash class

$hash = Hash::unique();

//we get data from user_session table ,

//and set user_id to id of currect user

$hashCheck = $this->_db->get('users_session',

array('user_id','=',$this->data()->id));

//now if inside our users_session hashCheck has no value

if (!$hashCheck->count()) {

//we insert

$this->_db->insert('users_session',array(

'user_id' => $this->data()->id ,

'hash' => $hash

));

}else {

//else we get first hash from user_session table

$hash = $hashCheck->first()->hash;

}

Cookie::put($this->_cookieName,$hash,

Config::get('remember/cookie_expiry'));

}

return true;

}

}

return false;

}

}

//method for find user and the reason we say user is

//because we want to find user not by only username but also by ID

public function find($user = NULL)

{

if ($user) {

//now if it was numeric we find it by id otherwise by username

$field = (is_numeric($user))? 'id' : 'username';

//declare data which is data from column of user table

$data = $this->_db->get('users',array($field,'=',$user));

if ($data->count()) {

$this->_data = $data->first();

return true;

}

}

return false;

}

//method for logout user

public function logout()

{

//for remove hash from DB

$this->_db->delete('users_session',array(

'user_id', '=' , $this->data()->id

));

//delete session

Session::delete($this->_sessionName);

//delete cookie for remeber

Cookie::delete($this->_cookieName);

}

//method for update user name

public function update($fields = array(),$id = NULL)

{

//here we declare if id is not declare and we are loggedin

if (!$id && $this->isLoggedIn()) {

//we get id

$id = $this->data()->id;

}

//we use update method from DB class

if (!$this->_db->update('users',$id,$fields)) {

throw new Exception("there was problem updating");

}

}

//method for cehck permission to see if we user admin or simple user

// public function hasPermission($key)

// {

// //first we check to see id of group

// $group = $this->_db->get('groups',array(

// 'id','=',$this->data()->group

// ));

// //then if we have any data

// if ($group->count()) {

// //we decode json permissions becuase our data is json and we

// //get first permissions

// $permissions = json_decode($group->first()->permissions,true);

// //then if our key is true we return true

// if ($permissions[$key] == true) {

// return true;

// }

// }

// return false;

// }

//method for return data property from users

public function data()

{

return $this->_data;

}

//method for return isLoggedIn property

public function isLoggedIn()

{

return $this->_isLoggedIn;

}

//method for check if data is exists

public function exists()

{

return (!empty($this->_data)) ? true : false;

}

}

?>

i'm sorry if its too long but i could find answer here so i prefer to post it by my self

分享给朋友:
您可能感兴趣的文章:
随机阅读: