2个弄好了蝙蝠,像瓦利德说你打开SQL注入,不是很好的事情已经发生:如果继续使用旧的API mysql_*()
你必须逃脱其对SQL注入值给你。我会考虑阅读关于MySQLi和PDO的教程,从那里尝试并潜入更好的方式或运行查询。
另外您还选择使用Cookie而不是会话来存储用户名?客户端可以修改Cookies,以表示任何有萤火虫的聪明用户希望他们成为的人。会话存储在服务器端,而客户端(最终用户)只有会话的ID。如果您将它作为会话发送,它们不能修改用户名。 (他们可以尝试更改会话ID到另一个随机一串数字,但多数民众赞成像撒尿进风,原谅我的法语。
下面有一些pseduo代码,将让你的方式,我觉得
<?php
include("dbconnect.php");
$database = "maxgee_close2"; //Set the database you want to connect to
mysql_select_db($database); //Select database
$username = $_SESSION['maxgee_me_user']; //Grab the username from a server-side stored session, not a cookie!
$query = "SELECT user_id FROM `users` WHERE `username` = '" . mysql_real_escape_string($username) . "' LIMIT 1"; //Note the user of mysql_real_escape_string on the $username, we want to clean the variable of anything that could harm the database.
$result = mysql_query($query);
if ($row = mysql_fetch_array($result)) {
//Query was ran and returned a result, grab the ID
$userId = $row["user_id"];
mysql_free_result($result); //We can free the result now after we have grabbed everything we need
$query_check = "SELECT * FROM `events_main` WHERE `user_id` = '" . mysql_real_escape_string($userId) . "'";
$check = mysql_query($query_check);
if (mysql_num_rows($check)>0) {
include("example.php");
}
else {
echo "example";
}
}
?>
该代码可以/不可以工作,但真正的关键变化是事实,你正在运行 了mysql_free_result($结果); 脚本有机会从数据库中获取用户ID之前
总而言之,我真的会回去阅读一些教程。
您已经开放SQL注入。 –
请不要使用'mysql_ *'函数,它已被弃用(请参阅[*红盒子*](http://php.net/manual/en/function.mysql-query.php)),并且容易受到sql注入。使用[* PDO *](http://php.net/manual/en/book.pdo.php)或[* MySQLi *](http://php.net/manual/en/book.mysqli.php) 。 – alfasin
在双引号SQL字符串中,您应该_not_引用数组键'user_id',或者更好地将'{}'中的所有内容包装在'user_id ='{$ row ['user_id']}' ,您的变量解析将在数组键的第一个引号中断,从而破坏您的SQL语句。 –