开发者

Pass data in URL without using GET

开发者 https://www.devze.com 2022-12-20 00:43 出处:网络
I have a page that displays inform开发者_如何学Goation for a selected member.Right now, I choose select the member in the URL using GET data like so: Page/index.php?Member=member1I see websites that d

I have a page that displays inform开发者_如何学Goation for a selected member. Right now, I choose select the member in the URL using GET data like so: Page/index.php?Member=member1 I see websites that do it like this: Page/member1 Is this possible to do with only one PHP file?


Friendly URLs need some kind of processing by the webserver so that the page requests are channeled through a single php script (aka controller).

For example, Apache has the ability to match patterns in the URL and modify the request behind the scenes - you would actually write the PHP to react as if the user had entered the original GET string, but they can use the friendly URL instead.

Simple example for Apache - put a .htaccess in your document root and write:

RewriteEngine On
RewriteRule   ^Page/([A-Za-z0-9\_]+)/?$   index.php?member=$1 [QSA,L]

I don't know what the equivalent would be in IIS - but I am aware that it is available


This can't be done with just PHP - you do this with mod_rewrite and usually .htacess on apache.


There are many ways to do this, all of which depend on the web server. With Apache, some possibilities:

  • mod_rewrite (most flexible way, but can be tedious to configure)
  • Option +MultiView (very easy, but requires PHP files to be named similar to the requested URL)
  • custom 404 handler (not recommended)

Say you are using Apache and have enabled MultiView. The file Page.php would be a match for Page/*, which would obviously include Page/member1. You would then have to inspect the URL (look at $_SERVER, it has everything you need) and decide if you can honor the request.


You can do it in PHP alone using the following:

1.) Get filename relative to the document root.

$_SERVER['PHP_SELF']

http://example.com/test.php/foo.bar would be /test.php/foo.bar.

2.) Explode by your delimiter (in this case /)

array explode  ( string $delimiter  , string $string  [, int $limit  ] )

3.) Verify values in array and then place into variables

var $myValue = myarray[0];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号