Yesterday I created a script which is working fine, but only with an opened Web Browser which isn't that what I wanted. What I want is that the script runs all the time, even with closed Web Browser.
Could not upload a Picture, so its a short sketch:
(lookup.php) -> pass var data1 -> (run_code.php) -> pass var data1 ->
(check.php) = {{refreshes every 5 seconds till var data2 exists in
MySQl.}} -> goto -> lookup.php.....
The only problem is that I have no idea how to send a value from one .php file to another without GET,POST,COOKIE or SESSION. Session would be the best option but "lookup.php" is looking for a value, and if I start session I get the error "header is already set".
So, how should I pass these value from "lookup.php" to "run_code.php"?
Second problem is, "check.php" is a code that checks if value exists in mysql. This code refreshed and executes itself after 5 seconds using META REFRESH. But also this is not working without a browser.
How should I write the script that the script executes itself after a time?
If i understand you want to write a script (and choose php probably because your familiar with its syntax) and you need it to run every X minutes.
Problem is that when you write a web site with PHP you can pass information using HTTP methods (get/post) and using the session
when running a script on a machine you don't have the HTTP traffic and no session (this can be simulated but its a bit complicated)
my suggestion is :
1) combine all the php files into 1 long php file that will be running (this way you can work with variables in the script with no problem) - you can copy past your code into 1 php file and you can include the needed files in your script so you can stile keep the original files
2) add a cron job (if its a Linux system) - http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/ - this way your script with run every X time (your choice)
Related
I have searched and found some suggestions to my question but almost all of them are executing php files, so i don't know if that has something to do with it not working for me.
My goal is for my webpage to load completely without finishing my script that takes x amount of time, but it wont do it with this line of code. Is there something im missing? i have seen this answer in many places and it seems to work for them.
<?php
exec("sudo ./EscalonVel 50 2 100 10 20 &> /dev/null &");
echo "Hello";
?>
If you has to show on the page data calculated on the C script, then "someone" has to wait until it's finished to show it out. If the data comes directly from the command executed (output/stdout of the execution), then you cannot background the command with &: the output might come after request is dispatched, and process backgrounding disconnects it's output from actual execution. So you have 2 options:
Show the page "template" completely and prepare it to accept the contents at the very end of your HTML (suboptimal but possible)
Do a 2 request page: the first shows the template (full page) and the second (AJAX) fills it up with data from command execution. Depending on how you write it, the AJAX can do several request until the command is terminated, which is preferable if the script runs for more than 20 seconds. Then you'll need some kind of process checking and some backend to save (by command) and view (by AJAX request) the data, as a file or database.
Hope it helps!
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have been searching everywhere for a method of streaming a text file. By streaming I mean I want the page to remember its location, and everytime a new entry is added, it will add it to the bottom.
Example
(Displayed on page from txt file)
Jack
John
Ellis
(text file is updated to)
Jack
John
Ellis
Emma
Carly
I want the page to pick up the changes without having to refresh the whole page.
Im learning alot. I have figured out how to display the text files on the page via php (my knowledge base is stuck with php/html only self taught). I can get it to refresh, but im having to use javascript to auto scroll down the page to the bottom everytime, and have the page refreshing every few seconds.
Any ideas or tips?
Well, I would not do this the Shrek's Donkey way:
Are we there, yet?", 5 seconds later, "Are we there, yet?
What is the problem with this?
Bandwidth consumption, pure and simply.
What should I do?
The answer is: Server Push. Or something like Server Push, since is not possible to start things from the server with HTTP.
Instead of poll the server every 5 seconds if there is a new version of the file, why not let the server notify you just when it really has changed?
And how do I do this?
The answer is: with Ajax, but with a different approach.
The high level algorithm is:
Send an Ajax request to the server from the browser.
When the server receives the request, verify if the file has changed.
If it has changed, then return the new data.
If not, make the server sleep for a while and then go back to step 2.
If a long time has passed and there is no modification, the server could (or must) return a response, with no data bounded to it.
How can I check if the file has changed?
Instead of reading the file and comparing the data, a better approach is to send with the request the timestamp of when the file had it last change. You can do this with the filemtime funciton.
On the server, you verify if the file last modified time is bigger than the one coming from the request. If it is, then you read the file and send the response along with the new file modified time (step 2a). If not, go for step 2b and use the usleep function to make the server sleep for a while and save CPU.
To know if a long time has passed and there's no change, you can use the microtime function at the beginning of the script and make the difference of its value on every iteration. If there has been past much time, you'll send an empty response.
Making a draft, the server-side script would be like:
$startTime = microtime();
$filePath = '/path/to/file.txt';
$lastModifiedTime = $_GET['lastModifiedTyme']; // Supposing it comes from the query string
$currentModifiedTime = null;
while ($currentModifiedTime = filemtime($filePath) < $lastModifiedTime) {
usleep(1000); // dorme por 1 seg.
// If has passed more than 1 minute
if ((microtime() - $startTime) > 60000) {
header('HTTP/1.1 304 Not Modified'); // Or simply http_response_code(304) for PHP 5.4+
exit;
}
}
$data = file_get_contents($file);
$jsonResponse = json_encode(array(
'data' => $data,
'modifiedTime' => $currentModifiedTime
);
echo $jsonResponse;
On the client side, you'll have to re-run the request every time you receive a response. This could (and should) have a litte delay.
It sounds like a kind of an overhead, I know, but just for you to know that there are another ways of doing this.
What I'd recommend you do is use AJAX to periodically call a PHP file which loads the text file and update your web content to display the new content.
It will be similar to this: Refresh a table with jQuery/Ajax every 5 seconds
I am using PHP and AJAX requests to get the output of a program that is always running and print it on a webpage at 5 second intervals. Sometimes this log file can get up to 2mb in size. It doesn't seem practical to me for the AJAX request to fetch the whole contents of this file every 5 seconds if the user has already gotten the full contents at least once. The request just needs to get whatever contents the user hasn't gotten in a previous request.
Problem is, I have no clue on where to begin to find what contents the user hasn't received. Any hints, tips, or suggestions?
Edit: The output from the program starts off with a time (HH:MM:SS AM/PM), everything after has no pattern. The log file may span over days, so there might not be just one "02:00:00 PM" in the file, for example. I didn't write the program that is being logged, so there isn't a way for me to modify the format in which it prints it's output.
I think using a head request might get you started along the right path.
check this thread:
HTTP HEAD Request in Javascript/Ajax?
if you're using jquery, it's a simple type change in the ajax call:
$.ajax({url: "some url", type: "HEAD".....});
personally, I would check the file size & date modified against the previous response, and fetch the new data if it has been updated. I'm not sure if you can fetch only parts of a file via ajax, but I'm sure this can be accomplished via php pretty easily, possibly this thread may help:
How to read only 5 last line of the text file in PHP?
It depends how your program is made and how does it print your data, but you can use timestamps to reduce the amount of data. If you have some kind of IDs, you should probably use them insteam of timestamps.
I've got a script in php that continually grows an array as it's results are updated. It executes for a very long time on purpose as it needs to filter a few million strings.
As it loops through results it prints out strings and fills up the page until the scroll bar is super tiny. Instead of printing out the strings, I want to just show the number of successful results dynamically as the php script continues. I did echo(count($array)); and found the number at 1,232,907... 1,233,192 ... 1,234,874 and so forth printed out on many lines.
So, how do I display this increasing php variable as a single growing number on my webpage with Javascript?
Have your PHP script store that number somewhere, then use AJAX to retrieve it every so often.
You need to find a way to interface with the process, to get the current state out of it. Your script needs to export the status periodically, e.g. by writing it to a database.
The easiest way is to write the status to a text file every so often and poll this text file periodically using AJAX.
You can use the Forever Frame technique. Basically, you have a main page containing an iframe. The iframe loads gradually, intermittently adding an additional script tag. Each script tag modifies the content of the parent page.
There is a complete guide available.
That said, there are many good reasons to consider doing more pre-computation (e.g. in a cron job) to avoid doing the actual work during the request.
This isn't what you're looking for (I'm as interested in an answer to this..), but a solution that I've found works is to keep track of the count server-side, and only print every 1000/5000/whatever number works best, rather than one-by-one.
I'd suggest that you have a PHP script that returns the value in JSON format. Then in another php page you can do an AJAX call to the page and fetch the JSON value and display it. Your AJAX call can be programmed to run perhaps every 5 seconds or so depending on how fast your numbers output. Iframe though easier, is a bit outdated.
I'm in charge of a printer, so I wrote a script which runs every 5 minutes and figures out if the printer has paper. If it doesn't, the script will text me. The problem is, if I'm busy, and can't fill the printer, I don't want the script to continue to text me every 5 minutes. Is there a way I can force it to only send me at most 1 text every 8 hours or so, to ensure that the script doesn't text me twice for the same out-of-paper situation? The only thing I can currently think of is to create a db of times that I get texts, then make sure that the most recent one wasn't too long ago, or to create a local file with the most recent time in it.
Thanks!
You need to store somewhere the fact that it has text you and when this last occurred. You could do this using a plain file and by reading the files modification date to see when the text was last sent or you can use a database.
Assuming that the script that sends the SMS is PHP, use something like this.
Can probably find a cleaner way of doing this, but this is just to show you the logic that is needed.
<?
/*
* Replace outOfPaper() / sendSms() with the actual logic of your script
*/
$statusFile = './lastsms';
if(outOfPaper() && (is_file($statusFile) && (filemtime($statusFile) < time()-((8*60)*60)))){
sendSms('+4412345678','Printer out of paper');
touch($statusFile);
}
?>