login.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'对不起,此用户名并未注册。请先注册';
}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.
";
echo"想进入到你的计划列表请点击这里
";
echo"注销";
echo"
";
echo"更改密码";
}
}
mysqli_close($dbc);
exit();
}else{
echo'
Error!
The following error(s) occurred:';
foreach($error as $msg){
echo"$msg\n";
}
echo'
Please try again.
';}
}
?>
用户名:
密码:
更改密码
返回首页
1.首先,你要已经注册成功。链接数据库。
2.一个登陆表单
用户名:
密码:
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[]="请输入密码";
}