欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php伪静态的几种实现代码的例子

程序员文章站 2022-06-08 17:21:39
...
  1. //伪静态方法一
  2. // localhost/jbxue/test.php?id|1@action|2
  3. $Php2Html_FileUrl = $_SERVER["REQUEST_URI"];
  4. echo $Php2Html_FileUrl."
    ";// /jbxue/test.php?id|1@action|2
  5. $Php2Html_UrlString = str_replace("?","",str_replace("/", "", strrchr(strrchr($Php2Html_FileUrl, "/"),"?")));
  6. echo $Php2Html_UrlString."
    ";// id|1@action|2
  7. $Php2Html_UrlQueryStrList = explode("@", $Php2Html_UrlString);
  8. print_r($Php2Html_UrlQueryStrList);// Array ( [0] => id|1 [1] => action|2 )
  9. echo "
    ";
  10. foreach($Php2Html_UrlQueryStrList as $Php2Html_UrlQueryStr)
  11. {
  12. $Php2Html_TmpArray = explode("|", $Php2Html_UrlQueryStr);
  13. print_r($Php2Html_TmpArray);// Array ( [0] => id [1] => 1 ) ; Array ( [0] => action [1] => 2 )
  14. echo "
    ";
  15. $_GET[$Php2Html_TmpArray[0]] = $Php2Html_TmpArray[1];
  16. }
  17. //echo '假静态:$_GET变量
    ';
  18. print_r($_GET); // Array ( [id|1@action|2] => [id] => 1 [action] => 2 )
  19. echo "
    ";
  20. echo "
    ";
  21. echo $_GET[id]."
    ";// 1
  22. echo $_GET[action];// 2
  23. ?>
复制代码

代码2,

  1. //伪静态方法二
  2. // localhost/jbxue/test.php/1/2
  3. $filename = basename($_SERVER['SCRIPT_NAME']);
  4. echo $_SERVER['SCRIPT_NAME']."
    ";// /jbxue/test.php
  5. echo $filename."
    ";// test.php
  6. if(strtolower($filename)=='test.php'){
  7. if(!emptyempty($_GET[id])){
  8. $id=intval($_GET[id]);
  9. echo $id."
    ";
  10. $action=intval($_GET[action]);
  11. echo $action."
    ";
  12. }else{
  13. $nav=$_SERVER['REQUEST_URI'];
  14. echo "1:".$nav."
    ";// /jbxue/test.php/1/2
  15. $script=$_SERVER['SCRIPT_NAME'];
  16. echo "2:".$script."
    ";// /jbxue/test.php
  17. $nav=ereg_replace("^$script","",urldecode($nav));
  18. echo $nav."
    "; // /1/2
  19. $vars=explode("/",$nav);
  20. print_r($vars);// Array ( [0] => [1] => 1 [2] => 2 )
  21. echo "
    ";
  22. $id=intval($vars[1]);
  23. $action=intval($vars[2]);
  24. }
  25. echo $id.'&'.$action;
  26. }
  27. ?>
复制代码

1 2 下一页 尾页