如何使用mysql数据库表进行login.php和验证

时间:2014-11-05 08:18:14

标签: php html mysql validation

如何使用mysql数据库表两列验证login.php。我的意思是我在数据库套件下有表用户。有两列:user和pwd。

我必须制作客户端引入用户和密码的基本HTML或PHP页面,如果密码或密码正确,它将重定向到report.php。

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

##session and include session stored variable
session_start();

##get connection
    $link = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
    mysql_query("SET NAMES 'utf8'"); 

    ##check database
    mysql_select_db(DB_NAME,$link) or die(DB_ERROR_LOGS);

    ##do operation
    if($_REQUEST["op"]=="login"){
        sleep(1); 
        ##encrypt password by md5
        $md5_pwd = md5($_POST["Password"]);
        $tmp_account = $_POST["Username"];
        $caselower_account = strtolower($tmp_account);
        $account_approve = "false";
        $sql_login_account = "SELECT * FROM users WHERE name ='".$caselower_account."';";
        $result = mysql_query($sql_login_account) or die("Invalid Username");
        while($login_account = mysql_fetch_assoc($result)){
            if($login_account['password'] == $md5_pwd){
                $account_approve = "true";
                $_SESSION['login'] = "true";
                $_SESSION['user'] = $caselower_account;
                setcookie("cookiename","cookiename",time() + 3600,'/');
                echo "success";
            }else{
                $account_approve = "false";
            }
        }
        ##if true, redirect to report.php
        if($account_approve=="true"){
            header("Location:report.php"); 
        }
        ##if false, redirect to login.php to re-enter your username and password
        else{
            $_SESSION['login'] = "false";
            setcookie("cookiename","",time() - 3600,'/');
            header("Location:login.php");  
        }
    }elseif($_REQUEST["op"]=="logout") {
        $_SESSION['login'] = "false";
        setcookie("cookiename","",time() - 3600,'/');
        header("Location:login.php");
    }else{
        $_SESSION['login'] = "false";
        setcookie("cookiename","",time() - 3600,'/');
        header("Location:login.php");
    }

我使用session将inputbox的用户名和密码传递给此页面。 相反,您可以使用post将您的用户名和密码传递给此页面。