这是我第一次真正尝试进入StackExchange,我希望我的问题尽可能相关。基本上,我正在关注O'Reilly的书中学习PHP,MySQL,Javascript等等。我对第16章的身份验证协议感到困惑。基本上,作者希望我们只将CSS和JS放在echo<<<< _END语句中,我试图将它们放在外部文件中。
我的问题是,当我加载PHP文件时,绝对没有任何问题。甚至没有标题。我担心这可能与我正在运行WebMatrix和IIS Express服务器进行开发这一事实有关,但是当我去的时候,我已经能够运行其他更小的echo<<< _END语句了。测试它们。
以下是代码,如果有人提出意见,我将非常感激。
<?php
$firstname = $lastname = $username = $password = $age = $email = "";
if (isset($_POST['firstname']))
{
$firstname = fix_string($_POST['firstname']);
}
if (isset($_POST['lastname']))
{
$lastname = fix_string($_POST['lastname']);
}
if (isset($_POST['username']))
{
$username = fix_string($_POST['username']);
}
if (isset($_POST['password']))
{
$password = fix_string($_POST['password']);
}
if (isset($_POST['age']))
{
$age = fix_string($_POST['age']);
}
if (isset($_POST['email']))
{
$email = fix_string($_POST['email']);
}
$fail = valide_firstname($firstname);
$fail .= valide_lastname($lastname);
$fail .= valide_username($username);
$fail .= valide_password($password);
$fail .= valide_age($age);
$fail .= valide_email($email);
echo <<<_END
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>An Example Form</title>
<link rel="stylesheet" type="text/css" href="StyleSheet.css>
</head>
_END;
if ($fail = "")
{
echo "<body>Form data succesffully validated: $firstname, $lastname, $username, $password, $age, $email.</body></html>";
//database entry field
exit;
}
echo <<<_END
<body>
<table class="signup" border="0" cellpadding="2"
cellspacing="5" bgcolor="#eeeeee">
<th colspan="2" align="center">Signup Form</th>
<tr><td colspan="2">Sorry, the following errors were found<br />
in your form: <p><font color=red size=1><i>$fail</i></font></p>
</td></tr>
<form method="post" action = "adduser.php"
onSubmit = "return validate(this)">
<tr><td>First name</td><td><input type ="text" maxlength = "32"
name = "firstname" value = "$firstname" /></td></tr>
<tr><td>Last name</td><td><input type ="text" maxlength = "32"
name = "lastname" value = "$lastname" /></td></tr>
<tr><td>Username</td><td><input type ="text" maxlength = "16"
name = "username" value = "$username" /></td></tr>
<tr><td>Password</td><td><input type ="text" maxlength = "12"
name = "password" value = "$password" /></td></tr>
<tr><td>Age</td><td><input type ="text" maxlength = "3"
name = "age" value = "$age" /></td></tr>
<tr><td>Email</td><td><input type ="text" maxlength = "64"
name = "email" value = "$email" /></td></tr>
<tr><td colspan = "2" align = "center"><input type = "submit" value = "Signup" /></td></tr></form></table>
<script type="text/javascript" src="validate.js"></script>
</body>
</html>
_END;
function validate_fistname($field){
if ($field == "") return "No First Name was entered <br/>";
}
function validate_lastname($field){
if ($field == "") return "No Last Name was entered <br/>";
}
function validate_username($field){
if ($field == "") return "No Username was enterted <br/>";
else if (strlen($field) < 5) return "Usernames must be at least 5 characters long <br/>";
else if (preg_match("/[\W]/", $field)) return "Only letters, numbers, - and _ is usernames<br/>";
return "";
}
function validate_password($field){
if ($field =="") return "No Password was entered <br/>";
else if (strlen($field) < 6) return "Passwords must be at least 6 characters in length <br/>";
else if (!preg_match("/[a-z]/", $field) ||
!preg_match("/[A-Z]/", $field) ||
!preg_match("/[\d]/", $field))
return "Passwords require one upper case, one lower case, and one number<br/>";
return "";
}
function validate_age($field) {
if ($field == "") return "No Age was entered<br />";
else if ($field < 18 || $field > 110)
return "Age must be between 18 and 110<br />";
return "";
}
function validate_email($field) {
if ($field == "") return "No Email was entered<br />";
else if (!((strpos($field, ".") > 0) &&
(strpos($field, "@") > 0)) ||
preg_match("/[^a-zA-Z0-9.@_-]/", $field))
return "The Email address is invalid<br />";
return "";
}
function fix_string($string) {
if (get_magic_quotes_gpc()) $string = stripcslashes ($string);
return htmlentities ($string);
}
?>
答案 0 :(得分:1)
您有语法错误。
按照here所述启用错误报告。
错误1
你拼写错误的拼写。这个 -
$fail = valide_firstname($firstname);
$fail .= valide_lastname($lastname);
$fail .= valide_username($username);
$fail .= valide_password($password);
$fail .= valide_age($age);
$fail .= valide_email($email);
必须是 -
$fail = validate_firstname($firstname);
$fail .= validate_lastname($lastname);
$fail .= validate_username($username);
$fail .= validate_password($password);
$fail .= validate_age($age);
$fail .= validate_email($email);
错误2
您的validate_firstname()
函数定义缺少'r' -
function validate_fistname($field){
^ Needs an 'r' here.
错误3
条件if ($fail = "")
应为if ($fail == "")
。
您目前正在使用=
进行作业,而不是进行比较==
答案 1 :(得分:0)
你有一些PHP错误。
我们从你的第一行开始吧。什么是$firstname = $lastname = $username = $password = $age = $email = "";
?
我无法看到你尝试用这条线做什么。如果要使用""
初始化每个变量,请编写以下内容:
$firstname = "", $lastname = "", $username = "", $password = "", $age = "", $email = "";
或者每个变量的单行更好:
$fistname = "";
$lastname = "";
$username = "";
$password = "";
$age = "";
$email = "";
您也不应该检查$variable == ""
。这在PHP中没有意义(因为你想检查类型安全,这不是类型安全的)。对于PHP中的类型安全检查,请使用3等于:$variable === ""
。或者甚至更好,如果你想检查字符串是否为空,只需写empty($variable)
。
请在if语句中使用括号。这不是一个很好的和可扩展的风格。 O'Reilly也许不是学习编程的最佳资源。当然,他得到了一些解决方案的好例子。但O'Reilly有自己的编程风格,很多程序员都不同意。在大多数情况下,O'Reilly也已经过时了。
您尝试使用
echo <<<_END
hello world
_END;
被称为heredoc operater,这是使用mutliline字符串的好方法。
编辑:混合HTML,JS和PHP也很难看。通过MVC或多层等设计模式将它们分成不同的文件。