On index.php, I am running the following AJAX to get updated notifications:
function updateNotifications() {
var user_id = "<?php echo $user_id; ?>";
var phpSessionId = "<?php echo session_id(); ?>";
var number_of_previous_notifications = "<?php echo $_SESSION['number_of_notifications']; ?>";
alert(number_of_previous_notifications);
$.ajax({
url : "notification_refresh.php",
type : "POST",
data : { "user_id": user_id, "session_id": phpSessionId },
success :
function(data) {
var response = $.parseJSON(data);
if (response.messages > 0) {
$('.inactive-notification-img').hide();
$('.notification-img').show()
$('.notification-number').html(response.messages).addClass('blink_me');
}
else {
$('.notification-img').hide();
$('.inactive-notification-img').show();
$('.notification-number').html('').removeClass('blink_me');
}
}
});
setTimeout('updateNotifications()', 15000); // Every 15 seconds.
}
updateNotifications();
On the page that AJAX calls, here is the code...
if (isset($_POST['session_id'])) { session_id($_POST['session_id']); }
session_start();
include_once('../../inc/database.php');
include_once('../../inc/session.php');
if (
(isset($_POST['user_id'])) && (is_numeric($_POST['user_id']))
){
$notif_user_id = $_POST['user_id'];
$search_notif_query = $db1q->query("SELECT id FROM User_Notifications WHERE FIND_IN_SET('$notif_user_id', to_user) AND status = '0'");
$response = ['messages' => $search_notif_query->num_rows];
$_SESSION['number_of_notifications'] = $search_notif_query->num_rows;
echo json_encode( $response );
}
As you can tell, in the function on index.php, I am alerting the number_of_previous_notifications to see if it ever changes. And I let the function run 4-5 times and the number_of_previous_notifications is NEVER updated.
Which is telling me that the session[‘number_of_notifications’] is not being updated on the Ajax request page.
Any thoughts?