Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class DB {
- protected $pdo;
- protected $engine = 'mysql';
- protected $host = 'localhost';
- protected $db_name = 'test';
- protected $db_user = 'root';
- protected $db_passwd = '';
- public function connect() {
- try {
- $this->pdo = new PDO($this->engine.':host='.$this->host.';dbname='.$this->db_name,$this->db_user,$this->db_passwd);
- $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- return $this->pdo;
- } catch(PDOException $e) {
- die($e->getMessage());
- }
- }
- }
- class user {
- public $link;
- public function __construct() {
- $pdoObj = new DB();
- $this->link = $pdoObj->connect();
- return $this->link;
- }
- }
- $obj = new user();
- $obj->SelectAll();
- // and so on..
- public function register($username, $email, $passwd, $re_passwd) {
- # fix email and passwd regex
- $reg_error = array();
- $regex_username = '/[a-zA-Z0-9-._]{6,20}$/';
- $regex_email = '/[a-zA-Z0-9._-]{3,}@[a-zA-Z0-9]{3,}.[a-zA-Z.]{2,}$/';
- if (empty($username) || empty($email) || empty($passwd) || empty($re_passwd)) {
- $reg_error['fields'] = 'All fields are required';
- } else {
- if (!preg_match($regex_username, $username)) {
- $reg_error['username'] = 'Username must be at least 6 characters';
- } if (!preg_match($regex_email, $email)) {
- $reg_error['email'] = 'Invalid email';
- } if(strlen($passwd) <= 6) {
- $reg_error['passwd'] = 'Password must be at least 6 characters long and must contain at least 1 uppercase, 1 lowercase and 1 digit value';
- } if ($re_passwd !== $passwd) {
- $reg_error['match'] = 'Passwords do not match';
- } if($this->usernameExists($username)) {
- $reg_error['username_exists'] = 'Username: ' .$username. ' already exists';
- } if($this->emailExists($email)) {
- $reg_error['email_exists'] = 'Email: ' .$email. ' already exists in our database';
- }
- }
- if (!empty($reg_error)) {
- foreach($reg_error as $error => $er) {
- echo 'Error: '.$er.'<br />';
- }
- } else {
- try {
- $query = $this->link->prepare("INSERT INTO users(username, email, password, ip_address, activation_code)
- VALUES(:username, :email, :passwd, :ip_addr, :code)");
- $passwd = hash('sha512', $passwd);
- $ip_addr = $_SERVER['REMOTE_ADDR'];
- $code = substr( hash('sha512', md5(rand(999, 9999))) , 15, 40);
- $params = array(':username' => $username,
- ':email' => $email,
- ':passwd' => $passwd,
- ':ip_addr' => $ip_addr,
- ':code' => $code);
- $query->execute($params);
- if ($query->rowCount() != 0) {
- redirect('index.php?lastid='.$this->lastId());
- } else {
- return 'There's been a tehnical problem during the registration process, please try again later.';
- }
- } catch (PDOException $e) {
- die($e->getMessage());
- }
- }
- }
- engine = "mysql"
- host = "localhost"
- name = "test"
- user = "root"
- password = ""
- class DB {
- private $pdo;
- private $engine;
- private $host;
- private $db_name;
- private $db_user;
- private $db_passwd;
- public function _construct($path) {
- if(!is_file($path)) {
- throw new InvalidArgumentException("Hey, that's not a file!");
- }
- $credentials = @parse_ini_file($path);
- if(empty($credentials)) {
- throw new InvalidArgumentException("Hey, that's a file, but it's not a valid INI file!");
- }
- $this->engine = $credentials["engine"];
- $this->host = $credentials["host"];
- $this->db_name = $credentials["name"];
- $this->db_user = $credentials["user"];
- $this->db_passwd = $credentials["password"];
- try {
- $this->pdo = new PDO($this->engine.':host='.$this->host.';dbname='.$this->db_name,$this->db_user,$this->db_passwd);
- $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- return $this->pdo;
- } catch(PDOException $e) {
- throw new Exception("Boo, wrong credentials!");
- }
- }
- }
- $db = new DB("database.ini");
- $this->engine = $credentials["engine"];
- $this->host = $credentials["host"];
- $this->db_name = $credentials["name"];
- $this->db_user = $credentials["user"];
- $this->db_passwd = $credentials["password"];
- } catch(PDOException $e) {
- die($e->getMessage());
- }
- } catch(PDOException $e) {
- throw new Exception("Boo, wrong credentials!");
- }
- public function __construct() {
- $pdoObj = new DB();
- $this->link = $pdoObj->connect();
- return $this->link;
- }
- $query = $this->link->prepare("INSERT INTO users(username, email, password, ip_address, activation_code)
- VALUES(:username, :email, :passwd, :ip_addr, :code)");
- class DB {
- ...
- public function getPDO() {
- return $this->pdo;
- }
- }
- public function __construct() {
- $database = new DB("database.ini");
- $this->link = $database->getPDO();
- }
- $database = new DB("database.ini");
- public function __construct(DB $database) {
- $this->link = $database->getPDO();
- }
- $database = new DB("database.ini");
- $user = new User($database);
- $database1 = new DB("database1.ini");
- $user1 = new User($database1);
- $database2 = new DB("database2.ini");
- $user2 = new User($database2);
- class Table {
- protected $database;
- public function __construct(DB $database) {
- $this->database = $database->getPDO();
- }
- }
- class User extends Table {
- // only user specific functions here
- }
- class Article extends Table {
- // only article specific functions here
- }
- class Comment extends Table {
- // only comment specific functions here
- }
- class User {
- public function register($username, $email, $passwd, $re_passwd) {
- $link = $this->database->getPDO();
- ...
- $query = $link->prepare( ... );
- ...
- }
- }
Add Comment
Please, Sign In to add comment