Session in AJAX php file is not updating

28
October 20, 2018, at 00:50 AM

I am facing a problem with updating the value of my SESSION named "subs". When I click to .userLink, a profile of specific user should show. That works - all profile informations are changing according to link I am clicking on. Even my session get the right value. But just once. When I click to another user link, profile information is changed (first name, last name, ...) but the value of my session stay same till I delete cookies. Then I am able to get the right value once again.

I am totally frustrated. I cannot find a mistake. AJAX is working correctly, all files have session_start() on top.

Can somebody help me, please?

JAVASCRIPT (AJAX CALL)

$(".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]);
          }
        });
      });

showProfile.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();
?>
READ ALSO
Check if variable exist with dynamic variable name and dynamic key name and deepness

Check if variable exist with dynamic variable name and dynamic key name and deepness

what is the correct syntax to use isset with a dynamic variable name and dynamic key name, without writing the keyname in braces

30
What is the difference between these 2 ways of exporting from a Javascript module?

What is the difference between these 2 ways of exporting from a Javascript module?

I'm new to JS modules and am confused by the following:

27
Converting array into a JS Object

Converting array into a JS Object

I have key value pairs in an array in the format -

56