0

我正在为在线商店构建一个基本的注册和登录系统。

我是一个完整的 php 新手,正在努力理解 php 的阅读方面。我的注册验证正常并且正在写入正确的文件。每个用户都需要使用唯一的电子邮件地址进行注册,但我不确定如何进行此检查。

<?php
// form filled out correctly
// assign all POSTed variables
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$address = $_POST['address'];
$email = $_POST['email'];
$sex = $_POST ['sex'];
$age = $_POST ['age'];
$pwd = $_POST ['pwd'];

// form validation such as all fields are filed in and email correct format

$fp = fopen("user.txt", "r");
//Output a line of the file until the end is reached

//combine string for output
$output_string = "Firstname: " .$fname ."\t"
."Lastname: " .$lname ."\t"
."Address: " .$address ."\t"
."Email: " .$email ."\t"
."Sex: " .$sex ."\t"
."Age: " .$age ."\t"
."Password: " .$pwd ."\n";

//write user to user.txt
$fp = fopen ("user.txt", "a");
fwrite ($fp, $output_string);
fclose($fp); 
echo "<p> Your registration was successful! </p>";
echo "<h3> <a href=\"login.php\"> Login here </h3>";
?>
4

1 回答 1

1

如果我是你,我不会将数据存储在 txt 中。我会使用数据库或 XML 文件。

xml结构示例:

<?xml version="1.0" encoding="UTF-8" ?>
<users>
   <user>
     <firstname>John</firstname>
     <lastname>Doe</lastname>
     <adress>aaa 23 street</adress>
     <email>aaa@gmail.com</email>
     <sex>male</sex>
     <age>1987</age> <!-- better to use year of birth -->
     <password>dkfzlkxcvzxkcv</age> <!-- password's hash -->
    </user>
</users>

使用这种文件结构,您可以使用简单的 xml

function findEmail()
{

 if (file_exists('users.xml'))
 {
   $xml = simplexml_load_file('test.xml');
   foreach ($xml->users->user as $user)
   { 
      if($user->email == $_POST['email']) return true;
   }  
 }
 return false;
}

阅读有关简单 XML的更多信息-> http://www.php.net/manual/en/book.simplexml.php 使用 XML 有很多优点,您应该在项目中考虑它。但是,如果您不想使用它。要将文件读取到数组元素为一行的数组中,您应该使用函数file()

于 2013-05-23T06:31:14.163 回答