0
Hy!如何在php中驗證參數
我有一個user_register php與幾個參數,他們都應該證明。我的格式非常難看。我把所有的驗證都打包成了錯綜複雜的if子句。
請看:
include '../db_connect.php';
$arr = array('Data' => null,'Code' => null);
$birthdate = mysql_real_escape_string($_POST['birth']);
$gender = mysql_real_escape_string($_POST['gender']);
$uname = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['pw']);
$email = mysql_real_escape_string($_POST['email']);
$lang = mysql_real_escape_string($_POST['lang']);
if (!proof_value($birthdate) && !proof_value($gender) && !proof_value($uname) && !proof_value($password) && !proof_value($email))
{
if (!user_exist($uname))
{
if(!email_exist($email))
{
if (count($pw)==32)
{
if(count($gender)==1 && ($gender=='m' ||$gender =='f'))
{
$code = genverification();
$sql = "Insert into USER (DATE_BIRTH,GENDER,USER_NAME,PASSWORD,EMAIL,VERIFICATION) VALUES ('$birthdate','$gender','$uname','$password','$email','$code')";
$result = mysql_query("Insert into USER (DATE_BIRTH,GENDER,USER_NAME,PASSWORD,EMAIL,VERIFICATION) VALUES ('$birthdate','$gender','$uname','$password','$email','$code')");
if ($result)
{
require_once("mailer.php");
if (sendmail($email,$link, $lang))
{
$arr['Code'] = 200;
}
else
{
$arr['Code'] = 422;
}
}
else
{
$arr['Code'] = 421;
//$arr['Date'] = $sql;
}
}
else
{
$arr['Code'] = 420;
}
}
else
{
$arr['Code']=423;
}
}
else
{
$arr['Code']=419;
}
}
else
{
$arr['Code']=418;
}
}
else
{
$arr['Code']=400;
}
mysql_close($db);
echo json_encode($arr);
正如你可以看到,如果一個驗證失敗我的腳本將返回錯誤代碼。我想我的實際工作中的格式更改爲更好的可讀格式,但我現在的想法有怎樣解決這個本來
THX
`我的格式非常ugly.`是的,它是。 – Toto 2011-12-15 15:22:37
如果用戶函數剛剛返回`isset()`那麼爲什麼不只是使用`isset()`而且`count`應該用`strlen`來代替也是`$ pw`永遠不會被設置爲 – 2011-12-15 15:26:52
@ MA42 :)。如果你寫這樣的評論,你也應該發表一個答案 – test123123 2011-12-15 15:53:49