Home arrow PHP arrow Page 2 - Building a Site Engine with PHP, Part 2

Best Site() Ever Made - PHP

If you read the first article in this series, you’d know that we’re about to start talking about the plug-in and module system for our site engine. Along with the plug-ins and modules, we’re also going to be discussing some of the general functions that are needed to make site engine work correctly.

TABLE OF CONTENTS:
  1. Building a Site Engine with PHP, Part 2
  2. Best Site() Ever Made
  3. OK, Plug it in Now
  4. Module Madness
  5. Bring it Together
By: James Murray
Rating: starstarstarstarstar / 70
June 14, 2004

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: