Dynamic PHP Content Tutorial

Discussion in 'Web Programming' started by cp, Feb 11, 2007.

Thread Status:
Not open for further replies.
Dynamic PHP Content Tutorial
  1. Unread #1 - Feb 11, 2007 at 12:15 PM
  2. cp
    Joined:
    Jan 30, 2007
    Posts:
    3,278
    Referrals:
    6
    Sythe Gold:
    0

    cp an cat
    Banned

    Dynamic PHP Content Tutorial

    First of all, who uses plain html for a website? I mean, the files start accumulating, when you need to change the layout, you have to edit all the html files, when you add a link, you also have to edit the navigation, and iFrames are just out of the question. This is where php comes in!

    Lets say you have index.html, and this is it's contents:
    Code:
    <table>
    <tr><td>
    Banner yay</td></tr>
    <tr><td><a href="blah.html">blah</a> | <a href="poo.html">poo</a> | <a href="html.html">html</a></td></tr>
    <tr><td>
    This is the index page. Yay</td></tr>
    </table>
    
    Not the most xhtml compliant or neat code, but it gets the job done.

    The webmaster that owns that site is pissed that it takes him nearly 4 hours just to update all the pages. So he uses google and finds out how to make his life easier. Unfortunately, google has failed him yet again.

    Okay, this is where we begin with the php.First, lets start off with the nav, it's easier.

    nav.php:
    Code:
    <a href="?page=blah">blah</a> | <a href="?page=pool">poo</a> | <a href="?page=html">html</a>
    
    content.php:
    Code:
    <?php
    if(isset($_GET['page'])) {
    if(file_exists('pages/'.$_GET['page'].'.php')) {
    include('pages/'.$_GET['page'].'.php');
    }
    else {
    echo('Page not found.');
    exit;
    }
    else {
    include('pages/main.php');
    }
    ?>
    
    Okay, if you don't understand that code, I'm here, so it's okay.

    You have to start out ALL php codes with either <? <?php or <% (if that's enabled in the php.ini file)

    Code:
    if(isset($_GET['page'])) {
    
    Now we check if the variable $_GET['page'] is set. $_GET signifies that it's a GET request being submitted. You can tell if a website is using GET requests when you see variables in the url, in this case, it would be content.php?page=whatever . If the page was content.php?page=hax , the variable $_GET['page'] would hold the string value 'hax'.

    Code:
    if(file_exists('pages/'.$_GET['page'].'.php')) {
    
    Now we know that the $_GET['page'] is set, we now have to see if this is an existant and allowed page. We're using the file_exists function to check if the file exists in the directory 'pages' with the file name of what ever is in the $_GET['page'] value. The reason I ended
    Code:
    if(file_exists('pages/'.$_GET['page'].'.php')) {
    
    in .php is because people tend to put other files in random directories, and you don't want someone using your script against you, which would be bad, so we limit the extensions allowed to be included to .php .

    If the file exists, hence the if() we run the code in the {}'s which includes the file.

    Code:
    include('pages/'.$_GET['page'].'.php')
    
    That looks for the file and includes it, it's sort of like iFrames, but it works on most, if not all browsers, and just plain blends in better with the layout.

    Code:
    else {
    echo('Page not found.');
    exit;
    }
    
    If the .php file isn't found in the pages directory, it will echo out the error message and exit from loading the rest of the page, just to be secure.

    Now for the second else statement:
    Code:
    else {
    include('pages/main.php');
    }
    
    If the $_GET['page'] holds no value, then it includes the main page, which normally would be attached to the index.html page, but is now pages/main.php.

    Remember, in the .php files in the pages directory, do not, and I repeat DO NOT put the layout code in those files, that will only make you look stupid and your site as well.

    Now, to actually begin using this system:
    Rename index.html to index.php for this to work. Get rid of any and all content in the content section of the layout and replace it with this:
    Code:
    <?php
    include('content.php']);
    ?>
    
    That gets the content.php and sort of 'binds' it to your page making it look like your original .html files yet much more advanced.

    And find where your nav is, and clear it, and add this:
    Code:
    <?php
    include('nav.php');
    ?>
    
    That finds nav.php and also binds it to your nav section. If you hadn't noticed, I changed the original nav links to something else in nav.php.
    I changed <a href="poo.html"> to <a href="?page=poo">. We need to make the links in that form instead for the $_GET['page'] variable to hold any data, or you'd be getting all sorts of errors.


    If anyone needs any help, if any mistakes are found, anything, just tell/ask me.
     
< In need of a server =/ | explaining how to make html scripts (easy+++, good instructions) >

Users viewing this thread
1 guest
Thread Status:
Not open for further replies.


 
 
Adblock breaks this site