棋子 发表于 2008-9-10 01:37:53

Mod Rewrite Made Easy

<?php

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

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

Example Usage:
-------------------------------------------------------------------------------


In the .htaccess you have:

RewriteEngine On
RewriteBase /
RewriteRule ^somepage/(.*)\.html somepage.php?rewrite=$1

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

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

<?php

if( $_GET['rewrite'])
{
        $request = $_GET['rewrite'];
        mod_rewrite( $request, '/', '-' );
# if you have register_globals enables uncomment the following
#        extract( $_GET, EXTR_OVERWRITE );
}

?>

What happenes when a user clicks the link:

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

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

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

viola !
mod_rewite made relatively easy




*/

function mod_rewrite( $request, $array_delim, $pair_delim )
{
        global $_GET, $HTTP_GET_VARS, $_REQUEST;
        $value_pairs = explode( $array_delim, $request );
        $make_global = array();

        foreach( $value_pairs as $pair )
        {
                $pair = explode( $pair_delim, $pair );
                $_GET[$pair] = $pair;               
                $_REQUEST[$pair] = $pair;               
                $HTTP_GET_VARS[$pair] = $pair;               
        }
}

?>

tonybuy 发表于 2008-9-11 00:07:28

PHP,,,,,,,,,,,,,,,,,,,,晕眩。
页: [1]
查看完整版本: Mod Rewrite Made Easy