Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Getting Dots to Work in PHP and GET / POST / COOKIE Variable Names

DZone's Guide to

Getting Dots to Work in PHP and GET / POST / COOKIE Variable Names

· Web Dev Zone ·
Free Resource

Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.

One of the oldest and ugliest relics of the register_globals era of PHP are the fact that all dots in request variable names gets replaced with “_”. If your variable was named “foo.bar”, PHP will serve it to you as “foo_bar”. You cannot turn this off, you cannot use extract() or parse_str() to avoid it and you’re mostly left out in the dark. Luckily the QUERY_STRING enviornment (in _SERVER if you’re running mod_php, etc) contains the raw string, and this string contains the dots.

The following “”parser”" is a work in progress and does currently not support the array syntax for keys that PHP allow, but it solves the issue for regular vars. I will try to extend this later on to do actually replicate the functionality of the regular parser.

Here’s the code. No warranties. Ugly hack. You’re warned. Leave a comment if you have any good suggestions regarding this (.. or know of an existing library doing the same..).

    function http_demolish_query($queryString)
    {
        $result = array();
        $segments = explode("&", $queryString);
     
        foreach($segments as $segment)
        {
            $parts = explode('=', $segment);
     
            $key = urldecode(array_shift($parts));
            $value = null;
     
            if ($parts)
            {
                $value = urldecode(join('=', $parts));
            }
     
            $result[$key] = $value;
        }
     
        return $result;
    }

(OK, that’s not the real function name, but it’s aptly named to be the nemesis of http_build_query)

Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}