PHP - Clear content in txt file every 24 hours [closed] - php

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

Related

Streaming a text file, auto update [closed]

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

How to created this type of layout correctly (containing PHP) [closed]

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 8 years ago.
Improve this question
Right what I am trying to do is create a layout like the image below :-
This layout will be used for over 50+ pages, so I would like to know the best way to go about creating this. I have given it ago but I can't seem to get my head around how this should work.
I have been trying to get it to work like so:
header.php - Holds the side bars. Sidebar 1 float: left; and sidebar 2 float: right and these are in a container to hold them in place.
index.php - Hold content such as a table with stats about the user (in a HTML table). This is wrapped in a div (main content). At the top of the page I include("header.php");. This does work but when the PHP at the top of the page uses echo the value will not be within the div.
Basically the layout I created is here:- DEMO HERE
This is how I want the layout to look but now here is the problem. I am using PHP to calculate stuff e.g:
if($userhp <= 0) {
echo "You are dead";
}
Now this code is located at the top of the page, this means it would echo out before the div (for the main content) was opened. Should this code be within the div?
The thing is this example is within a larger if statement so I would have to bring that down with it, some of these blocks can be 100+ lines of code.
So I'm not sure how to go about this, a nudge in the right direction would be great.
If you need anymore information or I didn't make something clear enough please leave a comment and I will get back to you.
Take a look at output buffering. You can store your output in a variable and print it later in your div:
<?
ob_start(); // Turn on output buffering
if ($userhp <= 0) {
echo "You're dead";
}
...
$contents = ob_get_clean(); // Store output in $contents
...
?>
<div id="content"><?php echo $contents ?></div>

How to add a line of text to a text file every thursday with PHP?

I have a textfile with a list of the newest minecraft snapshots which then gets displayed on my personel site, the problem is that I have to manually add the snapshot name every thursday and I cant find any way to automate this process.
The text file is laid out like this:
12w26a;
12w25a;
12w24a;
'12' stands for the current year, 'w' just stands for week, '26' indicated what number snapshot it is and 'a' isn't really important but has to be there.
I found some PHP online to automate the process but apparently it no longer works
http://pastebin.com/LP3WKCiZ
Any help would be much appreciated. :)
EDIT:
Here is a live demo of the above code (not sure why its not working since Mojang have gone back to just using jar files again)
http://langkid1.me/pre/
Create a php page that reads the text file contents and then checks whether another snapshot exists via the file_exists() function, and if it does it should update the text file that maintains the list.
Now create a script that contains a curl command to the php page.
#!/bin/sh
curl http://localhost/update.php
And finally you just need to set up a cron job to run the script every Thursday.
0 0 * * 4 sh /path/script.sh
Hope this helps. :)

Creating Temporary Filepage in Php - Possible? [closed]

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.

cronjob delaying based on PHP output

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);
}
?>

Categories