Php主页重定向空白

时间:2016-06-19 04:51:50

标签: 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>

2 个答案:

答案 0 :(得分:1)

你至少有两个问题:

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

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

删除echo行以解决此问题。

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

header("Location:#Home");

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

答案 1 :(得分:0)

重定向中的#符号会导致停留在同一页面上的问题 - 因为这表明它是您已经在的页面中的锚点位置。您需要将其更改为:

try
{
    String id = txtId.Text;
    String name = txtName.Text;
    String tel = txtTel.Text;
    String add = txtAdd.Text;

    String SqlQuery = @"INSERT INTO [Table]
                        VALUES(@id, @name, @tell, @add)";

    using (SqlConnection con = new SqlConnection(conString))
    using (SqlCommand cmnd = new SqlCommand(SqlQuery, con))
    {
        con.Open();

        cmnd.Parameters.Add("@id", SqlDbType.NVarChar).Value = id;
        cmnd.Parameters.Add("@name", SqlDbType.NVarChar).Value = name;
        cmnd.Parameters.Add("@tel", SqlDbType.NVarChar).Value = tel;
        cmnd.Parameters.Add("@add", SqlDbType.NVarChar).Value = add;

        cmnd.ExecuteNonQuery();

        MessageBox.Show("Saved Sucessfully", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message, "Error occurred while saving", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

另外 - 如果您只是想将用户重定向到主页的顶部 - 根本不需要它 - 您只会尝试将它们带到页面下方存在的锚点。如果您试图将它们引导到hte页面的顶部,只需使用没有列出锚点的相对路径:

header("Location: index.php#Home");