gpt4 book ai didi

javascript - 登录后页面刷新?

转载 作者:行者123 更新时间:2023-11-27 23:41:53 25 4
gpt4 key购买 nike

我想让我的页面在登录后刷新,这是我的 index.php 的格式

<?php
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username']))
{
// let the user access the main page
// i have made the main content with jquery mobile framework
}
elseif(!empty($_POST['username']) && !empty($_POST['password']))
{
// let the user login
include "login.php";

}
else
{
// display the login form
<form method="post" action="index.php" name="loginform" id="loginform">
<fieldset>
<label for="username">Username:</label><input type="text" name="username" id="username" /><br />
<label for="password">Password:</label><input type="password" name="password" id="password" /><br />
<input type="submit" name="login" id="login" value="Login" />
</fieldset>
</form>

在我的 login.php 里面

<?php

$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);

$checklogin = mysql_query("SELECT * FROM users WHERE Username = '".$username."' AND Password = '".$password."'");

if(mysql_num_rows($checklogin) == 1)
{
$row = mysql_fetch_array($checklogin);
$email = $row['email'];
$uid = $row['u_id'];

$_SESSION['Username'] = $username;
$_SESSION['EmailAddress'] = $email;
$_SESSION['LoggedIn'] = 1;
$_SESSION['uid'] = $uid;
$flag=1;

header("Location: http://5aday.dihops.net/index.php");
// echo "<meta http-equiv=\"refresh\" content=\"0;index.php\">";
//header("Refresh: 2; url=http://5aday.dihops.net/index.php");
// THIS IS WHERE I AM STUCK
}
else
{
echo "<h1>Error</h1>";
echo "<p>Sorry, your account could not be found. Please <a href=\"index.php\">click here to try again</a>.</p>";
}
?>
}
?>

我不知道我做错了什么。但是一旦用户提供了他的用户名和密码并提交......我得到一个空白页面。并且用户必须手动刷新网页才能看到网页内容。

最佳答案

根据 the link提到 header(location) 必须放在页面的顶部......所以我重新安排了 if 条件......

我在 index.php 的顶部添加了这个

if(!empty($_POST['username']) && !empty($_POST['password']))
{
include "login.php";


} ?>

然后在正文中我添加了其他 if 语句

if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username']))
{
// let the user access the main page
// i have made the main content with jquery mobile framework
}else
else
{
// display the login form
<form method="post" action="index.php" name="loginform" id="loginform">
<fieldset>
<label for="username">Username:</label><input type="text" name="username" id="username" /><br />
<label for="password">Password:</label><input type="password" name="password" id="password" /><br />
<input type="submit" name="login" id="login" value="Login" />
</fieldset>
</form>
}

这已经解决了问题。

关于javascript - 登录后页面刷新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31384985/

25 4 0
文章推荐: javascript - 如何使用 jQuery/JavaScript 通过用户输入更改
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com