A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearURL mapping in PHP? - page 1
User InfoPosts
URL mapping in PHP?#1
I come from a Java background and with any servlets-based technology, it's trivial to map a range of URLs (eg /reports/, /secure/.do) to a specified servlet. Now I'm less familiar with PHP but I haven't yet seen anything that does quite the same thing with PHP (or mod_php). It's entirely possible that I'm missing something simple.

How do you do this in PHP?

One of the reasons I want to do this is "one use" URLs. Now this can sorta be done with GET parameters (like an MD5 hash token) but I'm interested in URL mapping as a general solution to many problems.

Another big reason to use something like this is to have RESTful URLs.

posted date: 2008-12-27 17:02:00


Re: URL mapping in PHP?#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-27 17:02:01


Re: URL mapping in PHP?#3
Can't be done in PHP as such. Note that in Java, it's not the Servlets either that map URLs, but it's the configuration of the servlet container (e.g. Tomcat).You can achieve similar results for PHP applications by using URL rewriting.

posted date: 2008-12-27 17:08:00


Re: URL mapping in PHP?#4
This sounds like something that mod_rewrite would typically be used for (as a part of Apache, not PHP.) I have not heard of anything in PHP which would accomplish this.

posted date: 2008-12-27 17:10:00


Re: URL mapping in PHP?#5
With Apache, you are able to setup URL Rewriting for your php pages with mod_rewrite, check this resources:mod_rewrite: A Beginner's Guide to URL RewritingModule mod_rewriteURL Rewriting Guide

posted date: 2008-12-27 17:11:00


Re: URL mapping in PHP?#6
Other than using mod_rewrite, as already reported you can do a little of magic with a simple trick.Put into the .htaccess a directive like this one<FilesMatch "^servlet$"> ForceType application/x-httpd-php</FilesMatch>substitute ^servlet$ with a regular expression of your choice (it will be the name of your dispatcher)The file servlet should be similar to this<?php $data = explode('/',$HTTP_SERVER_VARS['PATH_INFO']); // $data[0] always empty $fileToInclude = $data[1].'.php'; if (file_exists($data[1]) { $params=array_slice($data,2); // you can do here something more sophisticated // for example sanitize parameters or assemble // an hash include ($fileToInclude); //Think to this file as a servlet } else { // issue a 404 error, maybe one of the 500 series }?>The url can have the form: http://yoursite/servlet/reports/sales/2009 you can also reach the form http://yoursite/reports/sales/2009plaiyng a little with the .htacces and the dispatcher.This method has the advantage that mod_rewrite is not required as FilesMatch (1.3+) and ForceType (2.0+) are in the apache core See for referencehttp://httpd.apache.org/docs/2.2/mod/core.html#forcetypehttp://httpd.apache.org/docs/2.2/mod/core.html#filesmatchhttp://www.devarticles.com/c/a/Apache/Using-ForceType-For-Nicer-Page-URLs/1/

posted date: 2008-12-27 18:19:00


Re: URL mapping in PHP?#7
Options for HTTP servers other than Apache:If you are using Lighttpd, then their mod_rewrite module has a completely different configuration syntax. See http://redmine.lighttpd.net/wiki/1/Docs:ModRewrite for details. If you are using IIS, then you need ISAPI_Rewrite (http://www.isapirewrite.com/) or the URL Rewrite Module (http://learn.iis.net/page.aspx/460/using-url-rewrite-module/).

posted date: 2008-12-28 01:31:00


Re: URL mapping in PHP?#8
As others have mentioned you can use mod_rewrite to do this but also you could use a front controller design pattern which is what many of the web frameworks use. Take a look at Horde routes or Zend_Controller_Router_Rewrite which can be used for MVC,

posted date: 2008-12-30 20:28:00


Re: URL mapping in PHP?#9
A trick I use is the $ _ SERVER['PATH_INFO'] (I think you need Apache). This gets you all of the information after the PHP file extension.http://www.example.com/page.php/rest/urlthe $ _ SERVER['PATH_INFO'] will contain:"/rest/url"The page can then redirect, process some code or whatever based on that string. This is good if you don't have access to the Apache Configs or .HTACCESS, Also if things change you can make page.php a directory name for static file placement. Oh and PHP doesn't need to have a .php extension. I've used .api or somesuch in the past.http://www.example.com/worker.api/rest/urlThat seems to work well for me.

posted date: 2008-12-31 01:12:00


Re: URL mapping in PHP?#10
This works in Lighttpd as well.

posted date: 2009-05-22 03:24:00


Re: URL mapping in PHP?#11
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post.

posted date: 2015-02-08 18:50:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.