file() is using cached version - php

When using:
file('http://example.com/test.txt');
after changing test.txt, i get the chached version for like 5 mins or so. Anyway to disable this caching so that every load it is fresh from the server?
I am attempting to make a way to check for updates of my script.. Here is my code..
$configLines = file($url);
foreach ($configLines as $av)
{
list ($a, $v) = explode("=", $av);
$$a = $v;
}

You could try to append some dummy parameter to the URL. This might trick the server into thinking that you are actually requesting a different file -
file('http://example.com/test.txt?foo='.time());

Related

How To Make a PHP Script Synchronously Update From Terminal Values

Using a Raspberry Pi I created a script which loads the CPU Temperature of the Pi through an Apache Server onto the Browser.
<?php
$temp = exec('vcgencmd measure_temp');
$temp = str_replace('temp=','',$temp);
$temp = str_replace('\'C','',$temp);
echo $temp;
?>
Using the code above I have to manually refresh the page to see the latest value.
It works fine but I'd like to know how I could set this up without having to refresh the browser all the time.
Within the Terminal on the Pi I was able to use the "watch" command which will give me the current value every 0.1 seconds.
But by executing this script, the browser will give me a blank page.
<?php
$temp = exec('watch -n 0.1 vcgencmd measure_temp');
$temp = str_replace('temp=','',$temp);
$temp = str_replace('\'C','',$temp);
echo $temp;
?>
Is there any way to make the script using the "watch" command work with the PHP Script? If not, is there any other way to make it refresh everytime the value changes in the terminal?
Note: I am new to programming and using the Pi.
I would really appreciate any helpful information!
Thank you in advance!
Watch wont work in your case, you can call jquery cdn from official website and then do this function. Do not forget to open console to see what comes back. F12
Add this into your php file.
if(isset($_GET)){
$temp = exec('vcgencmd measure_temp');
$temp = str_replace('temp=','',$temp);
$temp = str_replace('\'C','',$temp);
echo $temp;
}
Then into your index.html
$(function() {
startRefresh();
});
function startRefresh() {
setTimeout(startRefresh,1000); // 1000 represents 1 second, free to change
$.get('index.php', function(data) { // i assume your index.php in same folder with your html file.
console.log(data);
});
}
I actually found a easier way to set this up.
What I wanted to do was not having to manually refresh the page in order to get the current temperature values.
The answers above were correct but I wasn't able to set it up by myself so I figured out I can add a header and refresh into my PHP script which will make the page refresh every second (or whatever timeframe is needed).
The code looks like this now:
<?php header('refresh: 1');
$temp = exec('vcgencmd measure_temp');
$temp = str_replace('temp=','',$temp);
$temp = str_replace('\'C','',$temp);
echo $temp;
?>
Thank you to everyone who was trying to help me!

PHP - Hit counter textfile reset

I have an issue with my non-unique hit counter.
The script is as below:
$filename = 'counter.txt';
if (file_exists($filename)) {
$current_value = file_get_contents($filename);
} else {
$current_value = 0;
}
$current_value++;
file_put_contents($filename, $current_value);
When I'm refreshing my website very often (like 10 times per second or even faster), the value in the text file are getting reset to 0.
Any guess for fixing this issue?
This is a pretty poor way to maintain a counter, but your problem is probably that when you fire multiple requests at the site, one of the calls to file_exists() is getting a false because one of the other processes is removing and recreating the file.
If you want this to work consistantly you are going to have to lock the file between read and write See flock on php manual
Of course without the file lock you would also be getting incorrect counts anyway, when 2 processes manage to read the same value from the file.
Locking the file would also potentially slow your system down as 2 or more processes queue for access to the file.
It would probably be a better idea to store your counter in a database, as they are designed for coping with this kind of quick fire access and ensuring every process is properly queued and released.
Does it help if you add a check if file_get_contents isn't returning false?
$value = file_get_contents($filename);
if($value !== false)
{
$current_value = $value
}

Why is DOMDocument not working with WAMP server?

I’m using DOMDocument to retrieve several bits of text from a webpage and place them into an array. The same code works on another server, yet doesn’t on mine. I get Trying to get property of non-object for each iteration of the while loop and the array remains empty at the end.
$html = file_get_contents("http://sugarkettle.site44.com/catering.html");
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html);
libxml_clear_errors();
$meatPrices = array();
function fillArrayFromDOM($array,$type) {
global $doc;
$i = 0;
$label = 1;
$array = array();
while ($label <= 15):
$array[$i] = $doc->getElementById($type.$label)->textContent;
$i++;
$label++;
endwhile;
return $array;
}
fillArrayFromDOM($meatPrices,"meat");
echo var_dump($meatPrices);
Here’s a link to it working:
http://www.evintr.com/willtest.php
He’s running a GoDaddy server and I have a local WAMP (2.2) server. Any configuration options I can provide that might explain why this is happening? Or does the problem have nothing to do with my server config?
Any help much appreciated. Thanks in advance!
Update 1 - 11/16/12
On my server, I've tested $meatPrices[1] = $doc->getElementById('meat1')->textContent; and it works. For whatever reason, inside the while loop the same expression (except with variables in the getElementById parameters) tosses an error: Trying to get property of non-object.
Update 2 - 11/17/12
My WAMP server is running PHP version 5.3.13.
My friend's server is running PHP version 5.3.6.
Try with adding allow_url_fopen=on in your PHP configuration file (php.ini). Save it and restart Apache; it should work...
EDIT:
Check also if you have extension php_openssl.dll enabled (extension=php_openssl.dll in your php.ini file). Again, restart of Apache would be required.
EDIT:
It depends on PHP version you have but here are two potential solutions:
replace line fillArrayFromDOM($meatPrices,"meat"); with
$meatPrices = fillArrayFromDOM($meatPrices,"meat"); You can also
change your function to remove necessary $meatPrices parameter).
or
replace line function fillArrayFromDOM($array,$type) { with function fillArrayFromDOM(&$array,$type) { //note new character &; it will keep reference to $array variable so it could be changeable; you can also remove line: $array = array();
Both should work; I am in rush have no time to wait on your comment response. Let us know what you get...

Memcached item deletion doesn't work

I stumbled upon a weird behavior of Memcached server (version 1.4.5):
I have a single server and I'm trying to delete a stored value and it doesn't work as the item remains there (and I don't receive any error).
I wrote a quick PHP code that shows the problem:
$memcache_object = memcache_connect(MEMCACHED_SERVER_ADDRESS, MEMCACHED_SERVER_PORT);
$key = '64b788714dx7cds5350101e37ec0ddd40253123d';
$myObject = memcache_get($memcache_object, $key);
echo count($myObject); // Prints 1000
memcache_delete($memcache_object, $key);
$myObjectSecondTry = memcache_get($memcache_object, $key);
if (empty($myObjectSecondTry))
echo 'Empty'; // It does print it's empty
memcache_close($memcache_object);
Now if I run the code once it says "1000" and also it says that the object was empty on the second memcache_get() try.
But if I click refresh and run it again then the $key still exists on the memcached server and I get the same output.
I also tried to reconnect between each memcache call (i.e. get->delete->get) but it didn't help.
The only thing that clears the memory is reseting the Memcached service.
Please advise.
As far as I remember, it was an issue with the timeout so can you please try to use:
memcache_delete($memcache_object, $key, 0);

Updating an array before MySQL insert?

I'm receiving an XML via php://input and after using simpleXML to break the elements down into variables and then what I want to do is append an array, or create an array of the variables every 30 seconds or so.
The reason is this script will be getting regular inputs, and rather than doing loads of mySQL updates or inserts, I assume it might be better for efficiency.
So, a couple of questions if anyone has a moment.
1) is there a way to check for a new input on php://input.
2) is there a better way to do this repeat check than sleep function?
3) how do I append/add to an array with these updating variables?
I haven't gone too far yet, so the code isn't useful but if you can forgive me simpleness:-
function input() {
$xml = new SimpleXMLElement($input);
$session_id = $xml->session_id;
$ip = $xml->ip;
$browser = $xml->browser;
store($session_id, $ip, $browser);
}
function store() {
$session_id = array();
$ip = array();
$browser = array();
}
If I understand you correctly, it seems that you are trying to use PHP for a long-running stateful program. I trust you know the following:
PHP programs generally don't run for longer than a few miliseconds, at most a few seconds for the typical web application. Every time a resource is requested from the PHP handler, the parsing begins anew and there is no program state that remains from the previous execution. Being a stateless environment, it is up to you to maintain the state. For this reason, PHP is not made to handle input that changes with time, or to maintain a state.
This being said, the simplest way to append to an array is the follwing:
$myarray[] = "newvalue";
or
$myarray['newkey'] = "newvalue";
To process the stream:
while (!feof($handle)){ $data = fgets($handle, 4096); }

Categories