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 6005575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:25:00+00:00 2026-05-23T01:25:00+00:00

I have a simple example of PHP sessions and AJAX, which works when holding

  • 0

I have a simple example of PHP sessions and AJAX, which works when holding an array in session:

Request file:

<?php
    session_start();
    $_SESSION['data'] = array('foo','bar');
    echo count($_SESSION['data']);
?>
<html>
<head>
    <title>Test</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
</head>
<body>
    <button id="but1">Go</button>
    <script type="text/javascript">
        $('#but1').click(function() {
            $.ajax({
                url:'ajaxtest_remote.php',
                success:function(result) {
                    alert(result);
                }
            });
        });
    </script>
</body>
</html>

Remote file:

<?php
    session_start();
    echo 'count=' . count($_SESSION['data']);
?>

The echo on the first file shows 2, and the alert in the success function displays “count=2”. Happy days.

Where the problem happens is if I swap my array for a class object:

Request File:

<?php
    session_start();
    include('ajaxtest_class.php');
    $_SESSION['obj'] = new TestClass('foo,bar');
    echo count($_SESSION['obj']->dataList);
?>
<!-- HMTL AS ABOVE -->

Remote File:

<?php
    session_start();
    echo 'count=' . count($_SESSION['obj']->dataList);
?>

Class File:

<?php
    class TestClass {
        var $dataList;
        function TestClass($incoming) {
            $this->dataList = explode(',',$incoming);
        }
    }
?>

This still displays a 2 on the first page, but the ajax success alert comes back “count=0”. Can anyone explain why this is?

Update1

If I import the class file into remote it still doesn’t work, although I can prove the class is loaded.

<?php
    session_start();
    include('ajaxtest_class.php');
    $c = new TestClass('a,b,c');
    echo 'count=' . count($_SESSION['obj']->dataList) . '-' . count($c->dataList);
?>

The new alert from the ajax success reads count=0-3.

Update2

var_dump($_SESSION[‘obj’]);

object(__PHP_Incomplete_Class)#8 (2) {
    ["__PHP_Incomplete_Class_Name"]=>
    string(9) "TestClass"
    ["dataList"]=>
    array(2) {
        [0]=>
        string(3) "foo"
        [1]=>
        string(3) "bar"
    }
}
  • 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-05-23T01:25:00+00:00Added an answer on May 23, 2026 at 1:25 am

    You would need to include the Class in the remote_ajax file (before session_start()):

    edit: The serialize/unserialize requirement is a limitation of PHP4.

    Request file:

    <?php
    include('ajaxtest_class.php');
    session_start();
    $_SESSION['obj'] = serialize(new TestClass('foo,bar'));
    

    Remote file:

    <?php
        session_start();
        include('ajaxtest_class.php');
        $obj = unserialize($_SESSION['obj']);
        echo 'count=' . count($obj->dataList);
    ?>
    

    In PHP, the class constructor should be defined differently:

    <?php
        class TestClass {
            var $dataList;
            function __construct($incoming) {
                $this->dataList = explode(',',$incoming);
            }
        }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i would like a simple help... i have a url like this: example.com/profile.php?id= &
I have seen simple example Ajax source codes in many online tutorials. What I
I have site for example www.mysite.com, with a simple login system. I use PHP
I have a simple .htaccess file RewriteEngine on RewriteRule ^[^-]*-(.*)$ http://example.com/$1 [R] RewriteRule ^[(template)(internal)(stuff)(files)]
I have simple script which contains two pages - script.php and function.php. Script.php contains
I have found many sites that describes PRG, but no simple PHP code example.
I have this simple example I can't seems to get working : MERGE INTO
Does anyone have a simple example of implementing an async validation rule in csla?
I have this very simple example that I am using to learn structs in
Below I have a very simple example of what I'm trying to do. I

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.