如何在PHP中识别会话中的用户

前端之家收集整理的这篇文章主要介绍了如何在PHP中识别会话中的用户前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

登录一个用户,在我的PHP脚本中,我创建一个会话变量并为其分配用户名(因为它是唯一的).

@H_301_5@PHP session_start(); //$username= getting username from database and all after loggin $_SESSION['user_of_mywebsie']=$username; // using $username now

现在这是正确而安全的方式吗?如果没有,可以做些什么?
现在使用用户名创建会话..

@H_301_5@$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
最佳答案
不,不正确.您的代码容易受到SQL injection的攻击.如果我的用户名Robert'); DROP TABLE Students;--,该怎么办?

您应该至少逃避数据,甚至更好地使用预准备语句和绑定参数.

How can I prevent SQL injection in PHP?

您还使用了已弃用的数据库API. Either use mysqli_* or PDO.

猜你在找的MySQL相关文章