1

任何比我更有经验的人都可以看看这个页面并找出为什么 php 验证不起作用。这是我第一次用 PHP 编程,我通过 PHP lint 运行它,代码中没有致命错误,只需要在用户尝试提交表单时获取错误消息输出。JS 验证工作正常,红色错误消息是 jQuery 验证,所以最好关闭 JS。

谢谢

http://www.cambrianvacation.co.uk/vpc/register1.php

编辑这里的代码

//form processing
//import form info
//run this code when page submits test with js turned off
 if (isset($_POST["submit"])) {
$email = $_POST['email'];
$password = $_POST['password'];
$confirm_password = $_POST['confirm_password'];
$myArray = $errors();
function validate_firstname(){
//validate here, 
if(!isset($_POST['firstname'])){
     $errors = print 'Please enter your firstname';
    }
else if(strlen($_POST['firstname']) <=2){
     $errors = print 'Your firstname must be more than 2 characters';
}else{//data is fine send to encryption

    return $_POST['firstname'];
};
};

function validate_lastname(){
//validate here
if(!isset($_POST['lastname'])){
     $errors = print 'Please enter your lastname';
    }
else if(strlen($_POST['lastname']) <=2){
     $errors = print 'Your lastname must be more than 2 characters';
}else{//data is fine send to encryption

    return $_POST['lastname'];
};
};
function validate_username(){
//validate here
if(!isset($_POST['username'])){
     $errors = print 'Please enter your username';
    }
else if(strlen($_POST['username']) <=2){
     $errors = print 'Your username must be more than 2 characters';
}else{//data is fine send to encryption

    return $_POST['username'];
};
};      



  function is_valid_email_address($email){
    $qtext = '[^\\x0d\\x22\\xSc\\x80-\\xff]';
    $dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
    $atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c' .
    '\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
    $quoted_pair = '\\x5c[\\x00-\\x7f]';
    $domain_literal = "\\x5b($dtext|$quoted_pair)*\\x5d";
    $quoted_string = "\\x22($qtext|$quoted_pair)*\\x22";
    $domain_ref = $atom;
    $sub_domain = "($domain_ref|$domain_literal)";
    $word = "($atom|$quoted_string)";
    $domain = "$sub_domain(\\x2e$word)*";
    $local_part = "$word(\\x2e$word)*";
    $addr_spec = "$local_part\\x40$domain";
    return preg_match("!^$addr_spec$!", $email)? 1 : 0;
  };
  if (is_valid_email_address($email)){
     $errors = print 'Your email is a valid email address';
  } else{$errors = print 'Please enter a valid email address';
    };


  function validate_password($password){
    if(!isset($password)){
          $errors = print 'Please provide a password';
    }else if(strlen($password) <=5){
        $errors = print 'Your password must be at least 25 charachters long';
    }   
    else{return $password;}
};

    function confirm_password($confirm_password){

        if ($password !== $confirm_password){
             $errors = print 'Please enter the same password as above';
        }else{ $password === $confirm_password;}
            return $confirm_password;
        };
    };
 ?>

<div id="bigwrap">
        <div id = "space"></div>
    <div class="wrapper">

        <div class="content">
            <div id="form_wrapper" class="form_wrapper">

                <form class="register active" class="cmxform" action= "regprocess.php"           id="signupForm" class = "ajaxsubmit" method="get">
                    <fieldset class="ui-widget ui-widget-content ui-corner-all">
                    <h3 class="font">REGISTER</h3>
                    <div class="column">
                        <div>
                            <label for="cname">Firstname</label>
 <input id="cfirstname" name="firstname" type="text" class="required"   minlength="2" />

                        </div>
                        <div>
                            <label for="cname">Username</label>
 <input id="cusername" name="username" type="text"  class="required"  minlength="5" />

                        </div>
                        <div>
                            <label for="cemail">E-Mail:</label>
        <input id="cemail" name="email" type="text"  class="required email" />

                        </div>
                    </div>
                    <div class="column">
                        <div>
                            <label for="cname">Lastname</label>
 <input id="cname" name="lastname" type="text" class="required" class="ui-state-highlight" minlength="2" />

                        </div>
                        <div>
                            <label for="password">Password:</label>
        <input id="password" name="password" type="password"  class="required"/>

                        </div>
                        <div>
                            <label for="confirm_password">Confirm password:</label>
        <input id="confirm_password" name="confirm_password"   type="password" class="required" />

                        </div>
                    </div>
                    <div class="bottom">
                        <div class="remember">

                        </div>
                        <input type="submit" value="Register" class= "submit"/> 
                        <a href="index.html" rel="login" class="linkform">You have an account already? Log in here</a>

                        <div class="clear"></div>
                    </div>
                    <ul><?php echo $errors; ?></ul>
                </fieldset>
                </form>
4

2 回答 2

1

查看以下功能:

error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('log_errors', TRUE);
ini_set('display_startup_errors', TRUE);

这些将覆盖您的错误报告配置。确保在生产时将其删除。

于 2012-10-19T15:13:11.497 回答
0
//form processing
//import form info
//run this code when page submits test with js turned off
if (isset($_POST["submit"])) {
   $firstname = $_POST['firstname'];
$email = $_POST['email'];
$password = $_POST['password'];
$confirm_password = $_POST['confirm_password'];
 // $errors = array();
function validate_firstname(){
//validate here, 
 global $firstname;
 if(!isset($firstname)){

    //global $errors;
    //return  $errors;
    global $error_message1;
     $error_message1 = print 'Please enter your firstname';
    }
else if(strlen($firstname) <=2){
    //return $errors;
    global $error_message2;
     $error_message2 = print 'Your firstname must be more than 2 characters';
}else{//data is fine send to encryption

    return $firstname;
};
 };

function validate_lastname(){
//validate here
if(!isset($_POST['lastname'])){
     $errors = print 'Please enter your lastname';
    }
else if(strlen($_POST['lastname']) <=2){
     $errors = print 'Your lastname must be more than 2 characters';
}else{//data is fine send to encryption

    return $_POST['lastname'];
};
 };
function validate_username(){
//validate here
if(!isset($_POST['username'])){
     $errors = print 'Please enter your username';
    }
else if(strlen($_POST['username']) <=2){
     $errors = print 'Your username must be more than 2 characters';
}else{//data is fine send to encryption

    return $_POST['username'];
};
 };      



function is_valid_email_address($email){
    $qtext = '[^\\x0d\\x22\\xSc\\x80-\\xff]';
    $dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
    $atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c' .
    '\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
    $quoted_pair = '\\x5c[\\x00-\\x7f]';
    $domain_literal = "\\x5b($dtext|$quoted_pair)*\\x5d";
    $quoted_string = "\\x22($qtext|$quoted_pair)*\\x22";
    $domain_ref = $atom;
    $sub_domain = "($domain_ref|$domain_literal)";
    $word = "($atom|$quoted_string)";
    $domain = "$sub_domain(\\x2e$word)*";
    $local_part = "$word(\\x2e$word)*";
    $addr_spec = "$local_part\\x40$domain";
    return preg_match("!^$addr_spec$!", $email)? 1 : 0;
};
if (is_valid_email_address($email)){
     $errors = print 'Your email is a valid email address';
} else{$errors = print 'Please enter a valid email address';
    };


function validate_password($password){
    if(!isset($password)){
          $errors = print 'Please provide a password';
    }else if(strlen($password) <=5){
        $errors = print 'Your password must be at least 25 charachters long';
    }   
    else{return $password;}
};

    function confirm_password($confirm_password){

        if ($password !== $confirm_password){
             $errors = print 'Please enter the same password as above';
        }else{ $password === $confirm_password;}
            return $confirm_password;
        };
    };
  ?>

 <div id="bigwrap">
        <div id = "space"></div>
    <div class="wrapper">

        <div class="content">
            <div id="form_wrapper" class="form_wrapper">

                <form class="register active" class="cmxform" action= "regprocess.php"  id="signupForm" class = "ajaxsubmit" method="post">
                    <fieldset class="ui-widget ui-widget-content ui-corner-all">
                    <h3 class="font">REGISTER</h3>
                    <div class="column">
                        <div>
                            <label for="cname">Firstname</label>
 <input id="cfirstname" name="firstname" type="text" class="required" minlength="2" />

                        </div>
                        <div>
                            <label for="cname">Username</label>
 <input id="cusername" name="username" type="text"  class="required"  minlength="5" />

                        </div>
                        <div>
                            <label for="cemail">E-Mail:</label>
        <input id="cemail" name="email" type="text"  class="required email" />

                        </div>
                    </div>
                    <div class="column">
                        <div>
                            <label for="cname">Lastname</label>
 <input id="cname" name="lastname" type="text" class="required" class="ui-state-highlight" minlength="2" />

                        </div>
                        <div>
                            <label for="password">Password:</label>
        <input id="password" name="password" type="password"  class="required"/>

                        </div>
                        <div>
                            <label for="confirm_password">Confirm password:</label>
        <input id="confirm_password" name="confirm_password"   type="password" class="required" />

                        </div>
                    </div>
                    <div class="bottom">
                        <div class="remember">

                        </div>
                        <input type="submit" value="Register" class= "submit"/> 
                        <a href="index.html" rel="login" class="linkform">You have an account already? Log in here</a>

                        <div class="clear"></div>
                    </div>
                    <ul><?php $output =  validate_firstname(); echo "$output"; ?></ul>
                </fieldset>
                </form>
于 2012-10-23T21:25:26.170 回答