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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:17:35+00:00 2026-06-16T19:17:35+00:00

I have a web form that has an included file that outputs select options

  • 0

I have a web form that has an included file that outputs select options for states. The html looks like

    <select name="state" id="state">
        <option value="">--</option>
        <?php include ("resources/data/stateoptions.php"); ?>
      </select>

The state options makes a call to a web service so that the list of store locations is always current. It seems however that this contact form page runs exceptionally slow (much faster if I remove this include). So I want to cache the web service call. My state options file looks like this

<?php
  $cachefile = "cache/states.html";
  $cachetime = 5 * 60; // 5 minutes

  // Serve from the cache if it is younger than $cachetime
  if (file_exists($cachefile) && (time() - $cachetime < filemtime($cachefile))) 
  {
     include($cachefile);

     echo "<!-- Cached ".date('jS F Y H:i', filemtime($cachefile))." 
     -->n";

     exit;
  }

  ob_start(); // start the output buffer
?>

<?php
//url of locations web service
$serviceURL = 'http://webserviceurl/state';

//query the webservice
$string = file_get_contents($serviceURL);

//decode the json response into an array
$json_a=json_decode($string,true);

foreach( $json_a as $State => $IdealState){
$IdealState = $IdealState[State];
$IdealState2 = str_replace(' ', '-', $IdealState);
echo '<option value='.$IdealState2.'>'.$IdealState.'</option>';
}
?>

<?php
// open/create cache file and write data
$fp = fopen($cachefile, 'w'); 
// save the contents of output buffer to the file
fwrite($fp, ob_get_contents()); 
// close the file
fclose($fp); 
// Send the output to the browser
ob_end_flush(); 
?>

When I call this file directly, everything works as expected, and a states.html file is created. For some reason however when the stateoptions.php file is included in my contact form page, it never creates a cache file, and the speed problem persists. I’m a fairly novice programmer, so any help would be much appreciated.

Thanks!

  • 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-16T19:17:37+00:00Added an answer on June 16, 2026 at 7:17 pm

    The problem here is most likely going to be relative paths and working directories. The included file inherits its working directory from the calling script, it does not get a working directory of the location in which it resides automatically.

    You either need to use the something like the magic __DIR__ constant to construct an absolute path, or adjust the relative path accordingly.

    I’m going to go out on a limb a little bit here and say that if you change the first line to:

    $cachefile = "resources/data/cache/states.html";
    

    …you will probably find it works at you expect it to.

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

Sidebar

Related Questions

I have a pretty simple ASP.NET Web Form that looks a bit like the
Situation: you have a web page with a form that has a first name
I have a web form that has 2 radio buttons, depending on which one
I have a web app that has a big and complex form (fields, checks,
I have a index.jsp file that has two different types of forms <form action=searchpath
I have a web application that contains a page with a form that has
I have a web form that has multiple ListBoxes, TextBoxes, DropDowns. If I put
I have a web form that inserts a row into an SQL Server 2008
I have a web form that collects information and submits it to a cgi
I have a web form that manipulates records in a MySQL database. I have

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.