简体   繁体   中英

Log in page on index.html

I currently have a login system on my index page; which is working fine. However, when a user logs in, it directs them to a completely new page which i don't want. How to I implement the code so that;

A) the user stays on the index page after login B) A welcome message "Welcome...." displays on the index page.

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");

?>

put

header('Location: index.php');

at the bottom of the login.php

Also, if you add ?success at the end of this link and then call it in your index page it will display the message you want. Change the above code to

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

and then in you index page put this where you want the message to appear

<?php If($_GET['status'] = "success") { ?>
    <p>This is a welcome message</p>
<?php } ?>
<?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>';


        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM