PYTHON $_POST

is this article helpful?
|
Python replacement for PHP's $_POST [ edit | history ]
# coding: utf-8

from urllib.parse import unquote_plus
import re


def get_http_vars(text, encoding='utf-8'):
    """Gets PHP-like HTTP superglobal variable.

    :arg str text: query string
    :arg str encoding: input encoding
    :returns: dict
    """
    for pair in text.split('&'):
        pair = pair.split('=', 1)
        key = unquote_plus(pair[0], encoding=encoding)
        if key == '' or key[0] == '[':
            continue
        value = unquote_plus(pair[1], encoding=elf._encoding) if len(pair) > 1 else ''
        m = re.search(r'^([^\[]+)\[(.*)\]$', key)
        if not m:
            data[key] = value
            continue
        subkeys = [m.group(1)] + m.group(2).split('][')
        last_index = len(subkeys) - 1
        subdata = data
        for i in range(last_index):
            if subkeys[i] == '':
                subvalue = [] if subkeys[i + 1] == '' else {}
                subdata.append(subvalue)
            else:
                if subkeys[i] in subdata:
                    subvalue = subdata[subkeys[i]]
                else:
                    subvalue = [] if subkeys[i + 1] == '' else {}
                    subdata[subkeys[i]] = subvalue
            subdata = subvalue
        if subkeys[last_index] == '':
            subdata.append(value)
        else:
            subdata[subkeys[last_index]] = value
    return data

PHP $_POST

PHP original manual for $_POST [ show | php.net ]

$_POST

$HTTP_POST_VARS [deprecated]

$_POST -- $HTTP_POST_VARS [deprecated]HTTP POST variables

Description

An associative array of variables passed to the current script via the HTTP POST method.

$HTTP_POST_VARS contains the same initial information, but is not a superglobal. (Note that $HTTP_POST_VARS and $_POST are different variables and that PHP handles them as such)

Changelog

Version Description
4.1.0 Introduced $_POST that deprecated $HTTP_POST_VARS.

Examples

Example #1 $_POST example

<?php
echo 'Hello ' htmlspecialchars($_POST["name"]) . '!';
?>

Assuming the user POSTed name=Hannes

The above example will output something similar to:

Hello Hannes!

Notes

Note: This is a 'superglobal', or automatic global, variable. This simply means that it is available in all scopes throughout a script. There is no need to do global $variable; to access it within functions or methods.