dongshilve4392 2015-06-09 05:11
浏览 71
已采纳

会话在页面刷新时被破坏

i have php file which works fine on the first load but session is destroyed after refresh page. On refresh php page i get Warning: session_regenerate_id(): Session object destruction failed

<?php
    /** functions.php **/
    function startSession() {
        $session_name = 'sec_session_id';   // Set a custom session name
        session_name($session_name);
        session_start();                    // Start the PHP session 
        session_regenerate_id(true);        // regenerated the session
    }
?>

<!-- controller.js -->
function LoadFriend() {
        var listFriend = $('#list_member_friend');
        var UrlToPass = 'action=load';
        listFriend.html('loading..');
        $.ajax({
            url : 'ajax.php',
            type : 'POST',
            data : UrlToPass,
            success: function(responseText) {
                listFriend.html(responseText);
            }
        });
    }
loadFriend();
<!-- end controller.js -->

<?php
    /** ajax.php **/
    $action = $_GET['action']
    switch($action) {
       case "load": 
           /** PRINT LIST FRIEND **/
       ...
    }
?>

<?php
    /** index.php **/
    include('functions.php');
    startSession();
    $userid = 'HENRY';
    if(!isset($_SESSION['user_id'])) {
        $_SESSION['user_id'] = $userid;
    } 
?>
<html>
   <head>
       <title>Load List</title>
       <script src="controller.js" type="text/javascript"/>
   </head>
   <body>
       <div id="list_container">
           <div id="list_member_friend" class="list_member_friend">
           <!-- this is where it generates session error -->
           <!-- load list of members friend with ajax script controller.js from member.php -->
           </div>
       </div>
   </body>
</html>

in member.php i also start session

<?php 
    /** member.php **/
    include('functions.php');
    startSession()
    $db = new MySQLi('localhost','root','abcdefg','social');
    $query = "SELECT fid, fname, fage, fgender FROM memberFriends";
    $query .= " WHERE memberid = '" . $_SESSION['user_id'] . "'"; <!-- this is the problem -->
    $db->prepareQuery($query);
    $db->execute();
    ..... (Load List);
?>

Questions:

  1. Why session value is not passed in member.php after refresh page?

  2. What are the best approach to pass the Session value into member.php?

  • 写回答

2条回答 默认 最新

  • douqin3245 2015-06-09 05:13
    关注

    There is a syntactical error. Should be -

    if(!isset($_SESSION['userid'])) {
    

    isset is a function. It should be called by isset() not isset[].

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

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大