我正在使用一个 post 变量,我正在尝试使用 url 中的一个 get 变量来登录用户。
过程如下:
用户获得自己的 url,即 kayden.domain.com
当他们创建自己的帐户时,他们有自己的用户名和密码
当他们访问 kayden.domain.com 时,他们会使用这些凭据。
为了验证,我正在检查发布变量(用户名和密码)并尝试使用 $_GET['user_group'] 进行验证。
当我只使用 post 变量时脚本可以工作,但是当涉及到 GET 时它不起作用。下面是代码:
PHP:
$SUBDOMAIN = mysql_real_escape_string($_GET['user_group']);
// Process the POST variables
$username = $_SESSION["user_name"];
//$password = $_POST["password"];
// Set up the session variables
$_SESSION["user_name"] = $username;
$secret = $info['password'];
//Checks if there is a login cookie
if(isset($_COOKIE['ID_my_site']))
//if there is, it logs you in and directes you to the members page
{
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$check = mysql_query("SELECT user_name, password FROM accounts WHERE user_name = '$username' and user_group='$user_group'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{
if (@ $info['password'] != $pass)
{
}
else
{
header("Location: members.php");
}
}
}
//if the login form is submitted
if (isset($_POST['submit'])) { // if form has been submitted
// makes sure they filled it in
if(!$_POST['user_name'] | !$_POST['password']) {
die('You did not fill in a required field.');
}
// checks it against the database
if (!get_magic_quotes_gpc()) {
$_POST['user_name'] = addslashes($_POST['user_name']);
$_GET['user_group'] = addslashes($_GET['user_group']);
}
$check = mysql_query("SELECT user_name,password FROM accounts WHERE user_name = '".$_POST['user_name']."' and user_group='".$_GET['user_group']."'")or die(mysql_error());
//Gives error if user dosen't exist
$check2 = mysql_num_rows($check);
if ($check2 == 0) {
die('That user does not exist in our database. <a href=add.php>Click Here to Register</a>');
}
while($info = mysql_fetch_array( $check ))
{
$_POST['password'] = md5($_POST['password']);
$_POST['password'] = $_POST['password'];
//gives error if the password is wrong
if (@ $_POST['password'] != $info['password']) {
die('Incorrect password, please try again');
}
else
{
// if login is ok then we add a cookie
$_POST['user_name'] = stripslashes($_POST['user_name']);
$hour = time() + 3600;
setcookie(ID_my_site, $_POST['user_name'], $hour);
setcookie(Key_my_site, $_POST['password'], $hour);
//then redirect them to the members area
header("Location: members.php");
}
}
}
else
{
// if they are not logged in
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<table border="0">
<tr><td colspan=2><h1>Login</h1></td></tr>
<tr><td>username:</td><td>
<input type="text" name="user_name" maxlength="40">
</td></tr>
<tr><td>Password:</td><td>
<input type="password" name="password" maxlength="50">
</td></tr>
<tr><td colspan="2" align="right">
<input type="submit" name="submit" value="Login">
</td></tr>
</table>
</form>
<?php
}
?>
最佳答案
两层答案:
第 1 部分
您可以使用 PHP $_REQUEST 变量从 GET(查询字符串)、POST 和 COOKIE 获取详细信息。
例如:
$ugData = $_REQUEST['user_group'];
$unData = $_REQUEST['user_name'];
可以在此处找到更多相关信息:
http://php.net/manual/en/reserved.variables.request.php
第 2 部分
代码中的这一行:
$check = mysql_query("SELECT user_name,password FROM accounts WHERE user_name = '".$_POST['user_name']."' and user_group='".$_GET['user_group']."'")or die(mysql_error());
易受 SQL 注入(inject)攻击,恶意用户可以制作包含 user_group
或 user_name
值的请求,该值包含额外的 SQL,您的脚本将毫无疑问地执行它。
您应该始终验证任何外部输入,因为您不能相信它会始终包含您期望的内容。
可以找到有关此的更多信息:
http://php.net/manual/en/security.database.sql-injection.php http://stackoverflow.com/questions/60174/best-way-to-stop-sql-injection-in-php
如何解决这个问题的一个简单示例是:
$ugData = mysql_real_escape_string($_REQUEST['user_group']);
$unData = mysql_real_escape_string($_REQUEST['user_name']);
这将转义 $_REQUEST
输入,从而阻止任何制作恶意请求的人。但是,它不会验证给定的 user_group/user_name 是否为有效值。
关于php - 使用从 URL 获取变量来执行 MYSQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4747207/