7

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

4 Answers 4

12
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'), '?'));
Sign up to request clarification or add additional context in comments.

13 Comments

@Gumbo It wasn't a requirement. Merging this array with $_GET isn't difficult either.
@pestaa: But REQUEST_URI does not just contain the request URI path but also the query string.
@Gumbo Environment-specific, mine happens to include the URI path only. For clarity, I'll include a second snippet soon.
@Gumbo Have a look please. Can't be any shorter, right? :)
Sorry people - I only want the stuff after the domain address (ie forum/board1/sub-forum/topics/123 that)
|
4

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.

3 Comments

Well, the paths are virtual - they're taken from a database and constructed. Are you saying that navigating to that path will return a 404, but still execute the PHP?
No. I mean unless your webserver will know that all urls should be redirected to a PHP script, it will look at the corresponding path on the filesystem and return a 404 error when it doesn't exist.
Ahh ok thanks for that. I take it the solution is to use HTAccess files? I may be able to edit the mod_rewrite rules, but don't count on it.
2

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.

1 Comment

No no - that would be most insane! Creating all the directories is just mad. As I have mentioned in other comments, I will use HTAccess and possibly mod_rewrite. Thanks for your help.
1

If you already have configured your web server to send those requests to your particular PHP file, you can use parse_url and explode to get and then split the requested URI path into its segments:

$_SERVER['REQUEST_URI_PATH'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$pathSegments = explode('/', $_SERVER['REQUEST_URI_PATH']);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.