Tao login form bang php

StarWar

Internship/Fresher
Dec 8, 2013
28
0
0
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>
 
Last edited:

About us

  • Securityzone.vn là một trang web chuyên về an ninh mạng và công nghệ thông tin. Trang web này cung cấp các bài viết, tin tức, video, diễn đàn và các dịch vụ liên quan đến lĩnh vực này. Securityzone.vn là một trong những cộng đồng IT lớn và uy tín tại Việt Nam, thu hút nhiều người quan tâm và tham gia. Securityzone.vn cũng là nơi để các chuyên gia, nhà nghiên cứu, sinh viên và người yêu thích an ninh mạng có thể trao đổi, học hỏi và chia sẻ kiến thức, kinh nghiệm và giải pháp về các vấn đề bảo mật trong thời đại số.

Quick Navigation

User Menu