dtdfj08626 2014-03-24 15:29 采纳率: 0%
浏览 30

php中的会话安全性以及需要注意的事项

This has been asked a bunch of times. I am just curious as to if it is believe that the below code provides a fairly signicant level of security. are there other if's i should think about?

a few assumptions:

  1. if user does nothing in 5 seconds, log him out.
  2. if user is not coming from somePage.php log him out.
  3. if users ip address changes, log him out.

code below:

<?php
session_start();
$time = time();
$ip = $_SERVER['REMOTE_ADDR'];

if ($time - $_SESSION['time'] > 5)
{
    //function to log out user...//echo "logged Out,Time";
}
elseif ($ip !== $_SESSION['ip'])
{
    //function to log out user...//echo "logged out,IP";
}
elseif ($_SERVER['HTTP_REFERER'] !== "http://server.com/somePage.php")
{
    //function to log out user...//echo "logged out,Refer";
}
else
{
   //do sensitive stuff
}
$_SESSION['time'] = $time;
$_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];
?>

EDIT:

This is just some basic stuff in the "sensitive" area. on maybe a facebook level. I dont want anyone to get into who shouldn't be. but if they did, there would be some problems, but nuclear missles will not be launched.

5 seconds was my example for testing, clearly it would be longer.

Bryan,

if $_SERVER can be spoofed, are there better methods to ascertain the user's source ip?

  • 写回答

1条回答 默认 最新

  • douhanzhen8927 2018-10-01 20:39
    关注

    a few assumptions:

    1. if user does nothing in 5 seconds, log him out.
    2. if user is not coming from somePage.php log him out.
    3. if users ip address changes, log him out.

    You will drive your users (whom are probably not exclusively male) insane. Especially Tor users.

    The number of things you have to do to implement secure PHP sessions is small:

    1. Use a CSPRNG to generate session IDs.
    2. Use HTTPS.
    3. Use HttpOnly cookies (so they're invisible to JavaScript).
    4. Use Secure cookies (so they're only available in HTTPS).
    5. Use session_regenerate_id(true); whenever a user logs in or out before assigning new session variable values.
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集