【发布时间】:2014-05-24 09:18:08
【问题描述】:
我已经创建了一个 register.php 页面,它具有用于用户输入的 html 表单,然后它会在 php 验证输入的地方发布到自身,这一切都按我的意愿工作。我可以在验证后回显数据而没有问题,但是当我使用标头重定向添加重定向到 register.inc.php 然后尝试从 register.inc.php 页面回显发布数据时,它不会这样做,几乎就像数据未发布到 register.inc.php 文件,但是,如果我在 register.php 页面中包含 register.inc.php,它将在 register.php 页面上回显数据,这肯定表明正在发送发布数据,最终我希望将此数据输入数据库,但显然如果没有看到帖子数据,我将无法看到,我尝试了一个无效的会话开始,我似乎无法通过任何研究解决这个问题已经完成了,我敢打赌这是我想念的非常简单的事情。下面是registration.php页面和register.inc.php页面,我没有足够的积分无法添加图片。
注册.php
<?php
//include_once 'includes/register.inc.php';
// define variables and set to empty values
$usernameErr = $emailErr = $passwordErr = $clubnameErr = "";
$username = $email = $password = $clubname = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$password = test_input($_POST["password"]);
$clubname = test_input($_POST["clubname"]);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["username"])) {
$usernameErr = "**Name is required**";
} else {
$username = test_input($_POST["username"]);
if (!preg_match("/^[a-zA-Z ]*$/",$username)) {
$usernameErr = "**Only letters and white space allowed**";
exit();
}
}
if (empty($_POST["email"])) {
$emailErr = "**Email is required**";
} else {
$email = test_input($_POST["email"]);
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) {
$emailErr = "**Invalid email format**";
}
}
if (empty($_POST["password"])) {
$passwordErr = "**Password is required**";
} else {
$password = test_input($_POST["password"]);
}
if (empty($_POST["confirmpassword"])) {
$confirmpasswordErr = "**Please confirm password**";
} else {
$confirmpassword = test_input($_POST["confirmpassword"]);
}
if($_POST['password'] != $_POST['confirmpassword']){
$confirmpasswordErr = "**Passwords did not match**";
}
if($_POST["password"] && strlen(trim($_POST["password"])) < 6)
{
$passwordErr = "**Password needs to be at least 6 characters**";
}
if (empty($_POST["clubname"])) {
$clubnameErr = "**Club Name is required**";
} else {
$clubname = test_input($_POST["clubname"]);
if (!preg_match("/^[a-zA-Z ]*$/",$clubname)) {
$clubnameErr = "**Only letters and white space allowed**";
}
if ($usernameErr == '' && $emailErr == '' && $passwordErr == '' && $confirmpasswordErr == '' && $clubnameErr == '') {
$clubname;
header("Location: includes/register.inc.php");
}
}
}
//echo $_POST['username'];
//echo $_POST['email'];
//echo $_POST['password'];
//echo $_POST['confirmpassword'];
//echo $_POST['clubname'];
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style type="text/css">
#form {
width: 80%;
margin-left: auto;
margin-right: auto;
}
</style>
</head>
<body>
<div id="form">
<form id="registermember" name="registermember" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div align="center">
<fieldset>
<legend style="font-weight: bolder; font-size: large;">Register Members</legend>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="33%"><div align="right"><span style="color: #000000; font-weight: bold;">Members Name:</span></div></td>
<td width="33%"><div align="center"><span style="color: #000000; font-weight: bold;">
<input name="username" type="text" id="username" value="<?php echo $username;?>" size="40%">
</span></div>
<td width="33%"><div align="left"><span class="error" style="color: #FF0004; font-weight: bold;"><?php echo $usernameErr;?></span></div></td>
</tr>
<tr>
<td><div align="center"><span style="color: #000000; font-weight: bold;">
</span></div> <span style="color: #000000; font-weight: bold;">
<label for="email">
<div align="center">
<div align="right"><span style="color: #000000; font-weight: bold;">Members Email:</span></div>
</div>
<span style="color: #000000; font-weight: bold;"></span><span style="color: #000000; font-weight: bold;">
</label>
</span></td>
<td><div align="center"><span style="color: #000000; font-weight: bold;">
<input name="email" type="text" id="email" value="<?php echo $email;?>" size="40%">
</span></div>
<td style="color: #FF0004"><div align="left"><span style="color: #FF0004; font-weight: bold;"><span class="error"><?php echo $emailErr;?></span></span></div></td>
</tr>
<tr>
<td><div align="center"><span style="color: #000000; font-weight: bold;">
</span></div> <span style="color: #000000; font-weight: bold;">
<label for="password2">
<div align="center">
<div align="right"><span style="color: #000000; font-weight: bold;">Members Password:</span></div>
</div>
<span style="color: #000000; font-weight: bold;"></span><span style="color: #000000; font-weight: bold;">
</label>
</span></td>
<td><div align="center"><span style="color: #000000; font-weight: bold;">
<input name="password" type="password" id="password" size="40%">
</span></div>
<td style="color: #FF0004"><div align="left"><span style="color: #FF0004; font-weight: bold;"><span class="error"><?php echo $passwordErr;?></span></span></div></td>
</tr>
<tr>
<td><div align="center"><span style="color: #000000; font-weight: bold;">
</span></div> <span style="color: #000000; font-weight: bold;">
<label for="confirmpassword">
<div align="center">
<div align="right"><span style="color: #000000; font-weight: bold;">Confirm Password:</span></div>
</div>
<span style="color: #000000; font-weight: bold;"></span><span style="color: #000000; font-weight: bold;">
</label>
</span></td>
<td><div align="center"><span style="color: #000000; font-weight: bold;">
<input name="confirmpassword" type="password" id="confirmpassword" size="40%">
</span></div>
<td style="color: #FF0004"><div align="left"><span style="color: #FF0004; font-weight: bold;"><span class="error"><?php echo $confirmpasswordErr;?></span></span></div></td>
</tr>
<tr>
<td><div align="center"><span style="color: #000000; font-weight: bold;">
</span></div> <span style="color: #000000; font-weight: bold;">
<label for="clubname2">
<div align="center">
<div align="right"><span style="color: #000000; font-weight: bold;">Members Club:</span></div>
</div>
<span style="color: #000000; font-weight: bold;"></span><span style="color: #000000; font-weight: bold;">
</label>
</span></td>
<td><div align="center"><span style="color: #000000; font-weight: bold;">
<input name="clubname" type="text" id="clubname" value="<?php echo $clubname;?>" size="40%">
</span></div>
<td style="color: #FF0004"><div align="left"><span style="color: #FF0004; font-weight: bold;"><span class="error"><?php echo $clubnameErr;?></span></span></div></td>
</tr>
<tr>
<td> </td>
<td><div align="center">
<p>
<input name="submitmember" type="submit" id="submitmember" formaction="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" formmethod="POST" value="Add Member">
</p>
</div>
<td style="color: #FF0004"> </td>
</tr>
</table>
</fieldset>
</form>
</div>
</body>
</html>
register.inc.php
<?php
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$clubname = $_POST['clubname'];
echo 'success'.'<br/><br/>';
echo $clubname.'<br/><br/>';
echo $username.'<br/><br/>';
echo $email.'<br/><br/>';
echo $password.'<br/><br/>';
?>
【问题讨论】:
-
重定向时不会保留发布数据 .... 不能那样工作。您可能应该包含 register.inc.php 文件,而不是重定向到它。
-
我已经这样做了,但它会直接回响到注册页面,无论如何我可以阻止它这样做吗?
-
我试过了,register.inc.php 仍然不会回显数据,除非返回到注册页面
-
我这样做的原因是为了避免发布未经哈希处理的密码,一旦我知道数据被发布到 register.inc.php 页面,我将在 register.php 页面上对密码进行哈希处理,除非我的想法是错误的