Php-读写-注册
我正在为一家网上商店建立一个基本的注册和登录系统 我是一个完全的php新手,正在努力让我的头脑了解php的阅读方面。我的注册验证良好,正在写入正确的文件。每个用户都需要注册一个唯一的电子邮件地址,但我不确定如何进行此检查Php-读写-注册,php,file,read-write,Php,File,Read Write,我正在为一家网上商店建立一个基本的注册和登录系统 我是一个完全的php新手,正在努力让我的头脑了解php的阅读方面。我的注册验证良好,正在写入正确的文件。每个用户都需要注册一个唯一的电子邮件地址,但我不确定如何进行此检查 <?php // form filled out correctly // assign all POSTed variables $fname = $_POST['fname']; $lname = $_POST['lname']; $address = $_POST[
<?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>";
?>
如果我是你,我不会将数据存储在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的更多信息
使用XML在项目中应该考虑很多优点。但是,如果你不想使用它。若要将文件读取到数组,其中数组元素是一行,则应使用函数file()
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;
}