Tao login form bang php

StarWar

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

<body>
<?php
    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
        
        if(empty($errors)) 
            {
                        //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']);
                        $hash_pw=sha1($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));
                        if(mysqli_num_rows($result)==1)  
                        {
                            $_SESSION['email']=$_POST['email'];
                            $_SESSION['pword']=$_POST['pword'];
                            while($rows = mysqli_fetch_array($result, MYSQLI_ASSOC)) //Cau 5: Giai thich vong while
                            {
                                header ('Location: admin.php');    
                            }//end while
                        }else  
                                $errors[]="The email or password do not match those on filed.";
            
            }//end empty $errors
    }//end isset($_POST['submmit']
?>

<div id="wrapper">
<?php
    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">
        
        <p>
            <label for="email">    Email</label>
            <input type="text" name="email" />
        </p>
        
        <p>
            <label for="password">Password</label>
            <input type="password" name="pword" />
        </p>
        
        <p>
            <input type="submit" name="submit" value="submit" />
        </p>
    </form>
<div class='login'>
    <a href="#">Login</a>
</div>
</body>

</html>
 
Sửa lần cuối:
Back
Top