登录index.html页面

时间:2013-03-31 23:41:48

标签: php html redirect login

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

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

的index.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>

的login.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");

?>

2 个答案:

答案 0 :(得分: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 } ?>

答案 1 :(得分: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>';


        }
    }
}