简体   繁体   English

防止PHP中的会话劫持

[英]Prevent session hijacking in PHP

I wrote the log In/log Out scripts of my web page and when the user logs in I store in the $_SESSION variable the user agent. 我写了网页的登录/注销脚本,当用户登录时,我将$_SESSION变量存储在用户代理中。 Now each time a page is loaded I check if the user is logged in or not and if it is logged in I check if the user agent has changed or is still the same to prevent hijacking. 现在,每次加载页面时,我都会检查用户是否登录,是否已登录,我会检查用户代理是否已更改或仍然相同以防止劫持。 In case it has changed I call my logOut() function: 万一它改变了,我调用我的logOut()函数:

function logOut($conn) 
{
    $sql = "UPDATE Users SET logged='no' WHERE username='" . $_SESSION['username'] ."'";
    $conn->query($sql); 

    // Unset all session values 
    $_SESSION = array();

    // get session parameters 
    $params = session_get_cookie_params();

    // Delete the actual cookie. 
    setcookie(session_name(), '', time() - 42000, 
    $params["path"], 
    $params["domain"], 
    $params["secure"], 
    $params["httponly"]);

    // Destroy session 
  session_destroy();    
}

Now the problem is that the right user can still navigate my website as logged in while I would like to have it logged out. 现在的问题是,正确的用户仍可以按登录状态浏览我的网站,而我希望将其注销。 What could be the problem? 可能是什么问题呢?

To test this code I logged in using Mozilla Firefox and I copied the value of the cookie in Chrome and reloaded the home page and as expected I am unable to log in with Chrome but if I reload the page in Mozilla it is still logged in. 为了测试此代码,我使用Mozilla Firefox登录,并在Chrome中复制了cookie的值并重新加载了主页,但按预期,我无法使用Chrome登录,但是如果我在Mozilla中重新加载该页面,则仍可以登录。

It's better use unset() function ,for instead unset($_SESSION['username']) 最好使用unset()函数,而不要使用unset($ _ SESSION ['username'])

Also I can say you ,your codes have sql injection vulnerability in here 我也可以说你,你的代码在这里有sql注入漏洞

' $sql = "UPDATE Users SET logged='no' WHERE username='" . '$ sql =“ UPDATE用户SET已登录='否'WHERE username ='”。 $_SESSION['username'] ."'"; $ _SESSION ['username']。“'”; ' '

because everyone can with use curl , modify the variable username to ' or '1'='1 and inject this to 因为每个人都可以使用curl,将变量username修改为'或'1'='1并将其注入

your webpage and get access ,so you can modify this line to 您的网页并获得访问权限,因此您可以将该行修改为

$sql = "UPDATE Users SET logged='no' WHERE username='" . $ sql =“ UPDATE用户SET已登录='否'WHERE username ='”。 stripslashes($_SESSION['username']) ."'"; stripslashes($ _ SESSION ['username'])。“'”; ' '

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM