Php登录/注册数据存储在txt中

时间:2013-05-12 19:44:15

标签: php login registration

我正在尝试创建一个简单的登录/ reg,但登录不起作用,只能使用我的txt文件的最后一行,可能是因为其他行包含每个末尾的'\ n'但它之前有效,我不知道出了什么问题......为了让它发挥作用,我该怎么做?这是我的代码:

的login.php:

<?php
if(isset($_POST["name"]) && isset($_POST["password"])){
    $file = fopen('data.txt', 'r');
    $good=false;
    while(!feof($file)){
        $line = fgets($file);
        $array = explode(";",$line);
        if($array[0]==$_POST["name"] && $array[1]==$_POST["password"]){
            $good=true;
            break;
        }
    }

    if($good){
        $message="Welcome";
    }else{
        $message="Try again";
    }
    include 'message.html';
fclose($file);
}else{
    include 'login.html';
}
?>

reg.php:

<?php
if(isset($_POST["name"]) && isset($_POST["password"])){
    $f=fopen("data.txt","a");
    fputs($f,$_POST["name"].";".$_POST["password"]."\r\n");
    fclose($f);
}else{
    include 'reg.html';
}
?>

这就是data.txt的样子:

Viktor;1234
Eve;12345
Cathlin;12356

2 个答案:

答案 0 :(得分:1)

使用trim()去除字符串开头和结尾的空格。 http://php.net/manual/en/function.trim.php

while(!feof($file)){
    $line = fgets($file);

    list($user, $pass) = explode(';', $line);

    if(trim($user) == $_POST['name'] && trim($pass) == $_POST['password']){
        $good=true;
        break;
    }
}

答案 1 :(得分:0)

我创建了一个用于PHP注册和登录的演示程序,并进行了很好的测试。

这是reg html:

<html>
<head>
<title> Reg Page   </title>
</head>
<body>
<form action="" method="post">
    <table width="200" border="0">
  <tr>
    <td>  UserName</td>
    <td> <input type="text" name="user" > </td>
  </tr>
  <tr>
    <td> PassWord  </td>
    <td><input type="password" name="pass"></td>
  </tr>
  <tr>
    <td> <input type="submit" name="reg" value="REG"></td>
    <td></td>
  </tr>
</table>
</form>
</body>
</html>

注册php如下:

<?php
if(isset($_POST["user"]) && isset($_POST["pass"]))
{
    // check if user exist.
    $file=fopen("data.txt","r");
    $finduser = false;
    while(!feof($file))
    {
        $line = fgets($file);
        $array = explode(";",$line);
        if(trim($array[0]) == $_POST['user'])
        {
            $finduser=true;
            break;
        }
    }
    fclose($file);

    // register user or pop up message
    if( $finduser )
    {
        echo $_POST["user"];
        echo ' existed!\r\n';
        include 'reg.html';
    }
    else
    {
        $file = fopen("data.txt", "a");
        fputs($file,$_POST["user"].";".$_POST["pass"]."\r\n");
        fclose($file);
        echo $_POST["user"];
        echo " registered successfully!";
    }
}
else
{
    include 'reg.html';
}
?>

登录表单:

<html>
<head>
<title> Login Page   </title>
</head>
<body>
<form action="" method="post">
    <table width="200" border="0">
  <tr>
    <td>  UserName</td>
    <td> <input type="text" name="user" > </td>
  </tr>
  <tr>
    <td> PassWord  </td>
    <td><input type="password" name="pass"></td>
  </tr>
  <tr>
    <td> <input type="submit" name="login" value="LOGIN"></td>
    <td></td>
  </tr>
</table>
</form>
</body>
</html>

登录php:

<?php  session_start(); ?>  // session starts with the help of this function 

<?php

if(isset($_SESSION['use']))   // Checking whether the session is already there or not if 
                              // true then header redirect it to the home page directly 
 {
    header("Location:home.php"); 
 }
else
{
    include 'login.html';
}

if(isset($_POST['login']))   // it checks whether the user clicked login button or not 
{
     $user = $_POST['user'];
     $pass = $_POST['pass'];

    if(isset($_POST["user"]) && isset($_POST["pass"])){
    $file = fopen('data.txt', 'r');
    $good=false;
    while(!feof($file)){
        $line = fgets($file);
        $array = explode(";",$line);
    if(trim($array[0]) == $_POST['user'] && trim($array[1]) == $_POST['pass']){
            $good=true;
            break;
        }
    }

    if($good){
    $_SESSION['use'] = $user;
        echo '<script type="text/javascript"> window.open("home.php","_self");</script>';  
    }else{
        echo "invalid UserName or Password";
    }
    fclose($file);
    }
    else{
        include 'login.html';
    }

}
?>

我将源代码放在here上作为参考。