PHP implementation of pseudo-static code based on $_server[' Path_info ' and. htaccess

Source: Internet
Author: User
1. Operate the implementation according to $_server[' Path_info ').

For example, your site's address is http://127.0.0.1/show_new.php/look-id-1.shtml.
You echo $_server[' Path_info ' come out of the results will be/look-id-1.shtml see this I think you may already understand.
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

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 the  printed value is  /look-id-1.html//the URL address obtained by the regular expression match 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 ID value obtained $sql = "SELECT * FROM News  where id= $id"; $res =mysql_query ($sql); $res = Mysql_fetch_assoc ($res); Echo $re s[' title ']. " 

See the above I think you must understand that this way with a few of the following to everyone said the second method of AH

2. Implement according to the configuration. htaccess.
Let's start with the. htaccess file, create a notepad in the root directory of the Web site and double-click the Save as file name to write
. htaccess, save type Select all files, encode select Utf-8 encoded well this is what you see in the directory of this. htaccess file now
First in Apache Open Mod_rewrite.so,allowoverride None Here are two replacements for allowoverride all
For example, the href address is written one_new-id-1.shtml//This means one_new.php?id=1
This is where the. htaccess can write.

<ifmodule rewrite_module> #写你的rewrite规则RewriteEngine on# can configure multiple rules, the order of matching is from top to bottom rewriterule  one_new-id-(\d+) \.shtml$ one_new.php?id=$1//Here is the first parameter Ah rewriterule  abc_id (\d+) \.html$ error.php#     set 404 error # ErrorDocument  404  /error.php</ifmodule>

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

Related Article

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.