douyao6842 2013-12-18 08:55
浏览 42
已采纳

每个特定的browsertab使用会话cookie

I currently have a form that allows a user to edit entries. However, it doesn't seem to be possible to use 1 (specific) cookie per tab. Whenever a user edits an entry, the record in the last tab gets updated.

I've tried the following in my main script (eventfilters.php):

<?php
$cookie_name = $_SESSION['username'].md5(time());
session_name($cookie_name);
setcookie(session_name($cookie_name),session_id(),time()+"300");
if(!isset($_SESSION)){ session_start(); }

if (isset($_GET['edit'])){  
    // Pass cookiename in url variable $cookie, so it gets caught by $_GET['cookie']
    echo '<form action="eventfilters.php?save&cookie='.session_name().'" method="post">';

} else if (isset($_GET['save'])){
    if(isset($_GET['cookie'])){
        error_log("SAVE ".$_GET['cookie']); // Displays cookie url variable set by form action.
        error_log("LOW ".$_SESSION['level_low']); 
        // Displays correct session value received from ajax
    }
}

?>

`

The ajaxcode (also in eventfilters.php) contains this (called few times when page is already loaded):

$.post("include/severitygroups.php",{'cookie_name': "<?php echo $cookie_name; ?>", 'serialized_sev_groups': serialized_sev_groups}, function(data){});

This seems to pass the right cookiename to the other script, which successfully seems to return $_SESSION['level_low'] (as it appears in the error_log).

<?php
include('pdodb.php');

if(!isset($_SESSION)){ session_start(); }
$cookie_name = $_POST['cookie_name'];
error_log("SCRIPT ".$cookie_name);
error_log("COOKIEDATA ".$_COOKIE["$cookie_name"]);

// populating $_SESSION['level_low']
?>

It seems that the $_GET['save'] is populating the wrong sessions (initialised by the last loaded instance of eventfilters.php), even when the $_GET['save'] logs the right $_SESSION['level_low'] to the errorlog.

What is going wrong?

  • 写回答

1条回答 默认 最新

  • drjmrg8766 2013-12-18 09:01
    关注

    You can't do it. The best way to do it, is with Ajax, so, you will pass an "ID" to each tab (or page) like:

    editPost.php?id=someID
    

    And, each time you press save, you should send your content with your id param to save it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。