isset的PHP my if函数在按下submit后给出错误值



我的网站的注册页面出现了问题,该页面以前可以工作,但没有任何更改,现在它开始出现错误,在我按下名为"的提交按钮后,在我的"if(isset($_POST['signup']("函数中给出了错误值;注册";。下面是我注册过程的所有代码,所以错误应该在某个地方,但找不到。

signup.php:

<?php include 'includes/session.php'; ?>
<?php
if(isset($_SESSION['user'])){
header('location: cart_view.php');
}

?>
<?php include 'includes/header.php'; ?>
<body class="hold-transition register-page" style="background-image:url('images/log-bg.png'); background-repeat:no-repeat; background-position: center; background-size: cover;">
<div class="register-box">
<?php
if(isset($_SESSION['error'])){
echo "
<div class='callout callout-danger text-center'>
<p>".$_SESSION['error']."</p> 
</div>
";
unset($_SESSION['error']);
}
if(isset($_SESSION['success'])){
echo "
<div class='callout callout-success text-center'>
<p>".$_SESSION['success']."</p> 
</div>
";
unset($_SESSION['success']);
}
?>
<div class="register-box-body" style="box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);">
<p class="login-box-msg">Register a new membership</p>
<form action="register.php" method="POST">
<div class="form-group has-feedback">
<input type="text" class="form-control" name="firstname" placeholder="Firstname" value="<?php echo (isset($_SESSION['firstname'])) ? $_SESSION['firstname'] : '' ?>" required>
<span class="glyphicon glyphicon-user form-control-feedback"></span>
</div>
<div class="form-group has-feedback">
<input type="text" class="form-control" name="lastname" placeholder="Lastname" value="<?php echo (isset($_SESSION['lastname'])) ? $_SESSION['lastname'] : '' ?>"  required>
<span class="glyphicon glyphicon-user form-control-feedback"></span>
</div>
<div class="form-group has-feedback">
<input type="email" class="form-control" name="email" placeholder="Email" value="<?php echo (isset($_SESSION['email'])) ? $_SESSION['email'] : '' ?>" required>
<span class="glyphicon glyphicon-envelope form-control-feedback"></span>
</div>
<div class="form-group has-feedback">
<input type="password" class="form-control" name="password" placeholder="Password" required>
<span class="glyphicon glyphicon-lock form-control-feedback"></span>
</div>
<div class="form-group has-feedback">
<input type="password" class="form-control" name="repassword" placeholder="Retype password" required>
<span class="glyphicon glyphicon-log-in form-control-feedback"></span>
</div>
<div class="form-group has-feedback">
<input type="text" class="form-control" name="contact" placeholder="Phone Number" required>
<span class="glyphicon glyphicon-phone form-control-feedback"></span>
</div>

<div class="form-group has-feedback">
<input type="text" class="form-control" name="address" placeholder="Address" required>
<span class="glyphicon glyphicon-home form-control-feedback"></span>
</div>

<hr>
<div class="row">
<div class="col-xs-4">
<button style="background-color: rgb(39, 39, 39); color:white; width:100px;" type="submit" class="btn btn-block btn-flat" name="signup"><i class="fa fa-pencil"></i> Sign Up</button>
</div>
</div>
</form>
<br>
<a style="color: rgb(39, 39, 39);" href="login.php">I already have a membership</a><br>
<a style="color: rgb(39, 39, 39);" href="index.php"><i class="fa fa-home"></i> Home</a>
</div>
</div>

<?php include 'includes/scripts.php' ?>
</body>
</html>

register.php:

<?php
use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerException;
include 'includes/session.php';
if(isset($_POST['signup'])){
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$password = $_POST['password'];
$repassword = $_POST['repassword'];
$contact = $_POST['contact'];
$address = $_POST['address'];
$_SESSION['firstname'] = $firstname;
$_SESSION['lastname'] = $lastname;
$_SESSION['email'] = $email;
$_SESSION['contact'] = $contact;
$_SESSION['address'] = $address;


if($password != $repassword){
$_SESSION['error'] = 'Passwords did not match';
header('location: signup.php');
}
else{
$conn = $pdo->open();
$stmt = $conn->prepare("SELECT COUNT(*) AS numrows FROM users WHERE email=:email");
$stmt->execute(['email'=>$email]);
$row = $stmt->fetch();
if($row['numrows'] > 0){
$_SESSION['error'] = 'Email already taken';
header('location: signup.php');
}
else{
$now = date('Y-m-d');
$password = password_hash($password, PASSWORD_DEFAULT);
//generate code
$set='123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$code=substr(str_shuffle($set), 0, 12);
try{
$stmt = $conn->prepare("INSERT INTO users (email, password, firstname, lastname, activate_code, created_on, contact_info, address) VALUES (:email, :password, :firstname, :lastname, :code, :now, :contact, :address)");
$stmt->execute(['email'=>$email, 'password'=>$password, 'firstname'=>$firstname, 'lastname'=>$lastname, 'code'=>$code, 'now'=>$now, 'contact'=>$contact, 'address'=>$address]);
$userid = $conn->lastInsertId();
$message = "
<h2>Thank you for Choosing .</h2>
<p>Your Account Details:</p>
<p>Email: ".$email."</p>
<p>Password: ".$_POST['password']."</p>
<p>Please click the link below to activate your account.</p>
<a href='http://localhost/activate.php?code=".$code."&user=".$userid."'>Activate Account</a>
";
//Load phpmailer
require 'vendor/autoload.php';
$mail = new PHPMailer(true);                             
try {
//Server settings
$mail->isSMTP();                                     
$mail->Host = 'tls://smtp.gmail.com:25';                      
$mail->SMTPAuth = true;                               
$mail->Username = 'mymail';     
$mail->Password = 'pass';                    
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);                         
$mail->SMTPSecure = 'ssl';                           
$mail->Port = 25;                                   
$mail->setFrom('mymail');

//Recipients
$mail->addAddress($email);              
$mail->addReplyTo('mymail');

//Content
$mail->isHTML(true);                                  
$mail->Subject = 'Sign Up';
$mail->Body    = $message;
$mail->send();
unset($_SESSION['firstname']);
unset($_SESSION['lastname']);
unset($_SESSION['email']);
$_SESSION['success'] = 'Account created. Check your email to activate.';
header('location: signup.php');
} 
catch (Exception $e) {
$_SESSION['error'] = 'Message could not be sent. Mailer Error: '.$mail->ErrorInfo;
header('location: signup.php');
}

}
catch(PDOException $e){
$_SESSION['error'] = $e->getMessage();
header('location: register.php');
}
$pdo->close();
}
}
}
else{
$_SESSION['error'] = 'Fill up signup form first';
header('location: signup.php');
}
?>

给按钮一个value属性。。。

<button style="background-color: rgb(39, 39, 39); color:white; width:100px;" type="submit" class="btn btn-block btn-flat" name="signup" value="xxx"><i class="fa fa-pencil"></i> Sign Up</button>

或使用array_key_exists();检查张贴数据

if (array_key_exists('signup', $_POST)){ ...

发生这种情况是因为isset()也检查空值:

https://www.geeksforgeeks.org/difference-between-isset-and-array_key_exists-function-in-php/#:~:text=%20isset((%20函数%20是,在%20all%20其他%20可能的%20情况下。

相关内容

  • 没有找到相关文章

最新更新