PHP implementation pseudo static method summary _php instance

Source: Internet
Author: User
PHP pseudo-static use is mainly to hide the passed parameter name, the following gives you the implementation of the pseudo-static PHP method, see below for details.
Speaking of pseudo-static implementation of the scheme, you are not very quick to answer the "simple, configure the Apache rewrite rules on the line."

But have you found this situation, you recently made a lot of new features, a few new features every day, a lot of pseudo-static configuration every day, only to start two days of the students are also willing to cooperate, after two days of yun-dimensional students will scold. You paralysis, brain residue why not once finished, every day trouble me. But, you have to go online ah, have to hard to ask for the operation of the students, and then say a program ape the most shameless words, "This last change", and then change again, hey, your personality is sweeping ...

If there is such a problem, please look at the following article, to ensure that you do not seek the operation of the future, do what you want to do ...

How many ways does the PHP implement pseudo-static? Personal insights and Statistics Austria, there are four ways

1, the use of Apache URL rewrite rules, this everyone understand, in Apache configuration, here students are built, just list a simple configuration

Rewriteengine on
Rewriterule ^/test.html index.php?controller=index&action=test [L]

2, using PHP pathinfo, you do not have to see some sites such as playing ' www.xxx.com/index.php/c/index/a/test/id/100 ', of course to support this you need to put ' php.ini ' in the parameters

' Cgi.fix_pathinfo ' is set to 1. Take ' www.xxx.com/index.php/c/index/a/test/id/100 ' for example

echo $_server[' Path_info ']; Output '/c/index/a/test/id/100 '
Here, you should understand, you can parse this section, assign the actual address

3, using the 404 mechanism, the pseudo-static in general is the actual non-existent page, so you can use the Apache 404 configuration, but some problems, is the ' post ' type of request will be discarded, resulting in you cannot get ' $_post ',

But ' $_get ' is still available, assuming the 404 page here is ' 404page.php ', Apache is configured as follows:

ErrorDocument 404/404page.php

Then embed the following code in ' 404page.php '

Header ("http/1.1 OK"); There must be, otherwise the state is 404$requrl = $_server[' Request_uri ']; Request address/*** parse parameter from URL */function parseurlparams ($queryUrl) {$arr = explode ('? ', $queryUrl);p arse_str ($arr [1], $param); if ($param) {foreach ($param as $key = = $value) {$_get[$key] = $value;}}} Parseurlparams ($REQURL); URL parsing parameters//Then you can use $REQURL according to your own rules to match different actual request addresses if (Preg_match (' #^/test.html#is ', $REQURL, $matches)) {include (' index.php ');d ie ();}

4, Method 3 of the improved type, Method 3 in the Apache internal mechanism equivalent to redirect, resulting in post (get) passed parameters can not be obtained. Analysis above is actually not find the relevant files, that when the server can not find the relevant file, we specify a file for it, not OK, it does not jump, then post and so will not be lost. The Apache configuration is as follows:

Rewriteengine on
Rewritecond%{request_filename}!-f
Rewritecond%{request_filename}!-d
Rewriterule. index.php

The general meaning of the above configuration is that when the requested file or directory cannot be found using the ' index.php ' substitution in the root directory, then you can get the relevant parameters in ' index.php ' and resolve to the actual request address

/*** gets the URI address of the current request * @param void* @author painsonline* @return string uri*/function Getrequri () {return trim ($_server[) Request_uri "]);} $reqUri = Getrequri (), if (Preg_match ('/^\/test.html/isu ', $reqUri)) {//Parse request address include ' test.php '; exit ();}

The above content to introduce the implementation of the pseudo-static PHP method, we hope to help.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.