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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:35:42+00:00 2026-05-11T07:35:42+00:00

I’m trying to implement some kind of caching in a PHP script that will

  • 0

I’m trying to implement some kind of caching in a PHP script that will go out to many different clients/sites, which will be deployed by fairly non-technical users, using a variety of web hosts. Due to the non-technical nature of the users, I’d like to avoid asking them to tweak file permissions. The caching needs to be across sessions, so using session variables is out. If I was coding this in ASP I’d use application variables, but they don’t exist in PHP (to my knowledge)

Does anyone have any suggestions on how to accomplish this?

Here are some possibilities I’ve considered, any comments on these would be useful:

  • Caching via files in system temp folders – I could use sys_get_temp_dir() (or some home rolled similar function on PHP4) to help find such a folder. The disadvantage here, is it probably wouldn’t work on hosts using the openbase_dir restriction
  • Some website I looked at mentioned tricking PHP into making all user sessions share the same session state thereby forcing session variables to act like session variables. Of course I can’t find that post now… This seems scary anyway. (I’ll update this question with the link to this post once I find it again)
  • Use a third party data store like Amazon’s Simple Storage Service – seems like overkill
  • Cache the data on a server I control, and have the client download new data from there on each hit.

Once again, any comments on these ideas or any new ones would be appreciated.

UPDATE: I tried using session_id() to use a shared session state, but it doesn’t work. Each session is maintaining its own ‘GlobalCache’, any ideas why?:

// This code doesn't work, but the similar code in my answer does!  function test() {     if (!$_SESSION['session_count']) $_SESSION['session_count'] = 0;     $_SESSION['session_count']++;       $count = get_cache( 'count' );      print 'pre application count: $count<br>';      if ( !$count )         $count = 0;      $count++;      print 'session_id: ' . session_id() . '<br>';     print 'post application count: $count<br>';     print 'session_count: ' . $_SESSION['session_count'] . '<br>';      set_cache('count', $count); }   function set_cache( $name, $value ) {     $old_session = session_id();     print 'old_session (set): $old_session<br>';      session_id('GlobalCache');     print 'new_session (set): ' . session_id() .  '<br>';      $_SESSION[$name] = $value;     session_id( $old_session ); }  function get_cache( $name ) {     $old_session = session_id();     print 'old_session (get): $old_session<br>';      session_id('GlobalCache');     print 'new_session (get): ' . session_id() .  '<br>';      $value = $_SESSION[$name];     session_id( $old_session );     return $value; }  session_start(); test(); 

UPDATE: some have suggested using memcached, which is in fact a great solution for some, but since I don’t have control over the end server environment it isn’t an option. The idea is to have a script that people can just FTP up to a shared hosting account that just works out of the box.

UPDATE: someone suggested creating my own cache folder with the script, but wouldn’t I need to create such a folder inside a folder that already had write permissions?

UPDATE, SOLUTION FOUND: I ended up figuring out the problems in my global session script and have posted my own answer to that effect. Thanks for the help everyone.

  • 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. 2026-05-11T07:35:43+00:00Added an answer on May 11, 2026 at 7:35 am

    Okay, I figured out how to do this by emulating ASP-style application variables using a shared/global session state. Two key changes from my non-working code in the relevant update:

    1. To switch session state, we must end the current session, switch to the new one, then start it. I’ve encapsulated this process in switch_session()

    2. Since we are switching session ids around we have to buffer the page’s output using ob_start()/ob_end_flush() so that the session cookie isn’t sent too soon.

    The full working code follows (cleaned up too!). This can be easily tested by loading the page in an IE and a Firefox window, and reloading each several times to watch the counters rise:

    <?php function test() {      // Do a regular session count     print 'session_id: ' . session_id() . '<br>';     if (!$_SESSION['session_count']) $_SESSION['session_count'] = 0;     $_SESSION['session_count']++;     print 'session count: ' . $_SESSION['session_count'] . '<br>';       // Do an application count     $count = get_cache( 'count' );     if ( !$count ) $count = 0;     $count++;     print 'application count: $count<br>';     set_cache('count', $count); }   function set_cache( $name, $value ) {     $old_session = switch_session('GlobalCache');     $_SESSION[$name] = $value;     switch_session( $old_session ); }  function get_cache( $name ) {     $old_session = switch_session('GlobalCache');     $value = $_SESSION[$name];     switch_session( $old_session );     return $value; }  function switch_session( $session_id ) {      // switch the session and return the original     $old_id = session_id();      session_write_close();     session_id($session_id);     session_start();     return $old_id; }  ob_start(); session_start(); test(); ob_end_flush(); ?> 

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

Sidebar

Ask A Question

Stats

  • Questions 118k
  • Answers 118k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer A session cookie is just a normal cookie without an… May 11, 2026 at 11:31 pm
  • Editorial Team
    Editorial Team added an answer Nothing. Since the property type is Type, the XAML parser… May 11, 2026 at 11:31 pm
  • Editorial Team
    Editorial Team added an answer I was thinking if you can create two sockets. One… May 11, 2026 at 11:31 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.