PHP
  Home arrow PHP arrow Page 2 - Building a Site Engine with PHP, Part 2
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? 
PHP

Building a Site Engine with PHP, Part 2
By: James Murray
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 70
    2004-06-14


    Table of Contents:
  • Building a Site Engine with PHP, Part 2
  • Best Site() Ever Made
  • OK, Plug it in Now
  • Module Madness
  • Bring it Together

  • 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 with PHP, Part 2 - Best Site() Ever Made
    ( Page 2 of 5 )

    Now we need to make a class that will get all the information we need about the site. Since each site's common information is stored in the database, all we'll have to do is select it. However we'd need some bit of information about the site to give to the select statement to tell it what site we want information for. I really don't think anyone wants to hand code an entire list of all the sites they have, just to have to update it when you add a site, therefore we look toward the parse_url() function. This function is really useful for something like this; it will take the URL for a site and bust it up into an array that contains all the information about a URL you could ever need. Here's a list of all the elements it can handle:

    • scheme - e.g. http
    • host
    • port
    • user
    • pass
    • path
    • query - after the question mark ?
    • fragment - after the hashmark #

    Pretty useful isn't it? We'll also need to know what page has been requested (www.url.com?page=thispage), therefore we'll also include a small function to get that information. Here is the class we'll use for doing everything, save it as "your_site_dir/inc/site.inc.php":

    <?PHP
    class site{
       
        public $site; //This will hold the array of site information that will be used in other classes
       
        function site(){ //we name the main function the same as the class so that it initializes when the class is called
            global $sql; //this requests the $sql class as a global variable
            $purl=parse_url('http://'.$_SERVER['HTTP_HOST']);    //The parse_url function breaks the page's URL into and array of the URL elements
            $purl=$purl['host'];    //This sets the $purl variable to the value of "host" in the array previously made by parse_url
            $host=explode(".",$purl);    //now we break the hostname from $purl into an array delimited by a period
            if($host[0]=='www'){    //check the value of the hostname
                $this->site['host']=$host[1];    //if the first element of the hostname is www, the variable $site['host'] to the second element of the hostname
            }else{
                $this->site['host']=$host[0]; //if the first element isn't www, set the variable $site['host'] to the first element of the hostname
            }
            $site_info=$sql->_fetch_row($sql->_query("SELECT `sites`.`site_ID`,`sites`.`site_name`FROM`sites`WHERE(`sites`.`site_host` = '{$this->site['host']}') LIMIT 1")); //select the information about the site from the database
            $this->site['ID'] = $site_info[0];    //set the site's ID in the $site variable
            $this->site['name'] = $site_info[1];    //set the site's name in the $site variable
            $this->page();    // call the function page()
        }
       
        function page(){   
            if(isset($_REQUEST['page'])){    //check to see if "page" has been set as a _GET or _POST variable
                $this->site['page']=$_REQUEST['page'];    //if it has been set as a _GET or _POST variable, set it as the page name in $site
            }else{
                $this->site['page']="main";    //if it hasn't been set as a _GET or _POST variable, set the page name in $site too "main"
            }
        }
    }
    ?>

    This class will produce an object with an array that looks like this:

    $site->site['host'] -- the URL to a site without the www or the .com/.org/.net (i.e. www.devshed.com will be deshed)

    $site->site['ID'] -- the site's ID in the database

    $site->site['name'] -- the name you gave to the site when you set it up (it's nice to use as the title of the site)

    $site->site['page'] -- the currently requested page (www.url.com?page=thispage)

    See, using the parse_url() function is pretty useful when you need to retrieve site information. Sometimes it's even easier than calling the query_string.



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

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT