问题描述
我的网站注册页面存在问题,该页面以前可以正常运行,但没有任何变化,现在它通过在我的'if(isset($ set _ $ [POSTUP']))中提供错误值开始出现错误'功能是在按下具有“注册”名称的提交按钮之后。下面是我注册过程的所有代码,因此错误应该在某处,但找不到它。
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.2),0 6px 20px 0 rgba(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);" href="login.PHP">I already have a membership</a><br>
<a style="color: rgb(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 PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
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),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()
还会检查空值: