How to implement PHP pseudo static

Source: Internet
Author: User

There are several ways to implement PHP pseudo static:

1. According to $_server[' Path_info '] to operate the implementation.

For example, the address of your website is http://127.0.0.1/show_new.php/look-id-1.shtml.

The result of your echo $_server[' Path_info ' will be/look-id-1.shtml see this I think you may have understood.

The full demo

index.php

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 "

show_new.php page

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 '];   
$PA  Printed value is  /look-id-1.html   
//The URL address that is obtained through a regular expression match   
if (Preg_match ('/^\/look)-(ID)-([\d]) \. shtml$/', $pa, $arr)) { 
 $act = $arr [1];//This is the requested look method   
 $id = $arr [3];  This is the ID value obtained   
 $sql = "SELECT * FROM News  where id= $id";   
 $res =mysql_query ($sql);   
 $res = Mysql_fetch_assoc ($res);   
 echo $res [' title ']. 

See the above this I think you must understand it in fact this way with a few of the following for everyone to say the second method of AH

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/PHP/

2. According to the configuration. htaccess to achieve.

Let's say first. htaccess file how to create it, in the site root directory to build a notepad and then double-click Open Click Save as file name written

. htaccess, save type Select all files, encode select Utf-8 code okay this is what you see in the catalogue. htaccess file.

First on Apache Mod_rewrite.so,allowoverride None here are replaced by allowoverride all

such as the HREF address written one_new-id-1.shtml///This means one_new.php?id=1

Here's the. htaccess can write that.

<IfModule rewrite_module>
#写你的rewrite规则
RewriteEngine On
# 可以配置多个规则,匹配的顺序是从上到下
RewriteRule one_new-id-(\d+)\.shtml$ one_new.php?id=$1 //这里的$1 代表的是第一个参数啊
RewriteRule abc_id(\d+)\.html$ error.php
#设置404错误
#ErrorDocument 404 /error.php
</IfModule>

Your one_new.php page echo $_get[' id ' will definitely output the ID's value.

Description: This current personal ability can only be written here, I will gradually improve the

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.