That tutorial - Page 2
Page 2 of 2 FirstFirst 12
Results 16 to 25 of 25

Thread: That tutorial

  1. #16
    Senior Member cluelessPHP's Avatar
    Join Date
    Apr 2015
    Location
    Scotland
    Posts
    429
    config

    PHP Code:
    <?php
        
    class Config{
            public static function 
    get($path null){
                if(
    $path){
                    
    $config =$GLOBALS['config'];
                    
    $path explode ('/'$path);
                    
                    foreach(
    $path as $bit){
                        if(isset(
    $config[$bit])) {
                            
    $config $config[$bit];
                        }
                    }
                    return 
    $config;
                }
                return 
    false;
            }
        }
    validate

    PHP Code:
    <?php
    class Validate{
        private 
    $_passed false,
                
    $_errors =array(),
                
    $_db null;

        public function 
    __construct(){
            
    $this->_db DB::getInstance();
        }

        public function 
    check($source$items = array()){
            foreach(
    $items as $item =>$rules){
                foreach(
    $rules as $rule => $rule_value){

                    
    $value trim($source[$item]);
                    
    $item escape($item);
                    
                    if (
    $rule === 'required' && empty($value)){
                        
    $this->addError("{$item} is required");
                    } else if(!empty(
    $value)){
                        switch(
    $rule){
                            case 
    'min':
                                if(
    strlen($value) < $rule_value){
                                    
    $this->addError("{$item} must be a minimum of {$rule_value} characters.");
                                }
                            break;
                            case 
    'max':
                            if(
    strlen($value) > $rule_value){
                                    
    $this->addError("{$item} must be a maximum of {$rule_value} characters.");
                                }
                            break;
                            case 
    'matches':
                                if(
    $value != $source[$rule_value]){
                                    
    $this->addError("{$rule_value} must match {$item}");
                                }
                            break;
                            case 
    'unique':
                                
    $check $this->_db->get($rule_value, array($item'='$value));
                                if(
    $check->count()){
                                    
    $this->addError("{$item} already take.");
                                }
                            break;
                        }
                    }
                }
            }
            
            if(empty(
    $this->_errors)){
                
    $this->_passed true;
            }
            
            return 
    $this;
        }

        private function 
    addError($error){
            
    $this->_errors[] = $error;
            }
        
        public function 
    errors(){
            return 
    $this->_errors;
            }
            
            public function 
    passed(){
                return 
    $this->_passed;
            }
        }
    Once you had a good excuse, you opened the door to bad excuses ― Terry Pratchett, Thud
    Blog

    Six month project

  2. #17
    Senior Member cluelessPHP's Avatar
    Join Date
    Apr 2015
    Location
    Scotland
    Posts
    429
    user

    PHP Code:
    <?php
        
    class User {
            private 
    $_db,
                    
    $_data,
                    
    $_sessionName,
                    
    $_cookieName,
                    
    $_isLoggedIn;

            public function 
    __construct($user null) {
                
    $this->_db DB::getInstance();

                
    $this->_sessionName Config::get('session/session_name');
                
    $this->_cookieName Config::get('remember/cookie_name');

                if(!
    $user) {
                    if(
    Session::exists($this->_sessionName)) {
                        
    $user Session::get($this->_sessionName);

                        if(
    $this->find($user)) {
                            
    $this->_isLoggedIn true;
                        } else {
                                
    $this->logout();
                        }
                    }
                } else {
                    
    $this->find($user);
                }
            }

            public function 
    update ($fields = array(), $id null) {
                if(!
    $id && $this->isLoggedIn()) {
                    
    $id $this->data()->id;
                }

                if(!
    $this->_db->update('users'$id$fields)) {
                    throw new 
    Exception('There was a problem updating');
                }
            }

            public function 
    create($fields) {
                if(!
    $this->_db->insert('users'$fields)) {
                    throw new 
    Exception('There was a problem creating an account');
                }
            }
            public function 
    find($user null) {
                if(
    $user) {
                    
    $field = (is_numeric($user)) ? 'id' 'username';
                    
    $data $this->_db->get('users', array($field'='$user));

                    if(
    $data->count()) {
                        
    $this->_data $data->first();
                        return 
    true;
                    }
                }
                return 
    false;
            }

            public function 
    login($username null$password null$remember false) {

                if(!
    $username && !$password && $this->exists()) {
                    
    Session::put($this->_sessionName$this->data()->id);
                } else {
                    
    $user $this->find($username);

                    if(
    $user) {
                        if(
    $this->data()->password === Hash::make($password$this->data()->salt)) {
                            
    Session::put($this->_sessionName$this->data()->id);

                            if(
    $remember) {
                                
    $hash Hash::unique();
                                
    $hashCheck $this->_db->get('users_session', array('user_id''='$this->data()->id));

                                if(!
    $hashCheck->count()) {
                                    
    $this->_db->insert('users_session', array(
                                        
    'user_id' => $this->data()->id,
                                        
    'hash' => $hash
                                    
    ));
                                } else {
                                    
    $hash $hashCheck->first()->hash;
                                }

                                
    Cookie::put($this->_cookieName$hashConfig::get('remember/cookie_expiry'));
                            }

                        return 
    true;
                        }
                    }
                }
                return 
    false;
            }

            public function 
    hasPermission($key) {
                
    $group $this->_db->get('groups', array('id''='$this->data()->group));

                if(
    $group->count()) {
                    
    $permissions json_decode($group->first()->permissionstrue);

                    if(
    $permissions[$key] == true) {
                        return 
    true;
                    }
                }
                return 
    false;
            }

            public function 
    exists() {
                return (!empty(
    $this->_data)) ? true false;
            }

            public function 
    logout() {
                
    $this->_db->delete('users_session', array('user_id''='$this->data()->id));
                
    Session::delete($this->_sessionName);
                
    Cookie::delete($this->_cookieName);
            }

            public function 
    data() {
                return 
    $this->_data;
            }

            public function 
    isLoggedIn() {
                return 
    $this->_isLoggedIn;
            }
        }
    ?>
    Once you had a good excuse, you opened the door to bad excuses ― Terry Pratchett, Thud
    Blog

    Six month project

  3. #18
    Senior Member cluelessPHP's Avatar
    Join Date
    Apr 2015
    Location
    Scotland
    Posts
    429
    token

    PHP Code:
    <?php

    class Token {
        public static function 
    generate(){
            return 
    session::put(Config::get('session/token_name'), md5(uniqid()));
        }    

        public static function 
    check($token){
            
    $tokenName Config::get('session/token_name');
            
            if(
    Session::exists($tokenName) && $token === Session::get($tokenName)){
                
    Session::delete($tokenName);
                return 
    true;
            }
        return 
    false;
        }
    }
    session

    PHP Code:
    <?php

    class Session {
        public static function 
    exists($name){
            return(isset(
    $_SESSION[$name])) ? true false;
        }
        public static function 
    put($name$value){
            return 
    $_SESSION[$name] = $value;
        }

        public static function 
    get($name){
            return 
    $_SESSION[$name];
        }
        public static function 
    delete($name){
            if(
    self::exists($name)){
                unset(
    $_SESSION[$name]);
            }
        }

        public static function 
    flash($name$string =''){
            if(
    self::exists($name)){
                
    $session self::get($name);
                
    self::delete($name);
                return 
    $session;
            } else {
                
    self::put($name$string);
            }
        }
    }
    Once you had a good excuse, you opened the door to bad excuses ― Terry Pratchett, Thud
    Blog

    Six month project

  4. #19
    Senior Member cluelessPHP's Avatar
    Join Date
    Apr 2015
    Location
    Scotland
    Posts
    429
    database

    PHP Code:
    -- phpMyAdmin SQL Dump
    -- version 4.2.0
    -- http://www.phpmyadmin.net
    --
    -- 
    Host127.0.0.1
    -- Generation TimeJan 022015 at 01:13 AM
    -- Server version5.6.15-log
    -- PHP Version5.5.8

    SET SQL_MODE 
    "NO_AUTO_VALUE_ON_ZERO";
    SET time_zone "+00:00";


    /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
    /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
    /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
    /*!40101 SET NAMES utf8 */;

    --
    -- 
    Database: `oopdatabase`
    --

    -- --------------------------------------------------------

    --
    -- 
    Table structure for table `groups`
    --

    CREATE TABLE IF NOT EXISTS `groups` (
    `
    idint(11NOT NULL,
      `
    namevarchar(20NOT NULL,
      `
    permissionstext NOT NULL
    ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=;

    --
    -- 
    Dumping data for table `groups`
    --

    INSERT INTO `groups` (`id`, `name`, `permissions`) VALUES
    (1'Standard'''),
    (
    2'Administrator ''{"admin": 1}');

    -- --------------------------------------------------------

    --
    -- 
    Table structure for table `users`
    --

    CREATE TABLE IF NOT EXISTS `users` (
    `
    idint(11NOT NULL,
      `
    usernamevarchar(20NOT NULL,
      `
    passwordvarchar(64NOT NULL,
      `
    saltvarchar(32NOT NULL,
      `
    namevarchar(50NOT NULL,
      `
    joineddatetime NOT NULL,
      `
    groupint(11NOT NULL
    ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=39 ;

    --
    -- 
    Dumping data for table `users`
    --

    INSERT INTO `users` (`id`, `username`, `password`, `salt`, `name`, `joined`, `group`) VALUES
    (38'kevin''ad8977ef959f06b2335dd7b3f983d33d0f4c71cfe5bda9a4046bec0840a37f73''R+ Tza_ ''‰HH}o-Ʒ+*’œ''kevin''2015-01-02 00:56:16'1);

    -- --------------------------------------------------------

    --
    -- 
    Table structure for table `users_sessions`
    --

    CREATE TABLE IF NOT EXISTS `users_sessions` (
    `
    idint(11NOT NULL,
      `
    user_idint(11NOT NULL,
      `
    hashvarchar(50NOT NULL
    ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=;

    --
    -- 
    Indexes for dumped tables
    --

    --
    -- 
    Indexes for table `groups`
    --
    ALTER TABLE `groups`
     
    ADD PRIMARY KEY (`id`);

    --
    -- 
    Indexes for table `users`
    --
    ALTER TABLE `users`
     
    ADD PRIMARY KEY (`id`);

    --
    -- 
    Indexes for table `users_sessions`
    --
    ALTER TABLE `users_sessions`
     
    ADD PRIMARY KEY (`id`);

    --
    -- 
    AUTO_INCREMENT for dumped tables
    --

    --
    -- 
    AUTO_INCREMENT for table `groups`
    --
    ALTER TABLE `groups`
    MODIFY `idint(11NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=3;
    --
    -- 
    AUTO_INCREMENT for table `users`
    --
    ALTER TABLE `users`
    MODIFY `idint(11NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=39;
    --
    -- 
    AUTO_INCREMENT for table `users_sessions`
    --
    ALTER TABLE `users_sessions`
    MODIFY `idint(11NOT NULL AUTO_INCREMENT;
    /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
    /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
    /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */
    Once you had a good excuse, you opened the door to bad excuses ― Terry Pratchett, Thud
    Blog

    Six month project

  5. #20
    Senior Member cluelessPHP's Avatar
    Join Date
    Apr 2015
    Location
    Scotland
    Posts
    429
    That's it all

    Views? comments?
    Once you had a good excuse, you opened the door to bad excuses ― Terry Pratchett, Thud
    Blog

    Six month project

  6. #21
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,843
    Could be a good use case for Github: put all the files there into a repository, then just point to that repo. Then anyone who wants to dig into it could just clone the repo.
    Last edited by NogDog; 09-08-2017 at 10:32 PM.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  7. #22
    Senior Member cluelessPHP's Avatar
    Join Date
    Apr 2015
    Location
    Scotland
    Posts
    429
    Quote Originally Posted by NogDog View Post
    Could be a good use case for Github: put all the files there into a repository, then just point to that repo. Then anyone who wants to dig into it could just clone the repo.
    Yeah but then I couldn't attempt to give Weedpacket brain aneurysm
    Once you had a good excuse, you opened the door to bad excuses ― Terry Pratchett, Thud
    Blog

    Six month project

  8. #23
    Senior Member cluelessPHP's Avatar
    Join Date
    Apr 2015
    Location
    Scotland
    Posts
    429
    Well I did finally remember my github password and added the stuff there too

    https://github.com/cluelessphp/php-tutorial.git
    Once you had a good excuse, you opened the door to bad excuses ― Terry Pratchett, Thud
    Blog

    Six month project

  9. #24
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Contact Unit "Coping Mechanism"
    Posts
    22,505
    Quote Originally Posted by cluelessPHP View Post
    Yeah but then I couldn't attempt to give Weedpacket brain aneurysm
    Speed skims the thread

    "MyISAM? For the session table? Really?"
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  10. #25
    Senior Member cluelessPHP's Avatar
    Join Date
    Apr 2015
    Location
    Scotland
    Posts
    429
    Well I looked back at the tutorial and I guess MyISAM was my bad

    Once you had a good excuse, you opened the door to bad excuses ― Terry Pratchett, Thud
    Blog

    Six month project

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •