Re: Re: create htaccess.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



adwinwijaya wrote:
> www.mysite.com/article/file/19911/year/2004
> means: www.mysite.com/article.php?file=19911&year=2004 right ?
> :)
>
> and i have to put this script on my article.php, is it right ?
>
> $pi   = $_SERVER['PATH_INFO']; // Grab PATH_INFO
> $pi   = substr($pi, 1);        // Remove the opening slash
> $args = split('/', $pi);       // Create array of arguments
> $f    = $args[0];              // Grab first argument

Using split here is a bad habit.  explode would work fine, and is faster. 
Speed is probably irrelevant in this case, as you are only doing it once
per script.  But bad habits are bad habits.

Also, you might want to use:

www.mysite.com/article/file=19911/year=2004/

Then you can do:
$args = explode('/', $pi);
$PATH = '';
while (list(, $arg) = each($args)){
  $keyvalue = explode('=', $arg);
  switch(count($keyvalue)){
    case 0:
      # do nothing for '//' in URL
    break;
    case 1:
      $_PATH[$arg] = ''; //The arg is there, but has no specific value
      $PATH .= "$arg/";
    break;
    default:
      $variable = $keyvalue[0];
      unset($keyvalue[0]);
      $_PATH[$variable] = implode('=', $keyvalue);
    break;
  }
}

Here's why I find this useful:

1. Instead of relying on position to know what variable is what, I can put
my URL parts in any order.

2. I collect all elements with no specific value in $PATH -- This allows
me to more naturally define a directory (EG to a JPEG) in my URL:
http://example.com/scale_image/width=50/height=50/path/to/my.jpeg
You have to watch out for bad data in $PATH, of course, but I always use
relative pathnames, and check for ".." etc.

3. I allow for the case that an '=' is part of the value.

4. I store all my PATH_INFO inputs in a $_PATH array which is then treated
the same as $_POST or $_GET etc -- It's untrusted data, coming from the
outside world.

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux