2 Waleed が SQL インジェクションに対してオープンであると述べたように、すぐに発生したことは、あまり良いことではありません。MySQLi と PDO に関するチュートリアルを読んで、そこからより良い方法やクエリの実行に飛び込んでみます。
また、セッションの代わりに Cookie を使用してユーザー名を保存することを選択していますか? Cookie はクライアント側で変更して、firebug を持つ賢いユーザーが望んでいることを何でも言うことができます。セッションはサーバー側に保存され、クライアント (エンドユーザー) にはセッションの ID のみが与えられます。セッションとして送信した場合、ユーザー名を変更することはできません。(彼らはセッションIDを別のランダムな数字に変更しようとするかもしれませんが、それは風に小便をするようなものです、私のフランス語を許してください.
私が思うにあなたをあなたの道に連れて行くいくつかの疑似コードをここに示します
<?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($result); を実行していたという事実です。スクリプトがデータベースからユーザー ID を取得する前に。
全体として、私は本当に戻って、さらにいくつかのチュートリアルを読みます.