我的网站每个页面都有一个登录表单(在顶层菜单上),所以即使用户在“注册”页面上,登录表单仍然可以在此顶级菜单中找到。
我的问题是,在这种特定情况下,当用户填写注册表单并提交它时,它会返回所有字段都需要填写的错误。我发现这是因为我的登录表单使用了我的注册表单使用的相同输入名称($username
和$password
)。
因此,即使填写了注册表单,如果我将登录表单留空,它将返回此错误,但是如果我填写登录表单中的任何内容然后提交正确填写的注册表,则不会有任何冲突
以下是我的login form
,register form
和部分php code that process the register data
登录表单:
<form method="post" action="/ftp/index.php?login=1">
<div id="header_username">
<input type="text" name="username" id="txt_username" placeholder="username" required="" value="" />
<span class="username_icon"><i class="fa fa-user"></i></span>
</div>
<div id="header_password">
<input type="password" name="password" id="txt_password" placeholder="password" required="" />
<span class="password_icon"><i class="fa fa-lock"></i></span>
</div>
<div id="forgotten_pwd">
<a href=""><span> Forgotten Password ?</span></a>
</div>
<div id="submit_button">
<input type="hidden" name="redirect" value="<?php echo "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; ?>" />
<button type="submit" name="submit" id="sub_login"><i id="submit"class="fa fa-long-arrow-right"></i></button>
</div>
<div id="button-border"></div>
<div class="feedback">login successful <br />redirecting...</div>
</form>
注册表格:
<form action="<?php echo $_SERVER['PHP_SELF'].'?signup=1'; ?>" method="post">
<div class="line"><label for="username"><?php echo lang::get("Username")?> *: </label><input type="text" name="username" id="username" value="<?php if (isset($_POST['username'])) echo $_POST['username']?>"/></div>
<div class="line"><label for="email"><?php echo lang::get("Email")?> *: </label><input type="text" name="email" id="email" value="<?php if (isset($_POST['email'])) echo $_POST['email']?>"/></div>
<div class="line"><label for="password"><?php echo lang::get("Password")?> *: </label><input type="password" name="password" id="password"/></div>
<div class="line"><label for="password2"><?php echo lang::get("Confirm password:")?> </label><input type="password" name="password2" id="password2"/></div>
<?php
$rand_int1 = substr(mt_rand(),0,2);
$rand_int2 = substr(mt_rand(),0,1);
$rand_int3 = substr(mt_rand(),0,1);
$captcha_answer = $rand_int1 + $rand_int2 - $rand_int3;
$_SESSION['captcha_answer'] = $captcha_answer;
?>
<div class="line"><label for="captcha"><?php echo $rand_int1.' + '.$rand_int2.' - '.$rand_int3.' = ?';?> *: </label><input type="text" name="captcha" id="captcha" autocomplete="off"/></div>
<div class="line submit" style="text-align:right"><input type="submit" class="button" value="<?php echo lang::get("Sign up")?>" /></div>
</form>
<?php endif;?>
</div>
处理数据的php代码:
if (!empty($_POST)){
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$password2 = $_POST['password2'];
$captcha = $_POST['captcha'];
// requiered fields & validation
if ( !isset($username)
|| $username == ''
|| !ctype_alnum($username)
|| gator::getUser($username)
|| gator::getUser($email, 'email')
|| !isset($password)
|| !isset($password2)
|| !isset($_POST['captcha'])
|| $password == ''
|| $password != $password2
|| filter_var($email, FILTER_VALIDATE_EMAIL) == false
|| (int)$captcha != (int)$_SESSION['captcha_answer'])
{
if ($username && !ctype_alnum($username)) $_POST['username'] = '';
if (filter_var($email, FILTER_VALIDATE_EMAIL) == false) $_POST['email'] = '';
$errors = lang::get("All fields requiered!");
}
if ($errors == false){
$reloadaction = 'thanks';
我的问题如下:
即使我知道另一种解决方案是重命名我的寄存器表单的输入,是否有一个“更清洁”的解决方案呢?是否有任何我可以添加到我的php code that process the data
以使其在我提交注册表单时忽略登录表单的输入?大型网站如何处理这类冲突?
非常感谢你帮我解决这个问题!
-Apatik
编辑:我的完整注册码:
* Sign up init
*/
public function initSignup() {
// try to activate account?
if (gatorconf::get('signup_use_activation') && isset($_GET['activate'])){
$key = $_GET['activate'];
$user = gator::getUser($key, 'akey');
if ($user){
gator::updateUser($user['username'], array('akey' => '', 'permissions' => gatorconf::get('default_permissions_after_activation')));
$user['permissions'] = gatorconf::get('default_permissions_after_activation');
$this->loginUser($user);
}
header('Location: '.gatorconf::get('base_url'));
die;
}
$errors = null;
if (!empty($_POST)){
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$password2 = $_POST['password2'];
$captcha = $_POST['captcha'];
// requiered fields & validation
if ( !isset($username)
|| $username == ''
|| !ctype_alnum($username)
|| gator::getUser($username)
|| gator::getUser($email, 'email')
|| !isset($password)
|| !isset($password2)
|| !isset($_POST['captcha'])
|| $password == ''
|| $password != $password2
|| filter_var($email, FILTER_VALIDATE_EMAIL) == false
|| (int)$captcha != (int)$_SESSION['captcha_answer'])
{
if ($username && !ctype_alnum($username)) $_POST['username'] = '';
if (filter_var($email, FILTER_VALIDATE_EMAIL) == false) $_POST['email'] = '';
$errors = lang::get("All fields requiered!");
}
if ($errors == false){
$reloadaction = 'thanks';
$activationkey = '';
if (gatorconf::get('signup_use_activation')){
$activationkey = sha1(mt_rand(10000,99999).time());
$url = gatorconf::get('base_url').'/?signup=1&activate='.$activationkey;
$subject = gatorconf::get('account_email_subject');
$body = gatorconf::get('account_email_text'). "\n\n" .$url;
$this->sendEmail($email, $subject, $body);
$reloadaction = 'goactivate';
}
// homedir will be created based on username
$homedir = gatorconf::get('repository').'/users'.DS.$username;
// if dir does not exist - try to create one
if ($homedir != '' && !is_dir($homedir)){
if (!mkdir($homedir, 0755, true)) {
echo "ERROR: User's homedir cannot be created. Check permissions. DIR = ".$homedir; die;
}
}
// add new user
gator::addUser($username, array(
'password' => $password,
'permissions' => gatorconf::get('default_permissions_after_signup'),
'homedir' => $homedir,
'email' => $email,
'akey' => $activationkey));
// thanks on success or go activate!
header('Location: '.gatorconf::get('base_url').'/?signup=1&'.$reloadaction);
die;
}
}
if (isset($_GET['thanks'])){
// thanks on success or go activate!
header('Location: '.gatorconf::get('base_url'));
die;
}elseif (isset($_GET['goactivate'])){
gator::display("header.php");
gator::display("signup.php", array('goactivate' => 1));
}else{
gator::display("header.php");
gator::display("signup.php", array('errors' => $errors));
}
gator::display("../../../style/include/footer.php");
}
/**
*
答案 0 :(得分:2)
在注册表单中添加隐藏字段,例如<input type='hidden' name='action' value='register' />
,然后在$_POST
检查代码中检查该字段。
if ($_POST['action'] == 'register'){
// Process registration code
} else {
// Process login code
}
if ($_POST){
if ($_POST['action'] == 'action1'){
// do action 1 code
}
else if ($_POST['action'] == 'action2'){
// do action 2 code
}
else {
// do nothing
}
答案 1 :(得分:2)
是的,给你的提交按钮命名,然后检查一下:
if (isset($_POST('submit_login'))) {
// login related check
}
if (isset($_POST('submit_registration'))) {
// registration related check
}
答案 2 :(得分:1)
非常感谢每一个答案,这些答案都很有帮助。
我发现错误的是我的注册表的提交按钮没有名字(正如你在我的帖子中看到的那样)。我向其添加了name="register"
,然后在我的代码if (!empty($_POST)){
中更改为if (!empty($_POST['register'])){
现在看起来都很好。
答案 3 :(得分:1)
只需对注册页面的代码稍作修改即可。 首先给每个提交按钮一个名称,而不是登录php代码 写
<Button Style="{StaticResource btnStyle}"/>
其中"Style object is not allowed to affect the Style property of the object to which it applies."
和if(isset($_POST['submit_login'])&&!isset($_POST['submit_signup']))
是按钮的名称
我认为它应该工作..
祝你好运