Tao login form bang php


<!doctype html>
<meta charset="utf-8">    
<title>Bai tap login</title>
<link href="style.css" type="text/css" rel="stylesheet"/>

    session_start();//Cau 1
    if(isset($_POST['submit'])) //Kiem tra user da submit chua
        $errors = array();//Khoi tao mang errors rong
        $required = array('email','pword');//Tao mang required gom email va password
[/COLOR]        foreach($required as $fieldname) //Chay bien required den khi gap gia tri rong 
            //Kiem tra fieldname
            if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname])){                 {
                    $errors[] = "The <strong> $fieldname </strong> was left blank.";
                }//End: if
        }//End: foreach
                        //Ket noi den database baitap voi tai khoan root va pass la rong
                        $conn = mysqli_connect('localhost','root','','baitap') or die ('Could not connect to database');
                        $email= mysqli_real_escape_string($conn, $_POST['email']);//Cau 3:Giai thich cau lenh,cu phap
                        $pword= mysqli_real_escape_string($conn, $_POST['pword']);
                        $query= "SELECT CONCAT_WS('  ',first_name, last_name)
                        AS name
                        FROM user2
                        WHERE email='{$email}'
                        AND pword='{$hash_pw}'
                        LIMIT 1 ";//Cau 4:Giai thich cau truy van,ham CONCAT_WS
                        $result =mysqli_query($conn,$query) or die (mysqli_error($conn));
                            while($rows = mysqli_fetch_array($result, MYSQLI_ASSOC)) //Cau 5: Giai thich vong while
                                header ('Location: admin.php');    
                            }//end while
                                $errors[]="The email or password do not match those on filed.";
            }//end empty $errors
    }//end isset($_POST['submmit']

<div id="wrapper">
    if(!empty($errors))//Neu $errors co gia tri thi in ra man hinh
        foreach($errors as $error) //Cau 2:php o may loai vong lap,foreach khac gi voi nhung loai kia,vi du minh hoa
            echo "<ul>";
            echo "<li>".$error."</li>";
            echo "</ul>";
        }//end foreach

    <form action='baitap.php' method="post">
            <label for="email">    Email</label>
            <input type="text" name="email" />
            <label for="password">Password</label>
            <input type="password" name="pword" />
            <input type="submit" name="submit" value="submit" />
<div class='login'>
    <a href="#">Login</a>

Last edited: