Home > Backend Development > PHP Tutorial > Introduction to two methods of implementing pseudo-static in PHP

Introduction to two methods of implementing pseudo-static in PHP

WBOY
Release: 2016-07-25 08:59:42
Original
985 people have browsed it
  1. /**
  2. * php pseudo-static
  3. * bbs.it-home.org
  4. */
  5. $conn=mysql_connect("localhost","root","root")or dir("Connection failed");
  6. mysql_select_db("tb_demo ",$conn);
  7. $sql="select * from news";
  8. $res=mysql_query($sql);
  9. header("content-type:text/html;charset=utf-8");
  10. echo "

    News list

    ";
  11. echo "Add news
    ";
  12. echo " ";
  13. echo "
  14. ";
  15. while($row=mysql_fetch_assoc($res)){
  16. echo "
  17. ";
  18. }
  19. //The red address above It should have been show_news.php?act=look&id={$row['id']}
  20. echo "
  21. idtitleView detailsModify news
    {$row['id']}{ $row['title']}View detailsModify page
    ";
  22. //Close the resource
  23. mysql_free_result($res);
  24. mysql_close($conn);
  25. ?>
Copy code

2, show_new.php page

  1. header("Content-type:text/html;charset=utf-8");
  2. $conn=mysql_connect("localhost","root","root");
  3. mysql_select_db("tb_demo",$conn);
  4. mysql_query("set names utf8");
  5. $pa = $_SERVER['PATH_INFO'];
  6. //The printed value of $pa is /look-id-1.html
  7. //The url address obtained through regular expression matching
  8. if(preg_match('/^/(look)-(id)-([d]).shtml$/',$pa,$arr)){
  9. $ act = $arr[1]; //This is the requested look method
  10. $id = $arr[3]; //This is the obtained id value
  11. $sql="select * from news where id= $id";
  12. $res=mysql_query($sql);
  13. $res = mysql_fetch_assoc($res);
  14. echo $res['title']."
    ".$res['content'];
  15. }else{
  16. echo "url address is illegal";
  17. }
  18. mysql_close($conn);
  19. ?>
Copy the code

Second, implement it according to the configuration .htaccess Let’s first talk about how to create the .htaccess file. Create a notepad in the root directory of the website and then double-click to open it. Click Save as and write the file name as .htaccess. Select all files as the save type and utf-8 as the encoding. This is what you need. I saw this .htaccess file in the directory.

First, open mod_rewrite.so in apache, and replace AllowOverride None in two places with AllowOverride All.

For example, the href address is written as one_new-id-1.shtml //This means one_new.php?id=1 The .htaccess here is written like this:

  1. #Write your rewrite rules
  2. RewriteEngine On
  3. # You can configure multiple rules, and the matching order is from top to bottom
  4. RewriteRule one_new-id-(d+).shtml$ one_new .php?id=$1 //$1 here represents the first parameter
  5. RewriteRule abc_id(d+).html$ error.php
  6. #Set 404 error
  7. #ErrorDocument 404 /error.php
Copy the code

In one_new.php page echo $_GET['id'] will definitely output the value of id.

Let’s just introduce these. It is very simple to implement pseudo-static in PHP. I hope you can practice more and write more flexible and lightweight pseudo-static rules.



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template