首页 > 解决方案 > AJAX php 文件中的会话未更新

问题描述

我在更新名为“subs”的 SESSION 的值时遇到问题。当我单击 .userLink 时,应显示特定用户的个人资料。这行得通-所有个人资料信息都根据我单击的链接而变化。甚至我的会话也得到了正确的值。但只有一次。当我单击另一个用户链接时,个人资料信息会更改(名字、姓氏……),但我的会话值保持不变,直到我删除 cookie。然后我能够再次获得正确的价值。

我非常沮丧。我找不到错误。AJAX 工作正常,所有文件顶部都有 session_start()。

有人可以帮我吗?

JAVASCRIPT(AJAX 调用)

$(".userLink").click(function() {
        var user = $(this).html();
        var dataString = 'uzivatel='+ user;
        $.ajax({
          type: "POST",
          url: "includes/showProfile.php",
          data: dataString,
          dataType: 'json',
          cache: false,
          success: function(result){
            $("#otherProfile").toggle();

            $("#nameSection h2").html(result[1]);
            $("#nameSection h1").html(result[2]);
            $("#Pbirthdate").html(result[3]);
            $("#Pgender").html(result[4]);
            $("#description").html(result[5]);
          }
        });

      });

显示配置文件.php

<?php
session_start();

include 'connect.php';

$profileOwner = $_POST['uzivatel'];

$sql = "SELECT * FROM users WHERE username = '$profileOwner'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {

  while($row = $result->fetch_assoc()) {
    $_SESSION["subs"] = $row["userID"];
    $firstname = $row["firstname"];
    $lastname = $row["surname"];
    $birth = $row["birthdate"];
    $birth = date( "j.n.Y", strtotime($birth));
    if($row["gender"] == "male"){
      $gender = "MUŽ";
    } else{
      $gender = "ŽENA";
    }
  }
}

$file="../profiles/".$ID."/description.txt";
$imageFile="profiles/".$ID."/profileImage.jpg";

if(file_exists($file)){
   $description = file_get_contents($file);
}

$array = array($ID,$firstname,$lastname,$birth,$gender,$description);
echo json_encode($array);


$conn->close();
?>

标签: phpajaxsessionupdating

解决方案


推荐阅读