Page 1 of 2
making clean urls using PHP ??
Posted: Mon Sep 03, 2007 11:13 pm
by PHPycho
Hello forums !!
I am bored with working with the url with query string urls ie("
http://localhost/my_project/index.php?p ... ion=create").
Now i would like to work with clean urls like "
http://localhost/my_project/test/create"
I would like to rewrite the url without using mod_rewrite module rather i would like to use PHP.
How to explode the urls to get the required parameters ?
Note: I am working under the my_project folder
Any comments n suggestions are warmly welcome.
Thanks in advance to all of you.
Posted: Mon Sep 03, 2007 11:53 pm
by s.dot
There's
this class for making safe strings to pass as a URL component, from user entered strings. However that uses a .htaccess rule (could be configured not to, of course). Just thought I'd throw that in there because I wrote it.
I think what you want to do is explode('/' , $_SERVER['PATH_INFO']). I might have the wrong server array index, though.
Posted: Tue Sep 04, 2007 12:55 am
by Christopher
I use a .htaccess like this:
Code: Select all
RewriteEngine on
RewriteRule !\.[a-zA-Z0-9]{2,4}$ index.php
And then check $_SERVER['PATH_INFO'], however you may need to get that information from other globals depending on your sever configuration and whether you are in the root directory or in a sub directory.
Posted: Tue Sep 04, 2007 1:50 am
by PHPycho
how to enable $_SERVER['PATH_INFO'], it says undefined index PATH_INFO.
Posted: Tue Sep 04, 2007 1:52 am
by miro_igov
I think it is PATH_TRANSLATED
Posted: Tue Sep 04, 2007 2:29 am
by vigge89
PHPycho wrote:how to enable $_SERVER['PATH_INFO'], it says undefined index PATH_INFO.
As long as you've got permissions, "AcceptPathInfo On" in a htaccess file should do the trick.
Posted: Tue Sep 04, 2007 2:49 am
by CoderGoblin
Another potentially radical solution is to look at some frameworks which already exist, such as the Zend Framework and try to see how they do it. Would mean a lot of investigation and possible changes to code though
Posted: Tue Sep 04, 2007 3:50 am
by claws
oh !!! so URLs can be parsed by scipt??
it means if
http://somedomain.net/folder1/folder2/floder3/
may not 100% imply that there i am requesting for a file of folder3 which is in folder2 which is in folder1
is it???
if so.. I am very happy..
till now i was thinking that
http://somedomain.net/var1=kk&var2=bk as only way to send variables from client to server.
1. kindly tell me what are all the ways URL parsing can be done.??
2. please tell me the package of php for url parsing.
Posted: Tue Sep 04, 2007 4:40 am
by CoderGoblin
The Zend Framework uses a .htaccess file to redirect everything to an index.php file (called a bootstrap file) which then works out what you want to do, what parameters are etc. At it's basic level it is fairly easy to do/implement. The Zend Framework itself however has more work to be done on handling "views"/output of pages in regards to templating but this is probably a personal view.
Posted: Tue Sep 04, 2007 4:44 am
by playgames
I'think . path_info can help u
not need MOD_REWRITE.
NOT APAHCE SUPPORT
use $_SERVER['"PATH_INFO"].
BUT one thing need say to u that is THE php file should have no ext (it just mean apache need support no ext.)
the code below is my site used.
Code: Select all
if(@$path_info =$_SERVER["PATH_INFO"]){
if(preg_match("/\/(\d+)_(\d+)_(\d+)_(\d+)$/si",$path_info,$arr_path)){
$gid =intval($arr_path[1]);
$fid =intval($arr_path[2]);
$sid =intval($arr_path[3]);
$gameid =intval($arr_path[4]);
}else header('location:/');
}else header('location:/');
the emg like this :
http://www.play-games-online.cn/play-ga ... _1_1_10897
BUT MY SITE is used a host not server. So only be play-game.php. this shoulded be play-game/1_1_1_10897
AOL Speak
Posted: Tue Sep 04, 2007 6:12 am
by PHPycho
can [s]u[/s]
YOU [s]plz[/s]
PLEASE tell me what does this regex do ??
RewriteRule !\.[a-zA-Z0-9]{2,4}$ index.php
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:11. Please use proper, complete spelling when posting in the forums. AOL Speak, leet speak and other abbreviated wording can confuse those that are trying to help you (or those that you are trying to help). Please keep in mind that there are many people from many countries that use our forums to read, post and learn. They do not always speak English as well as some of us, nor do they know these aberrant abbreviations. Therefore, use as few abbreviations as possible, especially when using such simple words.
Some examples of what not to do are ne1, any1 (anyone); u (you); ur (your or you're); 2 (to too); prolly (probably); afaik (as far as I know); etc.
Posted: Tue Sep 04, 2007 10:43 am
by playgames
PHPycho wrote:can u plz tell me what does this regex do ??
RewriteRule !\.[a-zA-Z0-9]{2,4}$ index.php
the file name sames should be only contant letters 0-9 .2<=length<=4
using in .htaccess
need server supported
It's NOT you want
PATH_INFO JUST THE BEST

Posted: Tue Sep 04, 2007 10:52 am
by RobertGonzalez
Regardless of the way your app handles URL fetching, handling, production, etc, your server will need a way to map all incoming requests to a single file. This is commonly done with a .htaccess file in Apache in a shared hosting environment. If you manage your own Apache web server, you can add the rewrite rules to the httpd.conf directly and remove the need for a .htaccess file.
I am not sure how to handle request mapping in other servers.
Posted: Tue Sep 04, 2007 11:48 pm
by PHPycho
Pardon me..
i think PATH_INFO only works if i had following url
http://localhost/my_project/index.php/test/create
not if we use
http://localhost/my_project/test/create
Any idea ??
Posted: Wed Sep 05, 2007 12:08 am
by Christopher
So add the .htaccess I gave you above and see what you get. Create an index.php that looks like this:
and experiment and look at the server vars. You need to look at the values returned and see how you can reconstruct PATH_INFO.