<?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