PHP code help???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bliljerk101
Forum Newbie
Posts: 1
Joined: Thu Oct 16, 2008 5:40 pm

PHP code help???

Post by bliljerk101 »

I have been using this code on my site to switch content between one universal file (index.php). The only problem is that any file can be switched into there via changing the URL. I want it so that only specific files can be switched in. Maybe it could echo something like "no file found" when someone tries to alter the URL to go to a file I don't want shown.

Another thing, if someone can, when a bad URL is typed in, and there actually is NOT a file where the URL suggests, it shows the typical PHP errors: parse error, can't find whatever/whatever/blah.php

Here is the code I'm using:

Code: Select all

<?php
    $url = '';
    if (!empty($_GET['site'])) {
        $url .=  $_GET['site'] . '/';
    }
    if (!empty($_GET['category'])) {
        $url .=  $_GET['category'] . '/';
    }
    if (!empty($_GET['page'])) {
        $url .= $_GET['page'] . '.php';
    }
    if (!empty($_GET['product'])) {
        $url .= $_GET['product'] . '.php';
    }
?>
<?php
if ( !empty($_GET['page']) || !empty($_GET['product']) || !empty($_GET['site']) || !empty($_GET['category']) ) {
    include $url;
} else {
    include ('main/home.php');
}
?>
I had a few ideas of my own - such as using a specific array of files that can be show (code below) - but I can't figure out how to mingle the codes I have together with it:

Code: Select all

<?php
    $page = $_GET['page'];
    $pages = array('page1', 'page2', 'page3');
    if (!empty($page)) {
        if(in_array($page,$pages)) {
            $page .= '.php';
            include($page);
        }
        else {
        echo 'Page not found. Return to
        <a href="index.php">index</a>';
        }
    }
    else {
        include('page1.php');
    }
?>
Another problem with the code above is I need it to not only recognize the "page" but my site needs to recognize the "product" as well - because I decided to use two. They both have the same function. I just figured it would look better if I could choose between "page" or "product" - eg. index.php?site=store&page=one & index.php?site=store$product=one (both of these urls go to the exact same place) - sorry for the poor explanation. I'm kind'a in a hurry.

Any help would be greatly appreciated. :)
Post Reply