繁体   English   中英

如何使用PHP检查用户名和密码

[英]How to check username and password with PHP

我已经制作了两种形式-一种用于登录(如果用户已经注册),另一种用于注册。 用户日期我以这种方式保存到date.txt文件中,名称:Peter,登录名:pet123,通过:12345 | 名称:John,登录名:joh123,密码:54321等,我尚未使用DB。 就这个

<form action="#" method="post" id="user_new" class="user_new">`<br/>

        <label for="name">Name:</label><input type="text" name="name" id="name" required/>
        <label for="username">Username:</label><input type="text" name="username" id="username" required />
        <label for="password">Password:</label><input type="password" name="password" id="password"  required/>
        <input type="image" src="img/singUp.png" name="signUp" />
</form>  

第二种登录形式

<form action="#" method="post" id="login" class="login">

        <label for="username">Username:</label><input type="text" name="username" id="username" required="required" />
        <label for="password">Password:</label><input type="password" name="password" id="password" required="required"/>
        <input type="image" src="img/singIn.png" name="signIn" />
        <a href="registration.php" class="custom_link">Registrate Now</a>
</form>   

和php登录

 if (isset($_POST['singIn'])):

        $users = file('date.txt');
        foreach ($users as $key => $value)
        {
            $user = explode('|', $value);
            $use  = explode(',', $user);
            $us    = explode(':', $use);
            unset($us['name'], $us['username'], $us['password']);
        }
        if (!in_array(trim($_POST['username']), $us)) die('There is no user with this username');
endif;

但是这段代码行不通吗? 怎么了 第二个问题,为什么我写

if(isset($_POST["signUp"])): 

      $name = $_POST["name"]; 
      $uname = $_POST["username"];
      $upass = $_POST["password"];
      $users = fopen("date.txt", "a") or die("Couldn't open date.txt for add record"); 
      $record ="name:".$name . ",username:" .$uname . ",password:" . $upass . "|";     
      fwrite($users,$record) or die ("Couldn't add record");
      fclose($users);
endif;  

代码不起作用,但是在没有isset的情况下起作用。 在第二种情况下,我得到了双重记录

您没有使用explode正确处理字符串和数组。 explode拆分字符串并返回组成字符串的数组,因此$user = explode('|', $value)生成一个数组; 在字符串数组上使用explode (例如explode(',' $user) )将不起作用。

您需要做的是引入嵌套循环。 像这样的东西:

foreach ($users as $line)
{
    // Iterate over all the records in the file.
    $records = explode('|', $line);
    foreach ($records as $record)
    {
        // Iterate over all the fields in this record and extract the user's details.
        $fields = explode(',', $record);
        $user = array();
        foreach ($fields as $field)
        {
            list($key, $value) = explode(':', $field);
            $user[$key] = $value;
        }

        // Now check whether this user matches the details in $_POST.
        // ...
    }
}

但是,我建议不要使用|分隔记录| 在文件中,每行只有一条记录。 由于file将自动为您提供file中所有记录的数组(每行一个),因此解析起来会更加简单。

您最大的问题是拼写。 此代码中有多个实例带有“ singIn”或“ signIn”以及“ singUp”或“ signUp”。 只要使用相同,使用哪个都无所谓。

<input type="image" src="img/singIn.png" name="signIn" />是您的输入name =“ signIn ”。

if (isset($_POST['singIn'])):是您的帖子值' singIn ',它们必须匹配。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM