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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:05:26+00:00 2026-05-25T15:05:26+00:00

I have a simple problem. I use php as server part and have an

  • 0

I have a simple problem. I use php as server part and have an html output. My site shows a status about an other server. So the flow is:

  1. Browser user goes on http://www.example.com/status
  2. Browser contacts http://www.example.com/status
  3. PHP Server receives request and ask for stauts on http://www.statusserver.com/status
  4. PHP Receives the data, transforms it in readable HTML output and send it back to the client
  5. Browser user can see the status.

Now, I’ve created a singleton class in php which accesses the statusserver only 8 seconds. So it updates the status all 8 seconds. If a user requests for update inbetween, the server returns the locally (on http://www.example.com) stored status.

That’s nice isn’t it? But then I did an easy test and started 5 browser windows to see if it works. Here it comes, the php server created a singleton class for each request. So now 5 Clients requesting all 8 seconds the status on the statusserver. this means I have every 8 second 5 calls to the status server instead of one!

Isn’t there a possibility to provide only one instance to all users within an apache server? That would be solve the problem in case 1000 users are connecting to http://www.example.com/status….

thx for any hints

=============================
EDIT:

I already use a caching on harddrive:

public function getFile($filename)
{
    $diff = (time()-filemtime($filename));
    //echo "diff:$diff<br/>";
    if($diff>8){
        //echo 'grösser 8<br/>';
        self::updateFile($filename);
    }
    if (is_readable($filename)) {
        try {
            $returnValue = @ImageCreateFromPNG($filename);
            if($returnValue == ''){
                sleep(1);
                return self::getFile($filename);
            }else{
                return $returnValue;    
            }
        } catch (Exception $e){
            sleep(1);
            return self::getFile($filename);
        }
    } else {
        sleep(1);
        return self::getFile($filename);
    }
}

this is the call in the singleton. I call for a file and save it on harddrive. but all the request call it at same time and start requesting the status server.

I think the only solution would be a standalone application which does an update every 8 seconds on the file… All request should just read the file and nomore able to update it.
This standalone could be a perl script or something similar…

  • 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-25T15:05:27+00:00Added an answer on May 25, 2026 at 3:05 pm

    Php requests are handled by different processes and each of them have a different state, there isn’t any resident process like in other web development framework. You should handle that behavior directly in your class using for instance some caching.

    The method which query the server status should have this logic

    public function getStatus() {
      if (!$status = $cache->load()) {
        // cache miss
        $status = // do your query here
        $cache->save($status); // store the result in cache
      }
      return $status;
    }
    

    In this way only one request of X will fetch the real status. The X value depends on your cache configuration.

    Some cache library you can use:

    • APC
    • Memcached
    • Zend_Cache which is just a wrapper for actual caching engines

    Or you can store the result in plain text file and on every request check for the m_time of the file itself and rewrite it if more than xx seconds are passed.

    Update

    Your code is pretty strange, why all those sleep calls? Why a try/catch block when ImageCreateFromPNG does not throw?

    You’re asking a different question, since php is not an application server and cannot store state across processes your approach is correct. I suggest you to use APC (uses shared memory so it would be at least 10x faster than reading a file) to share status across different processes. With this approach your code could become

    public function getFile($filename)
    {
        $latest_update = apc_fetch('latest_update');
        if (false == $latest_update) {
          // cache expired or first request
          apc_store('latest_update', time(), 8); // 8 is the ttl in seconds
          // fetch file here and save on local storage
          self::updateFile($filename);
        }
        // here you can process the file
        return $your_processed_file;
    }
    

    With this approach the code in the if part will be executed from two different processes only if a process is blocked just after the if line, which should not happen because is almost an atomic operation.

    Furthermore if you want to ensure that you should use something like semaphores to handle that, but it would be an oversized solution for this kind of requirement.

    Finally imho 8 seconds is a small interval, I’d use something bigger, at least 30 seconds, but this depends from your requirements.

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

Sidebar

Related Questions

I have a simple problem when querying the SQL Server 2005 database. I have
I have a simple php script on a server that's using fsockopen to connect
I have the weirdest problem. I am implementing a simple gallery with a use
i have very simple problem. I need to create model, that represent element of
I have a simple problem that i cannot solve. I have a dictionary: aa
I have a simple problem but I am not sure how to solve it.
I have a simple problem but no matter what I try I can't see
I have a very simple problem which requires a very quick and simple solution
I have a very simple problem and a solution that will work, but I'm
I have a seemingly simple problem though i am unable to get my head

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.