出于某种原因,我的代码无法运行。我不知道为什么。我的PHP mail()函数有什么问题,或者是否有其他错误。基本上,我正在尝试在用户注册后发送电子邮件验证电子邮件。我是非常新的PHP,请帮助!
我的PHP代码:
<?php
$con=mysqli_connect('localhost','anuraag','1234','test');
if(!$con || mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die();
}
function enc($s) { global $con; return mysqli_real_escape_string($con, $s); }
function createUser() {
global $con;
$name = enc($_POST['name']);
$email = enc($_POST['email']);
$password = enc(sha1(trim($_POST['password'])));
$confirmpassword = enc(sha1(trim($_POST['confirmpassword'])));
if($password == $confirmpassword) {
mysqli_query($con, "INSERT INTO users (name, email, password) VALUES ('" . $name . "','" . $email . "','" . $password . "');");
echo "Your registration is completed. Please verify your email.";
$regid = sha1("foo" . $email . "bar"); // This is a cheap way to do it
mail($_POST['email'], "Email verification", "Click this link to verify your email: http://localhost/register.php?email=".urlencode($_POST['email'])."&token=".urlencode($regid)."\n\nThanks.", "From: noreply@healthfullu.com");
} else {
echo "Passwords do not match";
}
}
我的HTML:
<html>
<head>
<title> Register!</title>
</head>
<body>
<form action="register.php" method="POST">
<h1>Signup!</h1>
<p>Create an account:</p>
<div>
<label for="name">Name:</label>
<input type="text" id="name" name="name" value="" placeholder="First Name" class="login" />
</div> <!-- /field -->
<div>
<label for="email">Email Address:</label>
<input type="text" id="email" name="email" value="" placeholder="Email" class="login"/>
</div> <!-- /field -->
<div>
<label for="pass">Password:</label>
<input type="password" id="password" name="password" value="" placeholder="Password" class="login"/>
</div> <!-- /field -->
<div>
<label for="confirm_password">Confirm Password:</label>
<input type="password" id="confirmpassword" name="confirmpassword" value="" placeholder="Confirm Password" class="login"/>
</div> <!-- /field -->
<input type="submit" name="submit"></input>
</form>
</div> <!-- /content -->
</div> <!-- /account-container -->
</body>
</html>
答案 0 :(得分:0)
你应该执行你的功能。添加代码的结尾:
createUser();
只需运行此功能并处理$_POST
数据。