我正在尝试实现它,如果用户试图创建一个帐户,他们必须满足特定的字符串长度 ETC。如果他们不这样做,那么页面应该向他们吐出一条消息。
我仅针对用户名尝试了以下操作作为测试,但它并不成功。我只用了几天 PHP,谢天谢地,我是新手,我已经在谷歌、youtube 和 stackoverflow 上搜索过,但似乎无法让它适用于我的具体情况。
<?php
include_once 'config.php';
$_SESSION['message'] = "Initiating Account Creation.";
$mysqli = sqlsrv_connect($serverName, $conn_array) or die ($_SESSION['message'] = "No connection to the database.");
if (isset($_POST['register'])) {
session_start();
// No crazy special characters in user or email because it can mess with SQL query + inputs.
$username = ($_POST['username']);
$email = ($_POST['email']);
//VERIFY TWO PASSWORDS ARE EQUAL.
$password = ($_POST['password']);
$password2 = ($_POST['confirmpassword']);
$str_len1 = strlen(($_POST['username']));
if ($str_len1 < 6)
{
$_SESSION['message'] = "Registration Failed. Unable to add $username to the database!";
// exit(); using this completely erases the form even after refreshing the page.
}
if ($password == $password2) {
//md5 hashed password for basic security.
$password = md5($password);
$sql = "
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_WARNINGS ON
SET ANSI_PADDING ON
exec dnmembership.dbo.__NX__CreateAccount '$username','$password','$email'
";
sqlsrv_query ($mysqli, $sql);
$_SESSION['message'] = "Registration Successful. Added $username to the database!";
header("location: welcome.php");
}else{
$_SESSION['message'] = "Registration error. Try a valid username, password, or email.";
}
}
?>
绝地无双