目前在我的登录系统中,用户必须在注册后进行登录。
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p>Not a member yet?.Sign up here.By Sign up You are accepting our <a href="terms.php">terms and conditions</a> </p>
<fieldset>
<legend>Registration Info</legend>
<label for="username">Username:</label>
<input type="text" id="username" name="username" value="<?php if (!empty($username)) echo $username; ?>" /><br />
<label for="email">Email id:</label>
<input type="email" id="email" name="email" /><br />
<label for="password1">Password:</label>
<input type="password" id="password1" name="password1" /><br />
<label for="password2">Password (retype):</label>
<input type="password" id="password2" name="password2" /><br />
</fieldset>
<input type="submit" value="Sign Up" name="create" />
</form>
如果表单提交正确
<?php
if (!empty($user_username) && !empty($user_password)) {
// Make sure someone isn't already registered using this username
$query = "SELECT * FROM <database> WHERE username = '$username'";
$data = mysqli_query($dbc, $query);
if (mysqli_num_rows($data) == 0) {
// The username is unique, so insert the data into the database
$query = "INSERT INTO <database> (username,last_name,password,join_date) VALUES ('$username','$email',SHA('$password1'), NOW())";
$result=mysqli_query($dbc, $query) or die("cant create an account");
}else{
die("Somthing went wrong");
}
}
?>
一旦完成,他必须转到登录页面,并要求使用以下代码登录
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<fieldset>
<legend>Log In</legend>
<label for="username">Username:</label>
<input type="text" name="username" value="<?php if (!empty($user_username)) echo $user_username; ?>" /><br />
<label for="password">Password:</label>
<input type="password" name="password" />
</fieldset>
<input type="submit" value="Log In" name="submit" />
</form>
我不希望这个系统发生这样的事情,当用户注册时,用户必须被带到主页。并且在注册过程中还必须设置会话变量。
尝试重用代码,这将有助于管理您的代码和应用程序。请为登录过程创建一个函数,并在正常登录时使用相同的函数,注册后使用注册所用的详细信息调用相同的函数。
function authenticate($user_id, $pwd)
{
// do the logic for login,
//if success then set value in session and redirect to the respective page
//if failed then error page
}
if (!empty($user_username) && !empty($user_password)) {
// Make sure someone isn't already registered using this username
$query = "SELECT * FROM <database> WHERE username = '$username'";
$data = mysqli_query($dbc, $query);
if (mysqli_num_rows($data) == 0) {
// The username is unique, so insert the data into the database
$query = "INSERT INTO <database> (username,last_name,password,join_date) VALUES ('$username','$email',SHA('$password1'), NOW())";
$result=mysqli_query($dbc, $query) or die("cant create an account");
// call the authenticate function for login
authenticate($username, SHA('$password1'));
}else{
die("Somthing went wrong");
}
}
试试这样的东西:-
<?php
if (!empty($user_username) && !empty($user_password)) {
// Make sure someone isn't already registered using this username
$query = "SELECT * FROM <database> WHERE username = '$username'";
$data = mysqli_query($dbc, $query);
if (mysqli_num_rows($data) == 0) {
// The username is unique, so insert the data into the database
$query = "INSERT INTO <database> (username,last_name,password,join_date) VALUES ('$username','$email',SHA('$password1'), NOW())";
$result=mysqli_query($dbc, $query) or die("cant create an account");
$_SESSION["username"] = $username; // assign session variable here.
header("Location: home.php"); // redirect to home page
}else{
die("Somthing went wrong");
}
}
?>