PHP
  Home arrow PHP arrow Page 4 - Building a Site Engine Using PHP, Part 3
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Building a Site Engine Using PHP, Part 3
By: James Murray
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 48
    2004-06-28


    Table of Contents:
  • Building a Site Engine Using PHP, Part 3
  • Wanna Go Out on a Data?
  • Stop Blocking Me
  • Insert Username Here

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Building a Site Engine Using PHP, Part 3 - Insert Username Here
    ( Page 4 of 4 )

    The user authentication is relatively easy to work with -- easier than the blocks. The most important thing about the user authentication is the security behind the login. The password is the most vulnerable piece of information because that’s what everyone wants to get too. Setting cookies can be a security risk, if you don’t use them correctly. With the following authentication methods, you’ll be able to set a cookie that is secure, and on top of that you can use this authentication for every site that you host with the site engine not the mention you make one array that you can use anywhere in the engine to get information about the user. So let's get started.

    First we need to define our class and the variables we’ll be using. $usr will hold all the information about a user in an array. $username will hold the username passed to the class by the login form, and $userpass will, of course, hold the user’s password that is passed to the script by the login form.

    class user{
       
        public $user;

        public $username;
        public $userpass;

    Unlike the other classes in the site engine, we want to name the actual login function something different than the class name because we don’t want the login function to be called when the class is called. So, I named it __check_login().n Also set up the global variables we’ll be using and set up the password to match the md5’s password that’s stored in the database. MD5 isn’t really an encoding, rather it’s the calculated hash of the string that’s passed to it.

         function __check_login(){
            global $sql,$site;

      $pass = md5($this->userpass)

    Now get all our user's information from the database. Put the query function into the fetch_row() function to kill two birds with one stone. After the query has been run, check to make sure that the data returned is an array -- as it should be if it was a successful login. Then set the information in the $user with the corresponding information that we got from the database.
     
            $cl=$sql->_fetch_row($sql->_query("SELECT `users`.`user_ID`,`users`.`user_name`,`users`.`user_pass`,`users`.
    `site_ID`FROM`users`WHERE (`users`.`site_ID` = '{$site->site['ID']}') AND (`users`.`user_name` = '$this->username') AND (`users`.`user_pass` = '$pass') limit 1"));
            if(is_array($cl)){
                $this->user['ID'] = $cl[0];
                $this->user['name'] = $cl[1];
                $this->user['pass'] = $cl[2];
                $this->user['site'] = $cl[3];

    In comes the group users table in the database; we wouldn’t be able to do the next step with out it. After the user’s information is pulled from the users table in the database successfully, run the following query to get the information about what authentication groups the user belongs too. Since all users belong to at least two user groups, the result would be an array, so once again use the fetch_object function to get the results, only this time put it in a while loop. Then during the while loop, set the values to the user[‘gids’] array.

                $gids=$sql->_query("SELECT `groups`.`group_ID `FROM`group_users`,`groups`WHERE(`groups`.`group_ID` = `group_users`.`group_ID`) AND (`group_users`.`user_ID` = '{$this->user['ID']}') AND (`group_users`.`site_ID` = '{$this->usr['site']}')ORDER BY`groups`.`group_ID` DESC");
                while($gid=$sql->_fetch_row($gids)){
                    $this->user['gids'][] = $gid[0];
                }

    After the while loop, check to see if the user[‘gids’] has any values in it, if it doesn’t, assign the group ID’s for a visitor, manually.

                if(!isset($this->user['gids'])){
                    $this->user['gids'][] = "1";
                    $this->user['gids'][] = "2";
                }

    Finally after the group ID’s are taken care of, end the if statement that we started when checking to see if the results from selecting the users information, with an else so that way, if the user’s login wasn’t successful, manually apply the information for a visitor to the user array. This doesn’t bar the user from trying to login again; it just sets the user array to what I like to think of as “default settings”.     

            }else{
                $this->user['ID'] = 0;
                $this->user['name'] = "Guest";
                $this->user['site'] = $site->site['ID'];
                $this->user['gids'][] = "1";
                $this->user['gids'][] = "2";
            }
        }

    }

    Final Thoughts

    If you thought the blocks plug-in was confusing, just keep looking at it and thinking of it line by line. Follow the variables and you’ll understand it really fast. It’s quite logical in the way it works, and it teaches a lot about arrays and how they work. The authentication plug-in also shows a few nice ways to work with and utilize arrays in your everyday scripts.

    The file system can be changed to your likings -- just remember if you change it, that you’ll have to update the plug-ins and module systems, and the blocks plug-in. If you have any confusion when working with the file system, it’s always a good idea to make a test plug-in, a test module, and a test block. That way you can follow it by replacing the variables with the name of your test files to be sure of where the file should be.
     
    In the next article, I’ll be telling you about the template system and how to get your actual engine up and running in different environments. In the fifth and final article I can walk you through making a small site on your site engine, and hopefully by then you’ll be able to see fully what it does, how it does it, and why it does those things. By then you should be able the expand it to fit your needs.



     
     
    >>> More PHP Articles          >>> More By James Murray
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek