PHP implementation pseudo static method rollup _php instance

Source: Internet
Author: User

The use of PHP pseudo static is mainly to hide the passing parameter name, the following to introduce the implementation of the pseudo static PHP method, details please see below.
Speaking of pseudo static implementation, you are not very straightforward answer "simple, configure the Apache rewrite rules on the line"

But you have not found this situation, you recently made a lot of new features, every day a number of new features, every day there are many pseudo static configuration, just start two days Yun-dimensional students are willing to cooperate, after two days Yun-dimensional students will scold. You paralysis, brain residue why not once done, every day trouble me. But, you have to go online ah, have to hard to ask Yun-dimensional classmate, and then say a program ape the most shameless words "this time the last change", and then change, ah, your personality is sweeping the floor ...

If there is such a problem, please see the article below, to ensure that you will never ask for the operation of the Victoria, do what you want to do ...

How many ways can php implement pseudo static? Personal insights and Statistics Austria, there are four ways

1, the use of Apache URL rewrite rules, this everyone knows, in the Apache configuration, where the students are built, only to enumerate a simple configuration

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

2, using PHP pathinfo, you are not to see some sites like this play ' 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 ' to give an example

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

3, use 404 mechanism, under normal circumstances pseudo static is actually nonexistent page, so can use Apache 404 configuration, but some problems, is ' post ' type of request will be discarded, cause you can't get ' $_post ',

But ' $_get ' is still available, assuming the 404 page is ' 404page.php ' and 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, or the state is 404
$reqUrl = $_server[' Request_uri '];//Request Address
/**
* parsing parameters from URL/
function Parseurlparams ($QUERYURL)
{
$arr = explode ('? ', $QUERYURL);
Parse_str ($arr [1], $param);
if ($param)
{
foreach ($param as $key => $value)
{
$_get[$key] = $value;
}}} Parseurlparams ($REQURL); URL resolution parameters
///Then you can use the $REQURL to match the different actual request addresses according to your own rules
if (Preg_match (' #^/test.html#is ', $REQURL, $matches))
{
include (' index.php ');
Die ();
}

4, Method 3 of the improved type, Method 3 in the Apache internal mechanism is equivalent to redirection, resulting in post (get) passed parameters can not be obtained. Analysis of the above is actually unable to find the relevant files, when the server can not find the relevant files, we specify a file for it, not OK, it does not have to 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 to use the ' index.php ' in the root directory when the requested file or directory cannot be found, then you can get the relevant parameters in ' index.php ' and parse 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 for you to introduce the implementation of the pseudo static PHP method, hope to help everyone.

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.