login.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
<head>
<title>登录页面</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
session_start();
$dbc=@mysqli_connect('127.0.0.1', 'root','123456','todolist')
or die('could not connect to mysql');
mysqli_set_charset($dbc,'utf8');
$error=array();
if($_SERVER['REQUEST_METHOD']=='POST'){
if(!empty($_POST['username'])){
$username=$_POST['username'];
}else{
$error[]="请输入用户名";
}
if(!empty($_POST['password'])){
$password=$_POST['password'];
}else{
$error[]="请输入密码";
}
if(empty($error)){
$q="select user_id from usermessage where name='$username' ";
$r=mysqli_query($dbc,$q);
$q1="select user_id from usermessage where password='$password' ";
$r1=mysqli_query($dbc,$q1);
if(mysqli_num_rows($r)==0){
echo'对不起,此用户名并未注册。请先<a href="register.php">注册</a>';
}else{
while($row=mysqli_fetch_array($r,MYSQLI_ASSOC)){
$user_id=$row['user_id'];
}
if(mysqli_num_rows($r1)==0){
echo"密码错误,请重新输入";
}else{
$_SESSION['username']=$username;
$_SESSION['user_id']=$user_id;
//$_SESSION=mysqli_fetch_array($r,MYSQLI_ASSOC);
echo"{$_SESSION['username']},欢迎您使用todolist. <br />";
echo"想进入到你的计划列表<a href=\"list.php\">请点击这里</a> <br />";
echo"<a href=\"logout.php\">注销</a>";
echo"<br />";
echo"<a href=\"changepassword.php\">更改密码</a>";
}
}
mysqli_close($dbc);
exit();
}else{
echo'<h2> Error!</h2>
The following error(s) occurred:</br>';
foreach($error as $msg){
echo"$msg\n";
}
echo'<p>Please try again.</p>';
}
}
?>
<form action="login.php" method="POST">
用户名:<br />
<input type="text" name="username" />
<br />
密码:<br />
<input type="password" name="password" />
<br />
<input type="submit" value="登录" />
</form>
<a href="changepassword.php">更改密码</a>
<br />
<a href="index.php">返回首页</a>
</body>
</html
1.首先,你要已经注册成功。链接数据库。
2.一个登陆表单
<form action="login.php" method="POST">
用户名:<br />
<input type="text" name="username" />
<br />
密码:<br />
<input type="password" name="password" />
<br />
<input type="submit" value="登录" />
</form>
3.判断表单的传送方法,符合后,依次判断用户名、密码是否已填写,若未填写,则输出一个错误提醒,放在$error数组中。
if($_SERVER['REQUEST_METHOD']=='POST'){
if(!empty($_POST['username'])){
$username=$_POST['username'];
}else{
$error[]="请输入用户名";
}
if(!empty($_POST['password'])){
$password=$_POST['password'];
}else{
$error[]="请输入密码";
}