网络实验室

 找回密码
 注册账户
查看: 1527|回复: 3

Mod Rewrite Made Easy

[复制链接]
无心的棋子 发表于 2008-9-10 01:37:53 | 显示全部楼层 |阅读模式
  1. <?php

  2. /*
  3. Rewrite Function
  4. This function accepts an input and parses it into the $_GET, $_REQUEST, and $HTTP_GET_VARS Globals
  5. If your script relies on register_globals being enbaled
  6. call         extract( $_GET, EXTR_OVERWRITE );
  7. after runnng this function to reregister the $_GET array pair values in to the global namespace

  8. input:
  9.         $request = the string variable you need parsed into the global namespace
  10.         $array_delim = the array pair value deliminator
  11.         $pair_delim = the deliminator that seperates pair names from pairs values
  12.        
  13. returns:
  14.         void or no return value;

  15. Example Usage:
  16. -------------------------------------------------------------------------------


  17. In the .htaccess you have:

  18. RewriteEngine On
  19. RewriteBase /
  20. RewriteRule ^somepage/(.*)\.html somepage.php?rewrite=$1 [L]

  21. Your links:
  22. Your original link url was:         http://yoursite.com/somepage.php?id=20&name=funny
  23. You change your url to:        http://yoursite.com/somepage/id-20/name-funny.html

  24. In the script somepage.php you put this at the top:

  25. <?php

  26. if( $_GET['rewrite'])
  27. {
  28.         $request = $_GET['rewrite'];
  29.         mod_rewrite( $request, '/', '-' );
  30. # if you have register_globals enables uncomment the following
  31. #        extract( $_GET, EXTR_OVERWRITE );
  32. }

  33. ?>

  34. What happenes when a user clicks the link:

  35. User sends request for "somepage/id-20/name-funny.html"
  36. ModRewrite Engine is on and request matches pattern matches "somepage/"
  37. ModRewrite engine changes the request to somepage.php?rewrite=id-20/name-funny
  38. The PHP engine is called and the script is run
  39. the $_GET['rewrite'] is processed by the mod_rewrite function

  40. the mod_rewrite function changes this value "id-20/name-funny" into
  41. $_GET['id'] = '20';
  42. $_GET['name'] = 'funny';

  43. then if you depend on register_globals being on ( read converting an old script )
  44. you call this:
  45.         extract( $_GET, EXTR_OVERWRITE );
  46.        
  47. right after the mod_rewrite function to put all the new $_GET variables into the global name space

  48. viola !
  49. mod_rewite made relatively easy




  50. */

  51. function mod_rewrite( $request, $array_delim, $pair_delim )
  52. {
  53.         global $_GET, $HTTP_GET_VARS, $_REQUEST;
  54.         $value_pairs = explode( $array_delim, $request );
  55.         $make_global = array();

  56.         foreach( $value_pairs as $pair )
  57.         {
  58.                 $pair = explode( $pair_delim, $pair );
  59.                 $_GET[$pair[0]] = $pair[1];               
  60.                 $_REQUEST[$pair[0]] = $pair[1];               
  61.                 $HTTP_GET_VARS[$pair[0]] = $pair[1];               
  62.         }
  63. }

  64. ?>
复制代码
tonybuy 发表于 2008-9-11 00:07:28 | 显示全部楼层
PHP,,,,,,,,,,,,,,,,,,,,晕眩。
您需要登录后才可以回帖 登录 | 注册账户

本版积分规则

黑屋|存档|手机|网络实验室 本站服务器由美国合租以及IDCLayer国际数据提供!!!

GMT+8, 2024-4-26 04:16 , Processed in 0.180566 second(s), 9 queries , Gzip On, Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表