Я новичок, когда дело доходит до 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), система не распознает, что пользователи вошли в систему, и просит его снова войти в систему.И это все, бесконечный цикл, в который вы не можете войти.
Я перепробовал много тактик, чтобы дважды перезагрузить страницу, но это кажется очень неэффективным.