将数据记录为txt文件中的数组

时间:2014-07-31 12:53:58

标签: php

我需要在PHP中创建非常简单的注册/登录系统。用户详细信息必须存储在txt文件中的数组中。由于某些原因,即使PHP没有显示任何错误细节也不会保存到txt文件。任何提示?

$fullname='';
                     $email ='';
                     $username ='';
                     $password = '';
                     $error = '';

                    $form_is_submitted = false;
                    $errors_detected = false;
                    $clean = array();
                    $errors = array();
                    if (isset($_POST['submit'])) {
                    $form_is_submitted = true;
                    if (ctype_alnum ($_POST['fullname'])) {
                    $clean['fullname'] = $_POST['fullname'];
                    } else {
                    $errors_detected = true;
                    $errors[] = 'Please enter your Full Name!';
                    }
                    if (ctype_alnum ($_POST['email'])) {
                    $clean['email'] = $_POST['email'];
                    } else {
                    $errors_detected = true;
                    $errors[] = 'You have enter an invalid e-mail address. Please, try again!';
                    }
                    if (ctype_alnum ($_POST['username'])) {
                    $clean['username'] = $_POST['username'];
                    } else {
                    $errors_detected = true;
                    $errors[] = 'Please enter your user name!';
                    if (ctype_alnum ($_POST['password'])) {
                    $clean['password'] = $_POST['password'];
                    } else {
                    $errors_detected = true;
                    $errors[] = 'Please enter a valid password!';
                    }
                    }



                if ($form_is_submitted === true
                    && $errors_detected === false) {
                    $fp = fopen('filewriting.txt', 'w');
                    fwrite($fp, print_r($clean, TRUE));
                    fclose($fp);
                    } else {
                    echo $errors;
                    }

2 个答案:

答案 0 :(得分:1)

您的代码存在一些问题。

缺少大括号
if (isset($_POST['submit'])) {$form_is_submitted = true;

所以它需要读作

if (isset($_POST['submit'])) {
$form_is_submitted = true;
}

您正在使用ctype_alnum因此,当涉及到电子邮件地址时,@和点不会计为字母数字字符a-z A-Z 0-9;要么删除它if(ctype_alnum ($_POST['email'])),证明它是成功的测试。
您还可以使用其他功能,例如FILTER_VALIDATE_EMAIL

我引用PHP manual

  

返回值
  如果文字中的每个字符都是字母或数字,则返回 TRUE ,否则返回 FALSE

这个区块有一个错位的大括号

                if (ctype_alnum ($_POST['username'])) {
                $clean['username'] = $_POST['username'];
                } else {
                $errors_detected = true;
                $errors[] = 'Please enter your user name!';
                if (ctype_alnum ($_POST['password'])) {
                $clean['password'] = $_POST['password'];
                } else {
                $errors_detected = true;
                $errors[] = 'Please enter a valid password!';
                }
                }

哪个应该是

                if (ctype_alnum ($_POST['username'])) {
                $clean['username'] = $_POST['username'];
                } else {
                $errors_detected = true;
                $errors[] = 'Please enter your user name!';
                } // was missing

                if (ctype_alnum ($_POST['password'])) {
                $clean['password'] = $_POST['password'];
                } else {
                $errors_detected = true;
                $errors[] = 'Please enter a valid password!';
                }
                // } // was misplaced - commented out to show you

否则它不会将密码(作为数组的一部分)写入文件。

另外这个$error = '';应该"可能"读为$errors = '';,但没有阻止它将数据写入文件。

对于Array消息,请从[]

的所有实例中删除方括号$errors[]

答案 1 :(得分:-1)

我认为

fwrite($fp, print_r($clean, TRUE));

应该是

fwrite($fp, $clean, TRUE);

file_put_contents($fp, $clean);