PHP - 正在覆盖CSV文件

时间:2016-04-05 15:36:14

标签: php csv

编辑:修正了密码问题

我正在处理一个小问题。我正在尝试写入.csv文件来存储我为crypt()输出获取的输出。我正在寻找基线安全性,因为它将在我的局域网上保存,我只想使用crypt()开始学习如何使用它。我遇到的问题是每次尝试添加新用户时都会覆盖.csv的第一行。这是代码,随意乱用它,并提前感谢大家! (哦,密码没有正确比较,我也会在整个过程中进行比较)

    <?php
    $usr = $_GET['user'];
    $pass = $_GET['pass'];
    $salt = '$2a$07$jjskdkwidjfjvjfjskalclzjohwuvnounbfonwfnwgnwlfjn';
    $c_usr = crypt($usr,$salt);
    $c_pass = crypt($pass,$salt);
    echo $usr . ' - ' . $c_usr;
    echo "<br>" . $pass . ' - ' . $c_pass;
    if (file_exists('test.csv')) {
        echo '<br>file exists';
        $csvFile = file('test.csv');
        $data = [];
        $c = 0;
        foreach ($csvFile as $line) {
            $data[] = str_getcsv($line);
            list($r_usr,$r_pwd) = split(" - ", implode(" ", $data[$c]));
            if ($r_usr === $c_usr) {
                echo "<br>user in database";
                if ($r_pwd === $c_pass) {
                    echo "<br> password correct";
                } else {
                    echo "<br> incorrect password";
                }
            } else {
                echo "user not in database";
                $data = array(
                    $c_usr . ' - ' . $c_pass
                );
                $fp = fopen('test.csv', 'w');
                foreach ( $data as $line ) {
                    $val = explode(",", $line);
                    fputcsv($fp, $val);
                }
                fclose($fp);
                echo "user now in database";
            }
            $c++;
        }
    } else {
        $data = array(
                $c_usr . ' - ' . $c_pass
        );
        $fp = fopen('test.csv', 'w');
        foreach ( $data as $line ) {
            $val = explode(",", $line);
            fputcsv($fp, $val);
        }
        fclose($fp);
    }
?>

这就是我放入菜单栏的内容:

http://localhost/?user=bob&pass=1234

0 个答案:

没有答案