Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I was wondering if it's possible to use php to create a file with a specific name that would last 30 seconds, and then delete its self. I looked at the tmpfile and tmpnme functions but I must not know how to use them correctly.
I would like to click a button on webpage A, and then have information shown on webpage B for 30 seconds. Once the 30 seconds is up, the information would change or clear out entirely.
Possible?
PHP can easily create a temporary file for you, but it'll be up to you to actually remove it after the 30 seconds is up. PHP isn't a scheduler, though it can schedule things for you via cron or at.
Your best bet would be to embed some extra code into the PHP script as it's built to specify an expiry time, which the generated script can check when it's first started:
<?php
$expires = ...insert some timestamp value here...;
if ($expires < time()) {
unlink(__FILE__);
header("Location: somewhere else");
exit();
}
... do whatever you need to here ...
That'll take care of the script terminating itself. But if the script is never accessed outside of the 30 second window, it wouldn't clean itself up, so you'd still need an external job to go in and do some periodic housecleaning.
You can create a file like an XML for example. Add a new node and time stamp it. Then your website can load the file and based on the timestamp show the node that hasn't expired. You don't have to delete the file just don't show the old data. You can also when loading the node, delete the old ones to keep the file from getting large.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a text file, that stores chat log and I want the content to be cleared automaticly by the server when the clock is 00:00:00.000, or let’s say at a certain time, so that it’s cleared every 24 hours. I found something similar here but I want the server to automaticly clear not delete the txt file.
If you want it to run automatically you can set up a cronjob.
Or if you are on Windows use the Task Scheduler.
Using this you can run for example a php file on a specific time using:
php -f /path/to-your/file
An example of a line in the crontab will look like:
0 0 * * * php -f /path/to-your/file
The above will run the script every day at 00:00
create a php file /path/to/your/php/script.php
<?php
$log_file = /path/to/your/log/file.txt;
file_put_contents($log_file, '');
?>
now set cronjob on your server
0 0 * * * php -f /path/to/your/php/script.php
and you are done...
EDIT: if you don't want to use cronjob, then you can use this method
create a file name clear_logs.php
<?php
$_00hours = date('His');
if($_00hours == '000000'){
$log_file = '/path/to/your/log/file.txt';
file_put_contents($log_file, '');
}
?>
Now include this file in your website files
Note: Second method is not recommended as user must come to website at 00:00:00 so that the statements under if condition can be executed
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
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have created one simple note taking application in jquery & php.
Current functionality is like when user clicks save button it sends a ajax request with complete data to update it in db(mysql).
All works well. But now I want to auto save only changed text while user typing.
I don't want to send entire text to server again and again in text change event.
It should send only text which has changed.
For Ex:
This is saved text.
When user continues typing.
This is saved text. Unsaved text..
It should be able to send only "Unsaved text.." to server to update in db.
How can i implement this in jquery & server side script.?
Any Idea..?
I do not think this is a good idea. First of all why do you want this functionality? Just for fun, I saw it somewhere and thought that it will be cool is not really valid. One of the valid reason I see is that the notes will be huge and you do not like to send them constantly to a server. But you have to understand that with a text note it is hard to get a huge note. You have to type a lot in order to surpass 1KB.
If you really need it, you have to handle finding changes on a client and on the server. On a client you have to find what has changed (and taking into the account how much time have you spent thinking about a question, I highly doubt you can do this). It is not just adding new text in the end. What if person will:
add the text in the middle
remove some text
substitute a string with another string
make multiple above-mentioned things
Even if you will do this, when you receive this info on the server, you have to write a logic to change your field in the database. And it will be much harder than just simple update.
So my thoughts about this: do not over complicate things.
If you afraid that the person will lose his changes and do not want to send new draft to the server every time a user added/deleted few chars, than save it locally with the help of localstorage (html5 localshorage) or cookies and build your client side logic upon this.
An option would be to make the richtextbox a Content Editable div containing a simplified HTML markup so that when you add something it appears in a span like this:
<span class="changed added" data-pos"23">new text</span>
where data-pos is the location of the inserted text in the textbox and when you delete something it appears in a span like this:
<span class="changed deleted">deleted text</span>
That way when you click save you can send only the changed text $('#richtextbox span.changed') back to the server for processing. Then the server can do a string replace to add or remove the text as needed from the original text in the database.
I recently built a quiz web application using PHP and MySQL (www.ReadySetQuiz.ME) The format looks something like this:
When did we declare Independence from the British?
1775
1777
1776
1774
The Louisiana Purchase was an aquisition from which country?
England
France
Spain
Germany
The answers are a list of radio buttons. After you answer all the questions you hit the submit button and it shows how many you got correct and a breakdown of each question: whether you chose the correct answer (if not, which was the correct answer) and a distribution of previous users' choices.
One big problem I'd like to solve with this current quiz design is people can just open another tab in their browser and Google the answer. I'd like to only show 1 problem at a time and add a time limit to encourage quick responses, and consequently, eliminate the aforementioned problem.
So, I'm looking for some input/guidance - big picture (50,000 feet). How do I make it so one question is presented at a time and users only have a few seconds to answer the question? Do I use JavaScript? Are there any new improvements in HTML5 that can help me accomplish something like this? I'm really just looking for what I need to learn next. Thanks for your time!
Question1) How do I make it so one question is presented at a time and users only have a few seconds to answer the question?
You need to use a countdown/timer to present the user for a minute of less.
Question 2) Do I use JavaScript?
Yeah you can use javascript and use jQuery to make your life easier.
Also disabling right click menu and text selection in the browser window (using js) will make it difficult for the user to just copy the keyword and paste it in a new browser and search. So if they will have to type the question/keywords it will take time.
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);
}
?>