用于西班牙语DNI的PHP检查功能。服务器错误500,为什么?

时间:2014-03-20 17:28:42

标签: php regex if-statement

我很绝望。从那以后,我试图在PHP中为西班牙语DNI实现检查功能。我没有发现我的错误。有人可以看看吗?

<?php

require("bbdd.php");

$errors = '';
$dni = checkDNI($_POST['dni']);

/**
 * Function which checks if a string is a valid spanish DNI
 *
 * @param string $dniUnchecked
 * @return string $dni
 */      
function checkDNI($dniUnchecked) {
    //Possible values for the final letter
    $letterValues = array(
        'T' => 0, 'R' => 1, 'W' => 2, 'A' => 3, 'G' => 4, 'M' => 5,
        'Y' => 6, 'F' => 7, 'P' => 8, 'D' => 9, 'X' => 10, 'B' => 11,
        'N' => 12, 'J' => 13, 'Z' => 14, 'S' => 15, 'Q' => 16, 'V' => 17,
        'H' => 18, 'L' => 19, 'C' => 20, 'K' => 21, 'E' => 22
    );
    //Check if entered
    if($dniUnchecked == ''){
        $errors .= 'Please enter a DNI.<br/>';  
        echo $errors;
        return false;
    }       
    //Check length
    elseif(strlen($dniUnchecked) != 9){
        $errors .= 'Please enter a DNI that has 8 digits and a check-letter.<br/>';
        echo $errors;
        return false;
    }
    //Check validity
    elseif (preg_match('/^[0-9]{8}[A-Z]$/i', $dniUnchecked)) {
        //Check letter
        $checkArray = str_split($dniUnchecked);
        $checkArray[8] = $letterValues[$checkArray[8]];
        //var_dump($checkArray);
        $checkSum = 0;
        $i = 0;
        $factor = array(7, 3, 1, 7, 3, 1, 7, 3, 1);
        for($i=0; $i<9; $i++){
            $checkArray[$i] = (int)$checkArray[$i];
            $checkSum += ($checkArray[$i] *= $factor[$i]);
        }
        if((preg_match("/\d$/", $checkSum) == 3) {
        //All was ok
        echo 'all ok';
            $dni = trim($dniUnchecked);
            $dni = stripsplashes($dni);
            $dni = htmlspecialchars($dni);
            return $dni;
        } else {
        $errors .= 'Please enter a valid DNI.<br/>';
        echo $errors;
        }
    }
}

当我检查旧提交时,至少检查if和第一个elseif是否正常工作。当我实现第二个elseif时,没有任何事情发生了。正如您所看到的,我希望从PHP中看到错误,但没有任何内容。只需500。 如果有人向我显示错误或向我显示方向,那会很酷。谢谢!


工作版本恕我直言

$dni = checkDNI($_POST['dni']);

/**
 * Function which checks if a string is a valid spanish DNI
 *
 * @param string $dniUnchecked
 * @return string $dni
 */      
function checkDNI($dniUnchecked) {
    //Error-Output
    $errors = '';
    //Possible values for the final letter
    $letterValues = array(
        'T' => 0, 'R' => 1, 'W' => 2, 'A' => 3, 'G' => 4, 'M' => 5,
        'Y' => 6, 'F' => 7, 'P' => 8, 'D' => 9, 'X' => 10, 'B' => 11,
        'N' => 12, 'J' => 13, 'Z' => 14, 'S' => 15, 'Q' => 16, 'V' => 17,
        'H' => 18, 'L' => 19, 'C' => 20, 'K' => 21, 'E' => 22
    );
    //Check if entered
    if($dniUnchecked == ''){
        $errors .= 'Please enter a DNI.<br/>';  
        echo $errors;
        return false;
    }       
    //Check length
    elseif(strlen($dniUnchecked) != 9){
        $errors .= 'Please enter a DNI that has 8 digits and a check-letter.<br/>';
        echo $errors;
        return false;
    }
    //Check validity
    elseif (preg_match('/^[0-9]{8}[A-Z]$/i', $dniUnchecked)) {
        // take numbers as big integer
        $checkNumber = (int)substr($dniUnchecked, 0, 8);
        var_dump($checkNumber);
        // modulo 23 and check if modulo equals corresponding checkletter
        if($checkNumber % 23 == $letterValues[substr($dniUnchecked, 8, 1)]){
        //All was ok
        echo 'all ok';
            $dni = trim($dniUnchecked);
            $dni = stripslashes($dni);
            $dni = htmlspecialchars($dni);
            return $dni;
        } else {
        $errors .= 'Please enter a valid DNI.<br/>';
        echo $errors;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您还有一个(

if((preg_match("/\d$/", $checkSum) == 3) {
// ^
// there

但严重的是,打开error reporting

你可以通过几种方式做到这一点。例如:

ini_set('display_errors',1); 
error_reporting(E_ALL);

位于脚本的顶部。