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 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器