Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP take arguments from URL path

Tags:

url

php

Say I have a url like this:

http://www.mysite.com/forum/board1/sub-forum/topics/123

Is there a simple way in PHP (can't use HTAccess) to take that URL and extract board1, sub-forum, topics and 123 so I can use them in a database for example? Are there any built in functions or will I have to write my own?

Thanks,

James

like image 494
Bojangles Avatar asked Nov 18 '10 10:11

Bojangles


3 Answers

explode('/', getenv('REQUEST_URI'));

If your environment happens to include the query string part in the above value, here's a neat workaround:

explode('/', strtok(getenv('REQUEST_URI'), '?'));
like image 87
pestaa Avatar answered Sep 27 '22 21:09

pestaa


You can, but without redirecting requests your webserver will just return a 404 error for non-existing paths.

However, you can use urls like http://your.site.com/index.php/foo/bar/baz and then split the url into parts like @pestaa said which you can then parse into parameter values.

like image 25
nikc.org Avatar answered Sep 27 '22 21:09

nikc.org


This is taken from my MVC http://www.phpclasses.org/package/6363-PHP-Implements-the-MVC-design-pattern.html

The link is outdated at the minute, I have just updated it so it does not have the MVC stuff in, and this can be called with getLoadDetails($_URL); amd $_URL will be exactly the same as $_GET other than it gets the data from the folder path.

function getLoadDetails(&$_URL){
            $filePath = $_SERVER['REQUEST_URI'];
            $filePath = explode("/", $filePath);

            for($i = 0; $i < count($filePath); $i++){
                    $key = $filePath[$i];
                    $i++;
                    $val = $filePath[$i];
                    $keyName = urldecode($key);
                    $_URL[$keyName] = urldecode($val);
            }
    }

I do have one question, if you cant use HTACCESS how do you plan on coping with the folder path please dont tell me your system is going to create the folder paths and index file for every URL that will trash your server Speed and your Host will hate you for it.

like image 36
Barkermn01 Avatar answered Sep 27 '22 23:09

Barkermn01