javascript - PHP主页重定向空白

标签 javascript php jquery html mysql

我对这段代码有疑问。当我登录时,它不会带我进入主页,而是停留在登录页面上。我想在成功登录后将用户重定向到 ( http://localhost/trial/index.php#Home )。如何解决这个问题?

<!-- Including header file which contains j query and other libraries -->
<?php include("inc/incfiles/header.inc.php"); ?>
<?php
//Check if user is logged in or not
if (!isset($_SESSION["user_login"])) {
//Verification
}
else
{
 //Do nothing
}
?>

<?php
//Login Script
//user Login code
//Check user info when user inputs login information
if (isset($_POST['user_login']) && isset($_POST['password_login']) )
{
//filters input info
$user_login = preg_replace('#[^A-Za-z0-9)]#i','', $_POST['user_login']);//filters everything but numbers and letters
$password_login = preg_replace('#[^A-Za-z0-9)]#i','', $_POST['password_login']);//filters everything but numbers and letters
$password_login_md5 = md5($password_login); // encrypt password input because password in database is already encrypted in md5
//use Binary for case sensitive option
$sql = mysqli_query($con, "SELECT * FROM users WHERE BINARY username= BINARY'$user_login' AND password='$password_login_md5' AND closed='no' LIMIT 1"); //query
//check for existence if user exists
$userCount = mysqli_num_rows($sql); //Count the number of rows returned
//if username exists start session
if($userCount==1)
{
while($row = mysqli_fetch_array($sql)) //fecthing the row to display information
{
$id = $row["id"]; // store user id into variable called $id
}
$_SESSION["id"] = $id;  
$_SESSION['user_login'] = $user_login;
$_SESSION["password_login"] = $password_login;
echo "succes!";
header("Location:#Home");
//exit("<meta http-equiv=\"refresh\" content=\"0\">");              
}
else{echo"That information is incorrect, Please try again!";}
exit(); 
}
?>  

<!-- The welcome page where users must provide login info in order to be logged in -->
<div data-role="page" id="Welcome">
<div role="main" id="loginform">        
React now
<form action="" method="POST"> <!--provide username and password then submit -->
<input name="user_login" size= "25" type="text"><br /><br /><!-- Enter username /include username placeholder later-->
<input data-clear-btn="false" name="password_login" size= "25" type="password"><br /><br /><!-- Enter password /include password placeholder later-->
<input name="login" value="Login" type="submit" data-theme="a"/><!-- submit button style it later -->
</form>
<div>
<a href="#Sign Up" data-role="button">Sign Up</a> <!--Redirect user to sign up page if user not member yet-->
</div>  
</div>
<div data-role="footer" data-position="fixed" data-theme="a"><!-- Footer display/ displays once-->
<h4>(C) 2016</h4> <!-- copyright symbols include later-->
</div>
</div>  <!-- End of the login page-->       

<!-- Sign up page where to allow users to sign up-->
<div data-role="page" id="Sign Up">
<div data-role="header" data-theme="a">
<h1>Sign Up</h1>
</div><br><br><br><br>
Sign Up for Reactr
<form>
<!-- Just left the form blank for the moment to make the code smaller and easy to read-->
</form>
<div role="main" class="ui-content">
</div>
</div><!-- End of the sign up page-->


<!-- HOME PAGE AND USER PROFILE PAGE where users can enter and submit texts-->
<div data-role="page" id="Home">
<div data-role="header" data-theme="a"><!-- Jquery settings ref included in the header file-->
<h1>Text</h1>
</div>
<!-- Allow users to search for posted texts-->
<div class="search_box">
<!-- Setting form to allow users to type text, send and search for texts-->
<form action="search.php" method="GET" id="search"><!-- Search form -->
<input type="text" name="q" size="60"/><!-- Search for text /include search placeholder later-->
</form>
</div>
<div role="main" class="ui-content">
Enter your Text<br><!-- Enter and send text -->
<input name="text-basic" id="text-basic" value="" type="text">
<a href="" data-role= "button" data-theme="a" onClick="submittext(Q)">Send</a><!-- submit button with onclcick function -->
</div>
</div><!-- End of the Home page-->
</body><!-- End code-->
</html>

最佳答案

您至少有两个问题:

1 - 最重要的是,您在使用 PHP 发送一些内容后尝试重定向,这是不允许的:

echo "succes!";
header("Location:#Home");

删除 echo 行来解决此问题。

2 - 您还需要更新重定向以重定向到服务器端的某个位置。 PHP 不知道客户端指令,例如页面上的命名 anchor (在您的情况下为 #Home)

header("Location:#Home");

将此更新为 header("Location:index.php#Home"); 来解决它。请注意,如果您的index.php 是您的主页,您可能根本不需要#Home

关于javascript - PHP主页重定向空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37904049/

相关文章:

javascript - 自动完成 JQuery-UI 未定义列表

javascript - 使用 JQuery 使用对象填充表

php - 简单的 php "Undefined index"表单提交错误

javascript - 如何将元素放在绝对位置的元素下?

javascript - 为什么我的 click() 函数仅适用于调整大小?

javascript - 更改 jQueryUI 选项卡标题

javascript - AngularJS 版本 6.* 服务方法即将推出未定义

javascript - 如何在 javascript HTTP 请求中制作 HTML 标签?

php - Codeigniter 允许在插入查询中使用单引号

php - CakePHP:如何包含所有顶级关联