您好我有一个登录系统,登录的功能是:
if($user_data > 0) {
session_start();
echo "SESSION STARTED OK";
$_SESSION['touch_session_admin'] = true;
$_SESSION['touch_session_fingerprint'] = md5($_SERVER['HTTP_USER_AGENT'] . PHRASE . $_SERVER['REMOTE_ADDR']);
echo $_SESSION['touch_session_fingerprint'];
header('Location: index.php');
exit;
} else {
header('Location: index.php?error=Wrong Username/Password!!!');
}
我正在存储一些会话变量touch_session_admin和一个安全的touch_session_fingerprint我在解决这个会话时遇到了一些问题吗?
用于检查的代码是一个简单的CLASS
class SessionClass {
// __CONSTRUCTOR AS SESSION CHECK
public function SessionClass() {
session_start();
// is the one accessing this page logged in or not?
if (!isset($_SESSION['touch_session_admin'])
|| $_SESSION['touch_session_admin'] !== true) {
session_destroy();
header('Location: login.php');
exit;
}
if (
$_SESSION['touch_session_fingerprint'] != md5($_SERVER['HTTP_USER_AGENT'] . PHRASE . $_SERVER['REMOTE_ADDR'])
)
{ // Checking finger Print
session_destroy();
header('Location: login.php');
exit();
}
}
}
即使我们让它工作也是安全的代码吗?
答案 0 :(得分:0)
在此行中,您使用常量PHRASE
,但我没有看到PHRASE
在任何地方定义。
$_SESSION['touch_session_fingerprint'] != md5($_SERVER['HTTP_USER_AGENT'] . PHRASE . $_SERVER['REMOTE_ADDR'])
你有error reporting被压制吗?如果启用它,您可能会看到实际错误。否则,请检查日志中的消息。
答案 1 :(得分:0)
首先,这里有一些关于php的一般建议:
header()
或session_start()
__construct()
,而非Classname()
,与其他语言类似。你想用指纹实现什么目标?会话存储在服务器端,因此您无需哈希(至少,我认为没有任何好处),因为您的用户不能直接访问它。
我建议你用简单的东西替换你的代码,没有指纹,比如:
if($user_data > 0) {
session_start();
$_SESSION['touch_session_admin'] = true;
$_SESSION['last_user_agent'] = $_SERVER['HTTP_USER_AGENT'];
$_SESSION['last_ip'] = $_SERVER['REMOTE_ADDR'];
header('Location: index.php');
exit();
}
和
class SessionClass {
public static function checkAuth() {
session_start();
if (!isset($_SESSION['touch_session_admin'])) {
session_destroy();
$_SESSION = array();
header('Location: login.php');
exit();
}
if ($_SESSION['last_user_agent'] != $_SERVER['HTTP_USER_AGENT']) {
session_destroy();
die("User agent has changed !");
}
if ($_SESSION['last_ip'] != $_SERVER['REMOTE_ADDR']) {
session_destroy();
die("Ip has changed !");
}
}
}