dpl74687 2017-02-08 17:09
浏览 64
已采纳

从CSV创建登录页面

I am attempting to create a login page for my website. I have it set up so the user can create an account and these credentials are saved to a csv, saved on my ftp. (All the HTML and CSS is functional) I would like the system to work as follows:

1. From login page the user enters their credentials.
2. The CSV is searched, when the email is found the inputted password is compared with the corresponding password in the CSV.
3. If they match then another page is opened/If they don't match an error is displayed.
Here is the CSV:

Test@gmail.com,password1
Test2@gmail.com.password2

Here is the php which writes to the CSV:

<?php
$filename = $_POST['filename'];
foreach($_POST as $name => $value)
{
IF ($value != "Submit" and $value !=$filename)
{
$messagedisplay = $messagedisplay . $name. ": " . $value . "<BR>";
$filedata = $filedata . $value . ",";
}
}
$filedata = rtrim($filedata,",");
$filedata = $filedata . PHP_EOL;
$fs = fopen($filename,a);
fwrite($fs,$filedata);
fclose($fs);
$messagedisplay = "Your account has been created, please return to the main website and login.";
print $messagedisplay;
?>


Any ideas on how I would check the CSV to see if a) the email exists in the CSV and b) check the passwords match, subsequently redirecting to another page. Thanks.

  • 写回答

1条回答 默认 最新

  • doujiao2014 2017-02-08 17:53
    关注

    In your case you could slurp the csv into an array. Then it's as simple as iterating through the array until you find a match.

    <?php
    $credentials = [
        ['foo', 'jubblies'],
        ['bar', 'jangles']
    ];
    
    $check_credentials = function($username, $password) use ($credentials) {
        foreach($credentials as $credential)
            if($credential[0] == $username && $credential[1] == $password)
                return true;
    
        return false;
    };
    
    var_dump($check_credentials('foo', 'jiblets'));
    var_dump($check_credentials('foo', 'jubblies'));
    var_dump($check_credentials('ace', 'detective'));
    

    Output

    boolean false
    boolean true
    boolean false
    

    Reading your credentials from a csv file into an array (similar format as above) could be accomplished something like this:

    function get_credentials_from_file($path) {
        $fp = fopen($path, 'r');
        while ($line = fgetcsv($fp)) {
            $lines[] = $line;
        }
        fclose($fp);
    
        return $lines;
    }
    
    $credentials = get_credentials_from_file('/tmp/file.csv');
    

    See also fputcsv, for csv writing.

    Take care when storing user data.

    If you end up reading and writing from/to a csv or text file, you'll have to manage file locks etc. It could well be easier to use a database.

    See: Php's password_hash and password_verify to avoid storing plain text passwords.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了