Home >Backend Development >PHP Tutorial >How Can I Work Around PHP's Automatic Replacement of Dots in Request and Cookie Names?

How Can I Work Around PHP's Automatic Replacement of Dots in Request and Cookie Names?

Linda Hamilton
Linda HamiltonOriginal
2024-12-26 19:33:19177browse

How Can I Work Around PHP's Automatic Replacement of Dots in Request and Cookie Names?

PHP's Auto-Replacement of Dots in Request and Cookie Names: Is There a Solution?

PHP automatically replaces dots (.) in request and cookie names with underscores (_), a behavior that can be problematic.

Why does PHP do this? According to PHP.net, dots are not valid characters in PHP variable names, as demonstrated in the example below:

$varname.ext;  /* invalid variable name */

To avoid this parsing issue, PHP replaces dots with underscores.

Unfortunately, there is no direct way to disable this behavior. The solution is to manually convert the underscores back to dots in your PHP script using string manipulation functions like str_replace:

<?php
$request_uri = $_SERVER['REQUEST_URI'];
$get_vars = $_GET;

// Convert underscores back to dots in GET variables
foreach ($get_vars as $key => $value) {
  $key = str_replace('_', '.', $key);
  $get_vars[$key] = $value;
}

// Echo the modified GET variables
print_r($get_vars);
?>

In addition to dots, PHP also replaces the following characters with underscores:

  • chr(32) ( ) (space)
  • chr(46) (.) (dot)
  • chr(91) ([) (open square bracket)
  • chr(128) - chr(159) (various)

The above is the detailed content of How Can I Work Around PHP's Automatic Replacement of Dots in Request and Cookie 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