0

Playing Around with PHP URL Extraction


I discovered today that there is a feature in PHP that can grab current URL through $_SERVER['REQUEST_URI'] and convert it into an array using explode function. I think this is extremely useful to me. Partly because I always wanted to have an URL that is similar to the URL for this blog. Another reason is that I don’t have to edit my sever configuration either directly or through PHP to achieve the same result. The method I use to create an URL that only contains the folder of the page, that is, the page is index.php, might be useful for small websites that have less than fifty web pages or possibly less. I didn’t test it on a large set of web pages because I just thought about using this type of URL format in the final project for my CA 282 class. It is a four-page project, therefore I cannot guarantee that it can work on a larger set of web pages. I personally think that it can become disorganized and confused once I have a little more web pages by using this method. Here is the code if you want to test it. Basically, it includes all the files inside the main index page based on the value at the last slash of URL, e.x. http://www.example.com/folder/item :

    $currentURL = $_SERVER['REQUEST_URI'];
    $URLpart = explode("/", $currentURL);

    $endURL = $URLpart[sizeof($URLpart)-2];
        switch ($endURL) {
            case 'signup':    //  http://zebra0.com/Chen/final/account/signup/
                include_once 'includes/signup.php';
                break;
            case 'login':     // http://zebra0.com/Chen/final/account/login/
                include_once 'includes/login.php';
                break;
            case 'account':   // http://zebra0.com/Chen/final/account/
                include_once 'includes/account.php';
                break;
            case 'signout':   // http://zebra0.com/Chen/final/account/signout/
                include_once 'includes/signout.php';
                break;
            default:
                include_once 'includes/index.php';
        }

And the index.php file inside each of the folder simply include the main index.php at the root of the project folder. Because of the nature of the included file, I used the dynamic path using PHP $_SERVER array to generate an absolute URL for the links and style sheets instead of the static ones.

$currentURL = $_SERVER['REQUEST_URI'];
$URLpart = explode(“/”, $currentURL);

$endURL = $URLpart[sizeof($URLpart)-2];
switch ($endURL) {
case ‘signup’:
include_once ‘includes/signup.php’;
break;
case ‘login’:
include_once ‘includes/login.php’;
break;
case ‘account’:
include_once ‘includes/account.php’;
break;
case ‘signout’:
include_once ‘includes/signout.php’;
break;
default:
include_once ‘includes/index.php’;
}


There are no posts related to Playing Around with PHP URL Extraction.