1

我目前在我的索引页面上有一个登录系统;这工作正常。但是,当用户登录时,它会将他们定向到我不想要的全新页面。如何实现代码以便;

A) 用户登录后停留在索引页面 B) 在索引页面上显示欢迎消息“Welcome....”。

索引.html

<div id="leftmenu_top"></div>

                <div id="leftmenu_main"> 
                <div class="login">

                <h3>Please login below</h3>
                <br/>

                <!--Log in form-->
                <html>

<form action='login.php' method='POST'>
Username: <input type='text' name='username'><br>
Password: <input type='password' name='password'><br>
<input type='submit' value='Log in'>
</form>
</html>

登录.php

<?php

session_start();

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

if ($username&&$password)
{

$connect = mysql_connect("localhost", "root", "") or die("Couldn't connect");
mysql_select_db("a&e") or die("Couldn't find db");

$query = mysql_query("SELECT * FROM users WHERE username='$username'");

$numrows = mysql_num_rows($query);

if ($numrows==!0)
{
//code to login

while ($row = mysql_fetch_assoc($query))
{
    $dbusername = $row['username'];
    $dbpassword = $row['password'];
}

//check to see if they match
if($username==$dbusername&&$password==$password)
{
echo "Welcome $username";
$_SESSION['username']=$username;

}
else
echo "incorrect password";

}
else
    die("That user does not exist");


}
else
    die("please provide a  username and password");

?>
4

2 回答 2

1

header('Location: index.php');

在 login.php 的底部

此外,如果您在此链接的末尾添加 ?success,然后在您的索引页面中调用它,它将显示您想要的消息。把上面的代码改成

header('Location: index.php?status=success');

然后在您的索引页面中将其放在您希望消息出现的位置

<?php If($_GET['status'] = "success") { ?>
    <p>This is a welcome message</p>
<?php } ?>
于 2013-03-31T23:59:18.147 回答
0
<?php if(isset($_POST['submit'])){

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

    $query = "SELECT Username, Password FROM tbluser
              WHERE Username = '$username' AND Password = '$password'";
    $result = mysql_query($query) or die (mysql_error());
       if(mysql_num_rows($result) == 0){
        echo ' Password/Username is not found';
    }else {
        while($row = mysql_fetch_array($result)) {
          echo 'You are logged in </br>';


        }
    }
}
于 2013-03-31T23:45:56.937 回答