Php-based pseudo-static implementation details _ php instances

Source: Internet
Author: User
This article introduces the implementation of php pseudo-static in detail. For more information, see 1. perform operations based on $ _ SERVER ['path _ info.
For example, your website address is http: // 127.0.0.1/show_new.php/look-id-1.shtml
The result of your echo $ _ SERVER ['path _ info'] would be/look-id-1.shtml to see this I think you may already understand.
Complete demo
Index. php

The code is as follows:


Index. php

$ Conn = mysql_connect ("localhost", "root", "root") or dir ("connection failed ");
Mysql_select_db ("tb_demo", $ conn );
$ SQL = "select * from news ";
$ Res = mysql_query ($ SQL );
Header ("content-type: text/html; charset = utf-8 ");
Echo "news list ";
Echo "add news

";
Echo"





";Echo" ";While ($ row = mysql_fetch_assoc ($ res )){Echo" ";}// The Red address above should have been show_news.php? Act = look & id = {$ row ['id']}Echo"
Id Title View details Modify News
{$ Row ['id']} {$ Row ['title']} View details Modify page
";
// Close the resource
Mysql_free_result ($ res );
Mysql_close ($ conn );


Show_new.php page

The code is as follows:


Show_new.php

Header ("Content-type: text/html; charset = utf-8 ");
$ Conn = mysql_connect ("localhost", "root", "root ");
Mysql_select_db ("tb_demo", $ conn );
Mysql_query ("set names utf8 ");
$ Pa = $ _ SERVER ['path _ info'];
// $ The value printed out by pa is/look-id-1.html
// Use a regular expression to match the obtained url
If (preg_match ('/^ \/(look)-(id)-([\ d]) \. shtml $/', $ pa, $ arr )){
$ Act = $ arr [1]; // This is the look method of the request.
$ Id = $ arr [3]; // This is the obtained id value.
$ SQL = "select * from news where id = $ id ";
$ Res = mysql_query ($ SQL );
$ Res = mysql_fetch_assoc ($ res );
Echo $ res ['title']. "". $ res ['content'];
} Else {
Echo "invalid url ";
}
Mysql_close ($ conn );


I think you will understand the above. In fact, this method is not much used. I will explain the second method to you below.

2. configure. htaccess.
First, let's talk about how to create the. htaccess file. create a notepad under the root directory of the website, double-click it, and click "Save as file name ".
. Htaccess: Select All files for the storage type, and select UTF-8 for the encoding. you will see this. htaccess file in the directory.

Enable mod_rewrite.so in apache, and replace AllowOverride None with AllowOverride All.

For example, the href address is written as a one_new-id-1.shtml // which means one_new.php? Id = 1
Here, the. htaccess can be written in this way.

The code is as follows:



# Write your rewrite rules
RewriteEngine On
# You can configure multiple rules. The matching sequence is from top to bottom.
RewriteRule one_new-id-(\ d +) \. shtml $ one_new.php? Id = $1 // Here $1 represents the first parameter.
RewriteRule abc_id (\ d +) \. html $ error. php
# Setting 404 errors
# ErrorDocument 404/error. php


On the one_new.php page, echo $ _ GET ['id'] will definitely output the id value.

Note: my personal abilities can only be written here and I will gradually improve it later.
If you have any questions, please leave me a message.

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.