[英]Login form showing the first time with successful login
他們的關鍵詞搜索毫無意義,我什么也找不到。 希望你們幫助我。 這是代碼:
<?php
if(!isset($_SESSION['userid']))
{
include ("loginSection.php");
}
else
{
include ("loggedON.php");
}
?>
它在我的標題中。 它可以正常工作,但問題是用戶名和密碼正確時,它會在第一次顯示表單時顯示“登錄成功”消息。 但導航后就可以了。 無論如何,在頁面加載之前有沒有在會話中檢查userid的方法,因此第一次不會顯示登錄表單?
<?php
if (!empty($_POST['userpassSubmit']))
{
$username = $_POST['username'];
$password = $_POST['password'];
$query = "SELECT id, username FROM users WHERE username = ? AND password = ? LIMIT 1";
$statement = $databaseConnection->prepare($query);
$statement->bind_param('ss', $username, $password);
$statement->execute();
$statement->store_result();
if ($statement->num_rows == 1)
{
$statement->bind_result($_SESSION['userid'], $_SESSION['username']);
$statement->fetch();
echo "login successful";
}
else
{
echo "incorrect username password combination";
}
}
echo '
<div class="row">
<div class="col-sm-12 text-center">
<form id="regForm" role="form" action='.$_SERVER['PHP_SELF'].' method="post" class="form-inline">
<div class="form-group">
<label for="username">username</label>
<input type="text" class="form-control" id="username" name="username"><br>
</div>
<div class="form-group">
<label for="password">password</label>
<input type="password" class="form-control" id="password" name="password"><br>
</div>
<input name="userpassSubmit" class="btn btn-default" type="submit" value="login">
</form>
</div>
</div>';
?>
您需要創建注銷頁面或清除cookie才能顯示您的登錄頁面。 在注銷頁面中,您需要銷毀或取消設置會話。
**logout.php**
session_start();
if(isset($_SESSION['userid']))
{
unset($_SESSION['userid']);
}
對不起
我認為以下代碼已添加到您的loginSection.php頁面
<?php
session_start();
if(isset($_SESSION['userid']))
{
?>
<script>
window.location.href='loggedON.php';
</script>
<?php
}
if (!empty($_POST['userpassSubmit']))
{
$username = $_POST['username'];
$password = $_POST['password'];
$query = "SELECT id, username FROM users WHERE username = ? AND password = ? LIMIT 1";
$statement = $databaseConnection->prepare($query);
$statement->bind_param('ss', $username, $password);
$statement->execute();
$statement->store_result();
if ($statement->num_rows == 1)
{
$statement->bind_result($_SESSION['userid'], $_SESSION['username']);
$statement->fetch();
echo "login successful";
}
else
{
echo "incorrect username password combination";
}
}
echo '
<div class="row">
<div class="col-sm-12 text-center">
<form id="regForm" role="form" action='.$_SERVER['PHP_SELF'].' method="post" class="form-inline">
<div class="form-group">
<label for="username">username</label>
<input type="text" class="form-control" id="username" name="username"><br>
</div>
<div class="form-group">
<label for="password">password</label>
<input type="password" class="form-control" id="password" name="password"><br>
</div>
<input name="userpassSubmit" class="btn btn-default" type="submit" value="login">
</form>
</div>
</div>';
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.