Home >Backend Development >PHP Tutorial >How Can I Prevent PHP from Replacing Dots with Underscores in GET, POST, and COOKIE Variable Names?

How Can I Prevent PHP from Replacing Dots with Underscores in GET, POST, and COOKIE Variable Names?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-11 01:59:09815browse

How Can I Prevent PHP from Replacing Dots with Underscores in GET, POST, and COOKIE Variable Names?

PHP: Prevent Character Replacement in GET, POST, and COOKIE Names

PHP automatically replaces dots in incoming variable names with underscores. This behavior can be undesirable in some cases. How can we disable it?

Why PHP Replaces Dots

According to PHP.net, dots are not valid characters in variable names. The parser would interpret "varname.ext" as a variable named "varname" followed by the concatenation operator and the string "ext", which is not the intended result.

Alternatives

Despite attempts to prevent this behavior, PHP's parsing mechanism makes it necessary. However, there are ways to work around it:

  • POST Variables: Use str_replace to replace underscores with dots before accessing POST variables:
$_POST['x.y'] = str_replace('_', '.', $_POST['x_y']);
  • GET and COOKIE Variables: Use a global variable substitution pattern in your .htaccess file to replace underscores with dots before PHP parses the request:
RewriteEngine On
RewriteRule .* - [E=DOT:_]

The above is the detailed content of How Can I Prevent PHP from Replacing Dots with Underscores in GET, POST, and COOKIE Variable Names?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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