Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8648517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:19:53+00:00 2026-06-12T13:19:53+00:00

I am trying to set a session variable’s value from within a class function,

  • 0

I am trying to set a session variable’s value from within a class function, and comparing that session value at some later time inside a different function of the same class. None of these functions are in the __construct() function.

The problem seems to be that the comparison never returns true.

<?php
session_start();
$class = new Class();
?>

blah blah blah

<script type="text/javascript">
    // Ajax function to call PHP that creates new Class isntance and executes a Class->function();
    setInterval('checkNew()', 10000);
</script>

blah blah blah

// Another Ajax Function that calls creates a new Class isntance and executes the initial Class->function();
<body onload="getMessages();">

blah blah blah

This next function excerpt is ran from the script that the <body onload=""> ajax calls.
This is to compare this $_SESSION['last_sms'] later in a different function.

// Check the now() stamp of the most recent message loaded
$recent_msg_query = "SELECT date_received FROM messages ORDER BY date_received DESC LIMIT 1";
$recent_statement = $this->db_handle->prepare($recent_msg_query);
$recent_statement->execute();
$most_recent = $recent_statement->fetch(PDO::FETCH_NUM);
$_SESSION['last_sms'] = $most_recent[0];

This is the function inside the Class that is supposed to be doing the comparison, every ten seconds (via setInterval('checkNew()', 10000);)

public function checkNew()
{
    // Check the now() stamp of the most recent message loaded and compares it
    // to a stored now() stamp.
    $recent_msg_query = "SELECT date_received FROM messages ORDER BY date_received DESC LIMIT 1";
    $recent_statement = $this->db_handle->prepare($recent_msg_query);
    $recent_statement->execute();
    $most_recent = $recent_statement->fetch(PDO::FETCH_NUM);

    if (!isset($_SESSION['last_sms'])) {
        $_SESSION['last_sms'] = $most_recent[0];
    }

    if ($_SESSION['last_sms'] !== $most_recent[0]) {
        echo "New message(s) available, refresh.";
    } else {
        echo "No new messages yet.";
    }
}

When I send a new message, the page still says “No new messages yet.” Can you guys help me figure out where I’ve gone off the path here? This $_SESSION stuff is the only thing not working so far.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:19:55+00:00Added an answer on June 12, 2026 at 1:19 pm

    session_start() does not merely function as a session initiator. It must be called on every script which attempts to read or write to $_SESSION. Since your AJAX handler scripts are detached from the main PHP script that produced the page they are called from, and they function as entirely independent PHP scripts, you must also call session_start() on the AJAX handling PHP script.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to set cookies to a php session variable so that a
Hello i´m trying to set info from a post method into a variable that
I am trying to set a session variable and retrieve it on a future
I am trying to set session attribute in a jsp function. The function reads
I'm trying to get a session variable to be set when clicking on a
I am trying to get an array to pass in a session variable from
I'm trying to pass the value of a dropdownlist to a session variable, and
I have this session variable which i am trying to set to an access
I have a class that sets various session variables. After I set the session
Trying to set up Cancan within an app of mine and having trouble with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.