weixin_33734785 2012-12-01 17:04 采纳率: 0%
浏览 19

在onclick php之后更新表

i build system notifications and have me problem.

i want update table 'notifications' after onclick from 'new=1' to new='0' for a user logged.

how i update at database after onclick with ajax or something other. if i send request server with ajax as this:

        function readNotifications(str)
    {
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","controlpanel.php?notification=readNews",true);
    xmlhttp.send();
    }

The server does not know which USER ID to update the alerts. And if I send USER ID at time be possible to change the USER ID and everyone can update the notifications of each other and this a not security.

I'm sorry for my English, if I do not understand you can ask a question. Thank you!

  • 写回答

1条回答 默认 最新

  • 笑故挽风 2012-12-01 17:16
    关注

    I think its better for you to save the user_id in a session variable once the user logs in.

    <?php
    session_start();
    // store session data
    $_SESSION['user_id']=100001;
    ?>
    

    and access it from your code behind during the processing of your ajax request.

    $_SESSION['user_id']
    

    AND
    if you use cookies, you can still accept that from your server side code.

    $_COOKIE["user_id"]; 
    will do it.
    

    just make sure that, you set the withCredentials property to true.

    var req= new XMLHttpRequest();
    req.withCredentials = true;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法