<?php
session_Start();
if (isset($_POST['LOGIN']))
{
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mysite";
\t
\t
\t $conn = new mysqli($servername, $username, $password, $dbname);
\t
\t
\t if ($conn->connect_error)
\t
\t
{
die("Connection failed: ".$conn->connect_error);
}
\t else
{
\t $result1 = "SELECT userid, password FROM user WHERE username = '$username' And password = '$password'";
\t $queryResult = $conn->query($result1);
\t }
\t
\t if ($queryResult > 0)
\t {
\t
\t \t
\t \t
\t \t $_SESSION['username'] = $username;
\t \t header("location: home.php");
\t }
\t else
\t {
\t \t echo 'The username or password are incorrect!';
\t }
}
?>
<style>
.header{
width:100%;
background-color:#00adff;
float:left;
color:white;
padding: 1%;
}
.rightChuck{
float:right;
width:18%;
}
.menu{
float:left;
padding:5%;
}
#title
{
float:left;
margin-left:10px;
}
body{
margin:0px !important;
}
h1{
margin: 0px !important;
}
#main
{
text-align:left;
}
#content{
\t margin: auto;
position: absolute;
top: 100;
bottom: 100;
left: 100;
right: 100;
background: white;
\t border: 5px solid green;
width: 400px;
height: 200px;
\t
}
</style>
<html>
<head>
<title>Mysite.com</title>
</head>
<body>
<div class="header">
<div id="title">
<h1>Mysite.com</h1>
</div>
<div class="rightChuck">
<div class="menu">
<a href="login.php">LOGIN </a>
</div>
<div class="menu">
<a href="register.php">REGISTER</a>
</div>
</div>
</div>
<div style="float:left;width:100%;padding:left:10px;">
<h2>LOGIN</h2>
</div>
<div id="content">
<center>
<form name="form" method="post">
<table cellpadding="5" cellspacing="10">
<tr><th ><h3>USERNAME</h3></th><td><input type="text" placeholder="Username" name="username" size="20" id="textbox" required /></td></tr>
<tr><th><h3>PASSWORD</h3></th><td><input type="password" placeholder="Password" id="password"name="password" size="20" id="textbox" required /></td></tr>
</td>
</table>
<input type="submit" name="LOGIN" value="LOGIN" id="buttondesign" />
<br />
<br />
</form>
</div>
</body>
</html>
這個登錄頁面不能正常工作時,正確的做法是,當我點擊登錄按鈕,它的進入從DATABSE檢查用戶名和密碼,如果用戶名和密碼是正確的,它的用戶名和用戶標識存儲在會話中,它將轉到主頁,否則用戶名和密碼是錯誤的,它會出現錯誤消息。但是這個代碼包含一些錯誤,比如我點擊登錄按鈕它回到主頁頁面,而不是檢查用戶名和密碼,那麼會話不會被存儲。在形式上
<form name="form" method="post">
是在session_start();不是session_Start(); – Roy