2

I have a PHP script which runs an external process.

The external process outputs a JSON string with data from the system which changes every now and then.

When I run the external process I can clearly see that the JSON string is different every time I run it.

However, when I call my PHP script using AJAX from a web browser the JSON string is not updated after the first call to the PHP script.

The only time the JSON string gets updated is if I refresh the page or run the external process manually while I'm inside the website.

Why is this happening?

Does LIGHTTPD cache the output?

EDIT:

Here's my LIGHTTPD config file content:

server.modules = (
    "mod_access",
        "mod_cgi",
    "mod_alias",
    "mod_compress",
    "mod_redirect",
#       "mod_rewrite",
)

server.document-root        = "/var/www"
server.upload-dirs          = ( "/var/cache/lighttpd/uploads" )
server.errorlog             = "/var/log/lighttpd/error.log"
server.pid-file             = "/var/run/lighttpd.pid"
server.username             = "www-data"
server.groupname            = "www-data"
server.port                 = 80


index-file.names            = ( "index.php", "index.html", "index.lighttpd.html" )
url.access-deny             = ( "~", ".inc" )
static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" )

compress.cache-dir          = "/var/cache/lighttpd/compress/"
compress.filetype           = ( "application/javascript", "text/css", "text/html", "text/plain" )

# default listening port for IPv6 falls back to the IPv4 port
include_shell "/usr/share/lighttpd/use-ipv6.pl " + server.port
include_shell "/usr/share/lighttpd/create-mime.assign.pl"
include_shell "/usr/share/lighttpd/include-conf-enabled.pl"

$HTTP["url"] =~ "/cgi-bin/" {
        cgi.assign = ( "" => "" )
}

cgi.assign      = (
        ".cgi"  => ""
)

EDIT

According to the following Google Product thread a person called johnjbarton mentions the following:

Ok I spoke with a Chrome cache expert. The problem in my case is very likely described on this bug: Bug 30862 - Dynamically inserted subresources aren't revalidated even when the containing document is reloaded https://bugs.webkit.org/show_bug.cgi?id=30862

The force-refresh only forces resource loaded in the outer page. It does not force refresh on XHR loaded resources. Which is pretty much all of the resources in a dojo app.

When I look at the console of chrome under the Network section it says that all of the requests that my website is making are of type XHR.

Can this be somehow related?

EDIT

The headers of my request are

Accept */*

Content-Type application/x-www-form-urlencoded

VERY IMPORTANT EDIT I edited my PHP script so that it appends the results of the process it executes in to a log file.

It turns out that the output of the process always is the same.

However when I run the process manually it constantly changes as expected.

It seems to me as if LIGHTTPD is caching the output of the process that it executes.

EDIT

This is what my PHP script looks like:

<?php
session_start();
if(!isset($_SESSION['user'])){
    header("Location: /");
} else {
    header('Content-type: application/json');
    $output = shell_exec('/home/debian/myProcess/myProcess');
    file_put_contents("/var/log/lighttpd/access.log", $output, FILE_APPEND | LOCK_EX);
    echo $output;
}

?>

vaid
  • 1,390
  • 12
  • 33
  • Can you post relevant parts of lighttpd config? – Will Feb 29 '16 at 04:39
  • In your browser check the network traffic when doing the AJAX calls, it's quite likely the result is cached in your browser. Check with whatever JS framework you are using for AJAX on how to avoid caching AJAX responses, if not using a framework you'll have to e.g. add timestamp to each query URL. – Eborbob Feb 29 '16 at 11:35
  • @Eborbob oh, ok. So how do I add a timestamp and where do I add this timestamp? Do I add it on the client side or server side? Do you have any suggestions on where I can read more about this? And finally: Why would a timestamp change the behaviour of the browser caching? – vaid Feb 29 '16 at 11:43
  • @Eborbob do you mean something like answer #2 on this SO? http://stackoverflow.com/questions/367786/prevent-caching-of-ajax-call – vaid Feb 29 '16 at 11:44
  • Say you make an AJAX call to `http://example.com/ajax.php?action=ping`, to prevent the browser caching the response you could instead call `http://example.com/ajax.php?action=ping&nocache=1456765882`. Since this URL is different the browser will send the request to the server rather than getting something from its cache. – Eborbob Feb 29 '16 at 17:13
  • Yes, the answer you linked to is correct if you're using jQuery. – Eborbob Feb 29 '16 at 17:14
  • @Eborbob I tried changing the URL on each request by adding "?rnd=" but I still get the same results. Any other suggestion? – vaid Feb 29 '16 at 19:16
  • Check in your Lighttpd logs to make sure that the AJAX requests are getting through to the server. That should help you narrow down the problem to either the server or the client side. – Eborbob Feb 29 '16 at 21:23
  • What `Content-Type` header are you using to send your JSON? – Will Feb 29 '16 at 22:37
  • @Eborbob according to the logs it all seems OK. Every single request is registered, each with their matching random number at the end. So nothing is wrong there. My suspicion is now the same as your initial suspicion; the browser is caching the results. However this time completely ignoring the modified URL. – vaid Mar 01 '16 at 00:52
  • @Will I use `header('Content-type: application/json');` – vaid Mar 01 '16 at 00:52
  • Please see the edit in my question. – vaid Mar 01 '16 at 01:03
  • I think you'd better show your PHP code. – Eborbob Mar 01 '16 at 09:26
  • @Eborbob sure. I added the code of my PHP script to the bottom of my question. – vaid Mar 01 '16 at 12:53
  • What is the process that you are running (`/home/debian/myProcess/myProcess` in your code above)? If you change it to `/bin/date` or something else like that what happens? – Eborbob Mar 01 '16 at 13:07
  • The process `/bin/date`works flawlessly. The time is updated and received by the web browser – vaid Mar 01 '16 at 17:50
  • I found the problem. – vaid Mar 02 '16 at 02:41

1 Answers1

1

Ok, so I found the cause of the problem.

The process I am executing which I call myProcess runs a process itself called iwlist.

iwlist seems to require to be run as root.

Since my PHP script executes ANY process as the LIGHTTPD server user called www-data, the process myProcess will be executed as www-data which in turn tries to execute iwlist as www-data as well, and that is where it all failed.

My solution was to run myProcess through a daemon which dumps the output of myProcess to a file which my PHP script can read.

This works perfectly. `

vaid
  • 1,390
  • 12
  • 33