目前,在我的登录系统中,用户必须在注册后进行登录。
<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");
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句