-1

我试图学习 SQL 来进行用户登录,现在我得到了一些开源,我上网了解更多的语言并尝试从示例中学习。

现在我无法弄清楚本地主机;我的意思是我尝试添加表(命名用户),我只添加了 3 个详细信息:

Name      | Data Type | Default Value | Is Primary Key? | Is Identity? | Allow Nulls
Localhost | nvarchar  | Null -------- | No ------------ | No --------- | Yes
user      | nvarchar  | Null -------- | No ------------ | No --------- | Yes
pass      | nvarchar  | Null -------- | No ------------ | No --------- | Yes

--- 不是代码的一部分,它只是为了在表中排序:)。

无论如何,这是我的代码:

<?php

// Edit your mssql info here
// BEGIN MSSQL INFO
$CONFIG['host'] = "localhost";
$CONFIG['user'] = "sa";
$CONFIG['pass'] = "server";
// END MSSQL INFO

//----------------------------- DO NOT EDIT ANYTHING BELOW HERE !!!!! ------------------------------------

$CONFIG['conn']  = mssql_connect( $CONFIG['host'], $CONFIG['user'], $CONFIG['pass']);

function anti_injection($sql) {
   $sql = preg_replace(sql_regcase("/(from|select|insert|delete|where|drop table|show tables|#|\*|--|\\\\)/"),"",$sql);
   $sql = trim($sql);
   $sql = strip_tags($sql);
   $sql = addslashes($sql);
   return $sql;
}
?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>Test</title>
    </head>
    <body>
    <?php
    if(isset($_GET['action']) && ($_GET['action'] == "login")){

        $user = anti_injection($_POST['user']);
        $pass = anti_injection($_POST['pass']);
        $crypt_pass = md5($pass);

        $result1 = mssql_query("SELECT * FROM account.dbo.user_profile WHERE user_id = '".$user."'"); 
        $count1 = mssql_num_rows($result1); 

        $result2 = mssql_query("SELECT user_pwd FROM account.dbo.user_profile WHERE user_id = '".$user."'"); 
        $row2 = mssql_fetch_row($result2); 

        if($count1 == '0') {
            echo '<br>This game account is not found in the database.';
        }
        elseif($row2[0] != $crypt_pass) {
            echo '<br>Wrong password. Try again.';
        }
        elseif($_GET['login'] != 'login' && $count1 == '0') {
            echo '<br>Login Error, Please login again.';
        } else {

        // Begin secure content 
            $_SESSION['user'] = $user;
            echo "<h3>Welcome, ".$_SESSION['user']."</h3>";
            echo "<br>";
            echo "Your content here";
        // Dont forget to and your session
        // session_destroy();
        // End secure content
        }
    } else {

    echo '<h2>Login here</h2><br /> 
        <form name="" action="'.$_SERVER['php_self'].'?action=login" method="post"> 
            Name: <input type="text" name="user" maxlength="16"><br /> 
            Password: <input type="password" name="pass" maxlength="16"> <br />
            <input type="submit" value="Login!"> 
        </form>';
    }
    ?>
    </body>
</html>

正如我所说,我真的不知道如何在表格和代码之间建立联系。

你可以解释吗?

4

1 回答 1

1

使用mysql_real_escape_string()手动)保护您免受 SQL 注入

您与数据库的连接保存在一个变量中,您必须像这样将其添加到您的查询中

$result1 = mssql_query("Your Query", $CONFIG['conn']);
$count1 = mssql_num_rows($result1);
于 2013-04-05T21:58:05.667 回答