按提交按钮后登录面板上的页面刷新
Page refresh on Login Panel after pressing Submit button
我是 PHP 的新手,我希望获得有关我在互联网上找到的开源系统的一些帮助,我将其改编用于我目前正在从事的项目.
首先,登录是在一个完全不同的网页上,但我已经对其进行了调整以适应 index.php。登录系统工作得很好,但我有一个问题。登录后,网页不会自行刷新,登录表单也消失了。如果我刷新网页,该网站会显示我已正常登录。有什么办法可以解决这个问题吗?
我的代码是:
<div class="content">
<?php
//We display a welcome message, if the user is logged, we display it username
?>
Hello
<?php if(isset($_SESSION['username'])){echo ' '.htmlentities($_SESSION['username'], ENT_QUOTES, 'UTF-8');} ?>,
<br /> Welcome on our website.
<br /> You can <a href="users.php">see the list of users</a>.
<br />
<br />
<?php
//If the user is logged, we display links to edit his infos, to see his pms and to log out
if(isset($_SESSION['username']))
{
//We count the number of new messages the user has
$nb_new_pm = mysql_fetch_array(mysql_query('select count(*) as nb_new_pm from pm where ((user1="'.$_SESSION['userid'].'" and user1read="no") or (user2="'.$_SESSION['userid'].'" and user2read="no")) and id2="1"'));
//The number of new messages is in the variable $nb_new_pm
$nb_new_pm = $nb_new_pm['nb_new_pm'];
// Check if current user is the admin
if($_SESSION['userid']==1)
{ include('indexp.php');}
else { echo'nu merge';}
//We display the links
?>
<a href="edit_infos.php">Edit my personnal informations</a>
<br />
<a href="list_pm.php">My personnal messages(<?php echo $nb_new_pm; ?> unread)</a>
<br />
<a href="logout.php">Logout</a>
<?php
}
else
{
//Otherwise, we display a link to log in and to Sign up
?>
<a href="sign_up.php">Sign up</a>
<br />
<button data-toggle="collapse" data-target="#login">Log in</button>
<div id="login" class="collapse">
<?php
$ousername = '';
//We check if the form has been sent
if(isset($_POST['username'], $_POST['password']))
{
//We remove slashes depending on the configuration
if(get_magic_quotes_gpc())
{
$ousername = stripslashes($_POST['username']);
$username = mysql_real_escape_string(stripslashes($_POST['username']));
$password = stripslashes($_POST['password']);
}
else
{
$username = mysql_real_escape_string($_POST['username']);
$password = $_POST['password'];
}
//We get the password of the user
$req = mysql_query('select password,id from users where username="'.$username.'"');
$dn = mysql_fetch_array($req);
//We compare the submited password and the real one, and we check if the user exists
if($dn['password']==$password and mysql_num_rows($req)>0)
{
//If the password is good, we dont show the form
$form = false;
//We save the user name in the session username and the user Id in the session userid
$_SESSION['username'] = $_POST['username'];
$_SESSION['userid'] = $dn['id'];
}
else
{
//Otherwise, we say the password is incorrect.
$form = true;
$message = 'The username or password is incorrect. Please try again!';
}
}
else
{
$form = true;
}
if($form)
{
//We display a message if necessary
if(isset($message))
{
echo '<div class="message">'.$message.'</div>';
}
//We display the form
?>
<div class="content">
<form action="success.php" method="post">
Please type your IDs to log in:
<br />
<div class="center">
<label for="username">Username</label>
<input type="text" name="username" id="username" value="<?php echo htmlentities($ousername, ENT_QUOTES, 'UTF-8'); ?>" />
<br />
<label for="password">Password</label>
<input type="password" name="password" id="password" />
<br />
<input type="submit" value="Log in" />
</div>
</form>
</div>
<?php
}
?>
</div>
<?php
}
?>
</div>
我尝试了以下方法:
我设置当用户登录时,它被重定向到名为 success.php 的页面,其中显示成功消息和后退按钮。
问题是:当用户按下后退按钮(index.php)时,系统无法识别用户已登录并要求他重新登录。就是这样,一个你无法登录的无限循环。
我试了很多方法重新加载页面两次,但似乎很无效
您需要在需要会话的每个 PHP 页面的开头调用 session_start()
并保存它们。此外,我真的建议您将登录逻辑放在一个单独的 PHP 文件中,以提高可读性并能够轻松了解缺少的内容。
利用PHP会话并尝试在会话中存储用户名密码然后重定向。
以下是小示例哦 php 会话如何工作
<?php
ob_start();
session_start();
?>
<?
// error_reporting(E_ALL);
// ini_set("display_errors", 1);
?>
<html lang = "en">
<head>
<title>Tutorialspoint.com</title>
<link href = "css/bootstrap.min.css" rel = "stylesheet">
<style>
body {
padding-top: 40px;
padding-bottom: 40px;
background-color: #ADABAB;
}
.form-signin {
max-width: 330px;
padding: 15px;
margin: 0 auto;
color: #017572;
}
.form-signin .form-signin-heading,
.form-signin .checkbox {
margin-bottom: 10px;
}
.form-signin .checkbox {
font-weight: normal;
}
.form-signin .form-control {
position: relative;
height: auto;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
padding: 10px;
font-size: 16px;
}
.form-signin .form-control:focus {
z-index: 2;
}
.form-signin input[type="email"] {
margin-bottom: -1px;
border-bottom-right-radius: 0;
border-bottom-left-radius: 0;
border-color:#017572;
}
.form-signin input[type="password"] {
margin-bottom: 10px;
border-top-left-radius: 0;
border-top-right-radius: 0;
border-color:#017572;
}
h2{
text-align: center;
color: #017572;
}
</style>
</head>
<body>
<h2>Enter Username and Password</h2>
<div class = "container form-signin">
<?php
$msg = '';
if (isset($_POST['login']) && !empty($_POST['username'])
&& !empty($_POST['password'])) {
if ($_POST['username'] == 'tutorialspoint' &&
$_POST['password'] == '1234') {
$_SESSION['valid'] = true;
$_SESSION['timeout'] = time();
$_SESSION['username'] = 'tutorialspoint';
echo 'You have entered valid use name and password';
}else {
$msg = 'Wrong username or password';
}
}
?>
</div> <!-- /container -->
<div class = "container">
<form class = "form-signin" role = "form"
action = "<?php echo htmlspecialchars($_SERVER['PHP_SELF']);
?>" method = "post">
<h4 class = "form-signin-heading"><?php echo $msg; ?></h4>
<input type = "text" class = "form-control"
name = "username" placeholder = "username = tutorialspoint"
required autofocus></br>
<input type = "password" class = "form-control"
name = "password" placeholder = "password = 1234" required>
<button class = "btn btn-lg btn-primary btn-block" type = "submit"
name = "login">Login</button>
</form>
Click here to clean <a href = "logout.php" tite = "Logout">Session.
</div>
</body>
</html>
Login.php
它将清除会话数据。
<?php
session_start();
unset($_SESSION["username"]);
unset($_SESSION["password"]);
echo 'You have cleaned session';
header('Refresh: 2; URL = login.php');
?>
Logout.php
您在提交代码之前检查了会话值,因此无法在提交时检查会话变量。
我刚刚将您的提交代码块放在会话检查之上,以便它立即生效。
并且记得在每一页上都有 session_start();
。
<div class="content">
<?php
session_start();
$ousername = '';
//We check if the form has been sent
if (isset($_POST['username'], $_POST['password'])) {
//We remove slashes depending on the configuration
if (get_magic_quotes_gpc()) {
$ousername = stripslashes($_POST['username']);
$username = mysql_real_escape_string(stripslashes($_POST['username']));
$password = stripslashes($_POST['password']);
} else {
$username = mysql_real_escape_string($_POST['username']);
$password = $_POST['password'];
}
//We get the password of the user
$req = mysql_query('select password,id from users where username="' . $username . '"');
$dn = mysql_fetch_array($req);
//We compare the submited password and the real one, and we check if the user exists
if ($dn['password'] == $password and mysql_num_rows($req) > 0) {
//If the password is good, we dont show the form
$form = false;
//We save the user name in the session username and the user Id in the session userid
$_SESSION['username'] = $_POST['username'];
$_SESSION['userid'] = $dn['id'];
} else {
//Otherwise, we say the password is incorrect.
$form = true;
$message = 'The username or password is incorrect. Please try again!';
}
} else {
$form = true;
}
//We display a welcome message, if the user is logged, we display it username
?>
Hello<?php if (isset($_SESSION['username'])) {
echo ' ' . htmlentities($_SESSION['username'], ENT_QUOTES, 'UTF-8');
} ?>,<br/>
Welcome on our website.<br/>
You can <a href="users.php">see the list of users</a>.<br/><br/>
<?php
//If the user is logged, we display links to edit his infos, to see his pms and to log out
if (isset($_SESSION['username'])) {
//We count the number of new messages the user has
$nb_new_pm = mysql_fetch_array(mysql_query('select count(*) as nb_new_pm from pm where ((user1="' . $_SESSION['userid'] . '" and user1read="no") or (user2="' . $_SESSION['userid'] . '" and user2read="no")) and id2="1"'));
//The number of new messages is in the variable $nb_new_pm
$nb_new_pm = $nb_new_pm['nb_new_pm'];
// Check if current user is the admin
if ($_SESSION['userid'] == 1) {
include('indexp.php');
} else {
echo 'nu merge';
}
//We display the links
?>
<a href="edit_infos.php">Edit my personnal informations</a><br/>
<a href="list_pm.php">My personnal messages(<?php echo $nb_new_pm; ?> unread)</a><br/>
<a href="logout.php">Logout</a>
<?php
} else {
//Otherwise, we display a link to log in and to Sign up
?>
<a href="sign_up.php">Sign up</a><br/>
<button data-toggle="collapse" data-target="#login">Log in</button>
<div id="login" class="collapse">
<?php
if ($form) {
//We display a message if necessary
if (isset($message)) {
echo '<div class="message">' . $message . '</div>';
}
//We display the form
?>
<div class="content">
<form action="success.php" method="post">
Please type your IDs to log in:<br/>
<div class="center">
<label for="username">Username</label><input type="text" name="username" id="username"
value="<?php echo htmlentities($ousername, ENT_QUOTES, 'UTF-8'); ?>"/><br/>
<label for="password">Password</label><input type="password" name="password" id="password"/><br/>
<input type="submit" value="Log in"/>
</div>
</form>
</div>
<?php
}
?>
</div>
<?php
}
?>
</div>
我是 PHP 的新手,我希望获得有关我在互联网上找到的开源系统的一些帮助,我将其改编用于我目前正在从事的项目.
首先,登录是在一个完全不同的网页上,但我已经对其进行了调整以适应 index.php。登录系统工作得很好,但我有一个问题。登录后,网页不会自行刷新,登录表单也消失了。如果我刷新网页,该网站会显示我已正常登录。有什么办法可以解决这个问题吗?
我的代码是:
<div class="content">
<?php
//We display a welcome message, if the user is logged, we display it username
?>
Hello
<?php if(isset($_SESSION['username'])){echo ' '.htmlentities($_SESSION['username'], ENT_QUOTES, 'UTF-8');} ?>,
<br /> Welcome on our website.
<br /> You can <a href="users.php">see the list of users</a>.
<br />
<br />
<?php
//If the user is logged, we display links to edit his infos, to see his pms and to log out
if(isset($_SESSION['username']))
{
//We count the number of new messages the user has
$nb_new_pm = mysql_fetch_array(mysql_query('select count(*) as nb_new_pm from pm where ((user1="'.$_SESSION['userid'].'" and user1read="no") or (user2="'.$_SESSION['userid'].'" and user2read="no")) and id2="1"'));
//The number of new messages is in the variable $nb_new_pm
$nb_new_pm = $nb_new_pm['nb_new_pm'];
// Check if current user is the admin
if($_SESSION['userid']==1)
{ include('indexp.php');}
else { echo'nu merge';}
//We display the links
?>
<a href="edit_infos.php">Edit my personnal informations</a>
<br />
<a href="list_pm.php">My personnal messages(<?php echo $nb_new_pm; ?> unread)</a>
<br />
<a href="logout.php">Logout</a>
<?php
}
else
{
//Otherwise, we display a link to log in and to Sign up
?>
<a href="sign_up.php">Sign up</a>
<br />
<button data-toggle="collapse" data-target="#login">Log in</button>
<div id="login" class="collapse">
<?php
$ousername = '';
//We check if the form has been sent
if(isset($_POST['username'], $_POST['password']))
{
//We remove slashes depending on the configuration
if(get_magic_quotes_gpc())
{
$ousername = stripslashes($_POST['username']);
$username = mysql_real_escape_string(stripslashes($_POST['username']));
$password = stripslashes($_POST['password']);
}
else
{
$username = mysql_real_escape_string($_POST['username']);
$password = $_POST['password'];
}
//We get the password of the user
$req = mysql_query('select password,id from users where username="'.$username.'"');
$dn = mysql_fetch_array($req);
//We compare the submited password and the real one, and we check if the user exists
if($dn['password']==$password and mysql_num_rows($req)>0)
{
//If the password is good, we dont show the form
$form = false;
//We save the user name in the session username and the user Id in the session userid
$_SESSION['username'] = $_POST['username'];
$_SESSION['userid'] = $dn['id'];
}
else
{
//Otherwise, we say the password is incorrect.
$form = true;
$message = 'The username or password is incorrect. Please try again!';
}
}
else
{
$form = true;
}
if($form)
{
//We display a message if necessary
if(isset($message))
{
echo '<div class="message">'.$message.'</div>';
}
//We display the form
?>
<div class="content">
<form action="success.php" method="post">
Please type your IDs to log in:
<br />
<div class="center">
<label for="username">Username</label>
<input type="text" name="username" id="username" value="<?php echo htmlentities($ousername, ENT_QUOTES, 'UTF-8'); ?>" />
<br />
<label for="password">Password</label>
<input type="password" name="password" id="password" />
<br />
<input type="submit" value="Log in" />
</div>
</form>
</div>
<?php
}
?>
</div>
<?php
}
?>
</div>
我尝试了以下方法:
我设置当用户登录时,它被重定向到名为 success.php 的页面,其中显示成功消息和后退按钮。
问题是:当用户按下后退按钮(index.php)时,系统无法识别用户已登录并要求他重新登录。就是这样,一个你无法登录的无限循环。
我试了很多方法重新加载页面两次,但似乎很无效
您需要在需要会话的每个 PHP 页面的开头调用 session_start()
并保存它们。此外,我真的建议您将登录逻辑放在一个单独的 PHP 文件中,以提高可读性并能够轻松了解缺少的内容。
利用PHP会话并尝试在会话中存储用户名密码然后重定向。
以下是小示例哦 php 会话如何工作
<?php
ob_start();
session_start();
?>
<?
// error_reporting(E_ALL);
// ini_set("display_errors", 1);
?>
<html lang = "en">
<head>
<title>Tutorialspoint.com</title>
<link href = "css/bootstrap.min.css" rel = "stylesheet">
<style>
body {
padding-top: 40px;
padding-bottom: 40px;
background-color: #ADABAB;
}
.form-signin {
max-width: 330px;
padding: 15px;
margin: 0 auto;
color: #017572;
}
.form-signin .form-signin-heading,
.form-signin .checkbox {
margin-bottom: 10px;
}
.form-signin .checkbox {
font-weight: normal;
}
.form-signin .form-control {
position: relative;
height: auto;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
padding: 10px;
font-size: 16px;
}
.form-signin .form-control:focus {
z-index: 2;
}
.form-signin input[type="email"] {
margin-bottom: -1px;
border-bottom-right-radius: 0;
border-bottom-left-radius: 0;
border-color:#017572;
}
.form-signin input[type="password"] {
margin-bottom: 10px;
border-top-left-radius: 0;
border-top-right-radius: 0;
border-color:#017572;
}
h2{
text-align: center;
color: #017572;
}
</style>
</head>
<body>
<h2>Enter Username and Password</h2>
<div class = "container form-signin">
<?php
$msg = '';
if (isset($_POST['login']) && !empty($_POST['username'])
&& !empty($_POST['password'])) {
if ($_POST['username'] == 'tutorialspoint' &&
$_POST['password'] == '1234') {
$_SESSION['valid'] = true;
$_SESSION['timeout'] = time();
$_SESSION['username'] = 'tutorialspoint';
echo 'You have entered valid use name and password';
}else {
$msg = 'Wrong username or password';
}
}
?>
</div> <!-- /container -->
<div class = "container">
<form class = "form-signin" role = "form"
action = "<?php echo htmlspecialchars($_SERVER['PHP_SELF']);
?>" method = "post">
<h4 class = "form-signin-heading"><?php echo $msg; ?></h4>
<input type = "text" class = "form-control"
name = "username" placeholder = "username = tutorialspoint"
required autofocus></br>
<input type = "password" class = "form-control"
name = "password" placeholder = "password = 1234" required>
<button class = "btn btn-lg btn-primary btn-block" type = "submit"
name = "login">Login</button>
</form>
Click here to clean <a href = "logout.php" tite = "Logout">Session.
</div>
</body>
</html>
Login.php
它将清除会话数据。
<?php
session_start();
unset($_SESSION["username"]);
unset($_SESSION["password"]);
echo 'You have cleaned session';
header('Refresh: 2; URL = login.php');
?>
Logout.php
您在提交代码之前检查了会话值,因此无法在提交时检查会话变量。
我刚刚将您的提交代码块放在会话检查之上,以便它立即生效。
并且记得在每一页上都有 session_start();
。
<div class="content">
<?php
session_start();
$ousername = '';
//We check if the form has been sent
if (isset($_POST['username'], $_POST['password'])) {
//We remove slashes depending on the configuration
if (get_magic_quotes_gpc()) {
$ousername = stripslashes($_POST['username']);
$username = mysql_real_escape_string(stripslashes($_POST['username']));
$password = stripslashes($_POST['password']);
} else {
$username = mysql_real_escape_string($_POST['username']);
$password = $_POST['password'];
}
//We get the password of the user
$req = mysql_query('select password,id from users where username="' . $username . '"');
$dn = mysql_fetch_array($req);
//We compare the submited password and the real one, and we check if the user exists
if ($dn['password'] == $password and mysql_num_rows($req) > 0) {
//If the password is good, we dont show the form
$form = false;
//We save the user name in the session username and the user Id in the session userid
$_SESSION['username'] = $_POST['username'];
$_SESSION['userid'] = $dn['id'];
} else {
//Otherwise, we say the password is incorrect.
$form = true;
$message = 'The username or password is incorrect. Please try again!';
}
} else {
$form = true;
}
//We display a welcome message, if the user is logged, we display it username
?>
Hello<?php if (isset($_SESSION['username'])) {
echo ' ' . htmlentities($_SESSION['username'], ENT_QUOTES, 'UTF-8');
} ?>,<br/>
Welcome on our website.<br/>
You can <a href="users.php">see the list of users</a>.<br/><br/>
<?php
//If the user is logged, we display links to edit his infos, to see his pms and to log out
if (isset($_SESSION['username'])) {
//We count the number of new messages the user has
$nb_new_pm = mysql_fetch_array(mysql_query('select count(*) as nb_new_pm from pm where ((user1="' . $_SESSION['userid'] . '" and user1read="no") or (user2="' . $_SESSION['userid'] . '" and user2read="no")) and id2="1"'));
//The number of new messages is in the variable $nb_new_pm
$nb_new_pm = $nb_new_pm['nb_new_pm'];
// Check if current user is the admin
if ($_SESSION['userid'] == 1) {
include('indexp.php');
} else {
echo 'nu merge';
}
//We display the links
?>
<a href="edit_infos.php">Edit my personnal informations</a><br/>
<a href="list_pm.php">My personnal messages(<?php echo $nb_new_pm; ?> unread)</a><br/>
<a href="logout.php">Logout</a>
<?php
} else {
//Otherwise, we display a link to log in and to Sign up
?>
<a href="sign_up.php">Sign up</a><br/>
<button data-toggle="collapse" data-target="#login">Log in</button>
<div id="login" class="collapse">
<?php
if ($form) {
//We display a message if necessary
if (isset($message)) {
echo '<div class="message">' . $message . '</div>';
}
//We display the form
?>
<div class="content">
<form action="success.php" method="post">
Please type your IDs to log in:<br/>
<div class="center">
<label for="username">Username</label><input type="text" name="username" id="username"
value="<?php echo htmlentities($ousername, ENT_QUOTES, 'UTF-8'); ?>"/><br/>
<label for="password">Password</label><input type="password" name="password" id="password"/><br/>
<input type="submit" value="Log in"/>
</div>
</form>
</div>
<?php
}
?>
</div>
<?php
}
?>
</div>