0

我正在使用$_POST将数据发布到 php 文件。在那个 php 文件中,我有以下内容。

$params = array(
        'name' => "$fname",
        'email' => "$email",
        'ad_tracking' => 'test',
        'ip_address' => '$_SERVER["REMOTE_ADDR"]',
    );
    $subscribers = $list->subscribers;
    $new_subscriber = $subscribers->create($params);

$_POST使用数据定义数组中每个键的值的最佳方法是什么?的使用$_SERVER["REMOTE_ADDR"]也没有像希望的那样工作。

4

2 回答 2

1

POST variables are passed via the super global array $_POST in PHP. So in your case, this would technically work:

$params = array(
    'name' => $_POST['fname'],
    'email' => $_POST['email'],
    'ad_tracking' => 'test',
    'ip_address' => $_SERVER['REMOTE_ADDR'],
);

Your code for $_SERVER["REMOTE_ADDR"] was enclosed in single quotes, which in PHP means a verbatim string (i.e. without variable interpolation).

Btw, you should think of input filtering too - http://www.php.net/filter

To give you an example, this would perform input filtering in your current case:

$filtered = filter_input_array(INPUT_POST, array(
    'fname' => FILTER_SANITIZE_STRING,
    'email' => FILTER_VALIDATE_EMAIL,
);

Each value inside $filtered will either have a value (valid), be NULL (not present) or false (invalid).

于 2012-05-07T02:51:31.833 回答
0

关于“使用 $_SERVER["REMOTE_ADDR"] 也没有像希望的那样工作。”:单引号不评估 php 变量

$params = array(
    'name' => $_POST["fname"],
    'email' => $_POST["email"],
    'ad_tracking' => 'test',
    'ip_address' => $_SERVER["REMOTE_ADDR"],
);
$subscribers = $list->subscribers;
$new_subscriber = $subscribers->create($params);
于 2012-05-07T02:42:10.857 回答