5

ユーザーにログインし、PHP スクリプトでセッション変数を作成し、それにユーザー名を割り当てます (一意であるため)。

<?php
session_start();
//$username= getting username from database and all after loggin
$_SESSION['user_of_mywebsie']=$username;
// using $username now

今、これは正しく安全な方法ですか?そうでない場合、何ができるでしょうか?これで、ユーザー名を含むセッションが作成されます..

$sql_query=mysql_query("SELECT * FROM people WHERE username='$_SESSION['user_of_mywebsite']'");
while($row=mysql_fetch_assoc($sql_query))
{
$name=$row['name'];
$profile_pic=$row['pro_pic_location'];
}

//now i will use $name and $profile_pic furthur 
4

4 に答える 4

2

いいえ、正しくありません。あなたのコードはSQL インジェクションに対して脆弱です。ユーザー名が のような場合はどうなりRobert'); DROP TABLE Students;--ますか?

少なくともデータをエスケープするか、準備されたステートメントとバインドされたパラメーターをより適切に使用する必要があります。

PHPでSQLインジェクションを防ぐにはどうすればよいですか?

また、非推奨のデータベース API を使用しています。またはを使用しますmysqli_*PDO

于 2013-06-11T20:48:27.177 に答える
1

This will store the username in the session, but is not particularly secure.

If the only thing you are holding in the session is the username, this is probably fine, but if you have personal information stored in the users session, you might want to think about adding some security.

I found this handy looking quick tutorial for session safety http://phpsec.org/projects/guide/4.html

Furthermore, If you are new to php programming, it is probably better to use one of the frameworks that implements user management, as this has been done many times before, tested and perfected. No need to reinvent the wheel here.

If you do use a framework, you can check out this question about frameworks and user management and particularly this answer : https://stackoverflow.com/questions/10153619/looking-for-a-well-written-user-management-framework#10624058

于 2013-06-11T20:21:02.653 に答える
1

これは完全に正しいです!$_SESSION 変数には、あなたが $_SESSION に書き込んだ内容に関係なく、Web サイトの特定の訪問者に関する情報のみが含まれます (ただし、公平を期すために、共有ホスティングを使用している場合や、マルチアプリ設定など)。

私はこれとまったく同じようにやっています(これはあなたのものとほぼ同じです)

$_SESSION['user_name'] = $result_row->user_name;

私のPHPログインスクリプト(これは、githubで最も星が付けられ、ダウンロードされ、フォークされたスクリプトであり、非常にうるさい人によってチェックされています)。スクリプトの詳細については、こちらの githubを参照してください。

于 2013-06-11T20:07:17.397 に答える
0

動作しますが、次の 2 つの点に注意する必要があります。

  1. 同じ名前のユーザーが 2 人以上いないことを確認する必要があります。
  2. ログインしているユーザー (セッションが有効なユーザー名) が削除され、同じ名前の別のユーザーが作成された場合はどうなりますか?

そのような場合、まだセッション中のユーザーがログインしているのに気付くかもしれません...しかし、別のユーザーとして!

代わりに、固有のユーザー ID をセッションに入れてみてください。

于 2013-06-11T20:38:09.670 に答える