dongyongju9560 2009-06-22 15:07
浏览 37
已采纳

防止PHP中的会话冲突

Recently I stumbled on an interesting bug where entries would show up in our local custom-made ticket system from users that didn't exist in the app. After some poking around I realised that both this and another PHP app running on the same server were using $_SESSION['user'] for authentication purposes. When someone used one system and then opened the other one he was "automatically" logged in as a user from the first app.

After the obligatory facepalm I changed the session variable name in one of the systems to stop this. However I need to make a permanent, zero-maintenance change to my session handling class to avoid this in future projects. I figure this could be done by using a unique value (for example the script path) to put an app's variables in a place in $_SESSION that wont be used by another app.

Is there a better way?

Edit: This is on linux. And both apps are on the same website.

  • 写回答

3条回答 默认 最新

  • duankanyi6539 2009-06-22 15:11
    关注

    You can ensure that the Session cookies will be specific to the application by making sure that the domain and path of the cookies are set restrictively, e.g. for an application at http://www.example.com/apppath, you could do the following:

    <?php
    
    $currentParams = session_get_cookie_params();
    
    session_set_cookie_params($currentParams['lifetime'], '/apppath/', 'www.example.com', $currentParams['secure'], $currentParams['httponly']);
    
    session_start();
    

    This will leave the other session settings intact.

    The path is probably the important one, by default PHP will issue the cookie for the domain that the site was requested over (I think), so you could probably actually leave the domain parameter as the default.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度