php注册表单:语法错误

时间:2018-01-03 23:10:29

标签: php

我正在处理一个注册新用户表单,应该检查一些数据,如果数据正确将数据保存在txt文件中并重新显示表单,但我有一个'Parse错误:语法错误,意外的文件结束',我确信这是非常愚蠢的,我没有看到我需要你们的帮助。

    <?php
    session_start();
    echo "<a href = 'logout.php'>logout</a>";
?>
<!DOCTYPE html>
<html lang="en">
    <body>
    <?php
    include 'logout.php';
    include ('includes/header.php');
    ?>
        <div class="nav">
            <?php
            include ('includes/menu.php');
            ?>
        </div>
                <footer>
            <?php include ('includes/footer.php');?>
        </footer>
    </body>
</html>
<?php
function arrayKey ($array){
    reset($array);
    $arrayKey=key($array);
    return $arrayKey;
}

function checkTxt ($data){
    $errors = array('error' => '');
    $clean = array();
    $pass = false;
    if(!empty($data)){
        if(strlen($data)>30){
            $errors['error'] = 'input too long';
        }else{
            if(ctype_alpha($data) == false){
                $errors['error'] = 'Must contain only letters';
            }else{
                $clean['pass'] = $data;
                $pass = true;
            }
        }
    }if ($pass == true) {
        return $clean;
    }else {
        return $errors;
    }
}

function checkUserName ($username){
    $errors = array('error' => '');
    $clean = array();
    $pass = false;
    if(!empty($username)){
        if(strlen($username)<6){
            $errors['error'] = 'input not long enough';
        }else{
            if (!ctype_alpha($username)) {
                $errors['error'] = 'No special characters allowed';
            }else{
                $clean['pass'] = $username;
                $pass = true;
            }
        }
    }if ($pass == true) {
        return $clean;
    }else {
        return $errors;
    }
}

function checkPassword ($password){
    $trimPass = trim($password);
    $errorsPass = array('error' => '');
    $clean = array();
    $pass = false;
    if(!empty($trimPass)){
        if(strlen($trimPass)<6){
            $errorsPass['error'] = 'input not long enough';
        }else{
            if (!ctype_alpha($trimPass)) {
                $errorsPass['error'] = 'No special characters allowed';
            }else{
                $clean['pass'] = $password;
                $pass = true;
            }
        }
    }if ($pass == true) {
        return $clean;
    }else {
        return $errorsPass;
    }
}
}
$output = '';
$form_is_submitted = false;
$errors_detected = false;

$cleanArray = array();
$errorsArray = array();
$dataArray = array();

if(isset($_POST['SUBmit'])){
    $form_is_submitted = true;
    if (isset($_POST['title'])){
        $cleanArray['title'] = $_POST['title'];
    }

    if (isset($_POST['fname'])) {
        $fname = $_POST['fname'];
        $trimFn = trim($fname);
        $lowerFn = strtolower($trimFn);
        $dataArray = checkTxt($lowerFn);
        $keyData = arrayKey($dataArray);
        if ($keyData == 'pass'){
            $cleanArray['fname'] = $dataArray[$keyData];
        }else{
            $errorsArray['fname'] = $dataArray[$keyData];
            $errors_detected = true;
        }
    }
    $dataArray = array();

    if (isset($_POST['lname'])) {
        $lname = $_POST['lname'];
        $trimLn = trim($fname);
        $lowerLn = strtolower($trimLn);
        $dataArray = checkTxt($lowerLn);
        $keyData = arrayKey($dataArray);
        if ($keyData == 'pass'){
            $cleanArray['lname'] = $dataArray[$keyData];
        }else{
            $errorsArray['lname'] = $dataArray[$keyData];
            $errors_detected = true;
        }
    }
    $dataArray = array();

    if (isset($_POST['email'])) {
        if (empty($_POST['email'])){
            $errors_detected = true;
            $errorsArray = 'Email not entered';
        }else {
            $emailGet = $_POST['email'];
            $emailTrim = trim($emailGet);
            if (!filter_var($emailTrim, FILTER_VALIDATE_EMAIL)){
                $errorsArray['email'] = 'Invalid email format';
                $errors_detected = true;
            }else{
                $cleanArray['email'] = $emailTrim;
            }
        }
    }

    if (isset($_POST['username'])) {
        $username = $_POST['username'];
        $trimUsnm = trim($username);
        $lowerUsnm = strtolower($trimUsnm);
        $dataArray = checkUserName($lowerUsnm);
        $keyData = arrayKey($dataArray);
        if ($keyData == 'pass'){
            $cleanArray['username'] = $dataArray[$keyData];
        }else{
            $errorsArray['username'] = $dataArray[$keyData];
            $errors_detected = true;
        }
    }
    $dataArray = array();

    if (isset($_POST['password'])) {
        $password = $_POST['password'];
        $trimPass = trim($password);
        $dataArray = checkPassword($trimPass);
        $keyData = arrayKey($dataArray);
        if ($keyData == 'pass'){
            $cleanArray['password'] = $dataArray[$keyData];
        }else{
            $errorsArray['password'] = $dataArray[$keyData];
            $errors_detected = true;
        }
    }

if($form_is_submitted == true && $errors_detected == false){
    $dir = '/home/sbau01/public_www/php/fma/data';
    if (is_dir($dir)){
        $handleDir = opendir('/home/sbau01/public_www/php/fma/data');
        while(false !==($file = readdir($handleDir))){
            $path = "/home/sbau01/public_www/php/fma/data/".$file;
            if(is_file($path)){
                $handle =  fopen($path, 'a');
                $text = $cleanArray['fname'].' '.$cleanArray['lname'].' '.$cleanArray['email'].' '.$cleanArray['username'].' '.$cleanArray['password']. PHP_EOL;
                $result = fwrite($handle, $text);
                if($result == false){
                    $formsErorrs = '<p>Oops! data not written</p>';
                }
                else{
                    $formsErorrs = '<p>Thank you for register with us. Yours details has been saved </p>';
                    $fullName = htmlentities($cleanArray['fname']).' '.htmlentities($cleanArray['lname']);
                }
                fclose($handle);
            }
            else{
                $formsErorrs= '<p>Oops!!! file  has not been found >>>  '.$path.'</p>';
            }
        }closedir($handleDir);
        $output= '<p>Registration form has been submitted</p>';
    }else{
       $formsErorrs = '<p>Oops, the file has NOT been found!!!</p>'; 
    }
}
else{
    $self = htmlentities($_SERVER['PHP_SELF']);
    $errmsg = '';
    if($form_is_submitted === true){
        foreach($errorsArray as $error){
            $errmsg = $errmsg.'<p>'.£error.'</p>';                  
        }   
    }
    if (isset($cleanArray['fname'])) {
        $firstN = htmlentities($cleanArray['fname']);
    } 
    else {
        $firstN = '';
    }
    if (isset($cleanArray['lname'])) {
        $lastN = htmlentities($cleanArray['lname']);
    } 
    else {
        $lastN = '';
    }
    if (isset($cleanArray['email'])) {
        $email = htmlentities($cleanArray['email']);
    } 
    else {
        $email = '';
    }
    if (isset($cleanArray['username'])) {
        $userN = htmlentities($cleanArray['username']);
    } 
    else {
        $userN = '';
    }
    if (isset($cleanArray['password'])) {
        $pass = htmlentities($cleanArray['password']);
    } 
    else {
        $pass = '';
    }
    $output = '<form action="'.$self.'" method="post">
        <fieldset>
            <legend>Details:</legend>
                <label for="tl">Title:</label>
                    <select name="title">
                        <option value="Mr">Mr.</option>
                        <option value="Miss">Miss</option>
                        <option value="Mrs">Mrs.</option>
                    </select>
                <div>
                    <label for="fn">First Name:</label>
                    <input type="text" name="fname" id="fn" value="' . $firstN . '" />
                </div>
                <div>
                    <label for="ln">Surame:</label>
                    <input type="text" name="lname" id="ln" value="' . $lastN . '" />
                </div>
                <div>
                    <label for="email">Email:</label>
                    <input type="text" name="email" id="email" value="' . $email . '" />
                </div>
                <div>
                    <label for="un">Username:</label>
                    <input type="text" name="username" id="un" value="' . $userN . '" />
                </div>
                <div>
                    <label for="pw">Password:</label>
                    <input type="password" name="password" id="pw" value="'.$pass.'"/>
                </div>
                <input type="submit" name="SUBmit" value="SUBMIT" />
        </fieldset>
    </form>';
}
$newUser = array ($cleanArray, $errorsArray);
echo $output;
echo $errmsg;
?>

1 个答案:

答案 0 :(得分:0)

第104行的代码缺少大括号。

 if(isset($_POST['SUBmit'])){
    $form_is_submitted = true;
    if (isset($_POST['title'])){
        $cleanArray['title'] = $_POST['title'];
    }