making clean urls using PHP ??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

making clean urls using PHP ??

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Post by PHPycho »

how to enable $_SERVER['PATH_INFO'], it says undefined index PATH_INFO.
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

I think it is PATH_TRANSLATED
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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
claws
Forum Commoner
Posts: 73
Joined: Tue Jun 19, 2007 10:54 am

Post 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.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
User avatar
playgames
Forum Newbie
Posts: 22
Joined: Tue Sep 04, 2007 4:28 am

Post 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
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

AOL Speak

Post 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.
User avatar
playgames
Forum Newbie
Posts: 22
Joined: Tue Sep 04, 2007 4:28 am

Post 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 :lol:
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Post 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 ??
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

PHPycho wrote: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 ??
So add the .htaccess I gave you above and see what you get. Create an index.php that looks like this:

Code: Select all

<?php
phpinfo();
and experiment and look at the server vars. You need to look at the values returned and see how you can reconstruct PATH_INFO.
(#10850)
Post Reply