rad1964 Posted November 29, 2012 Share Posted November 29, 2012 Is there some sort of error in coding with this goto routine? I want it to erase a file after the choosen time has elapsed... Quote Link to comment https://forums.phpfreaks.com/topic/271369-write-and-erase-contents-of-a-file-after-a-length-of-time-has-expired/ Share on other sites More sharing options...
rad1964 Posted November 29, 2012 Author Share Posted November 29, 2012 a: if (time() > ($timestamp + $timedalertseconds)){ $fp = fopen ($filename, "w"); if ($fp) { // overwrite the file fwrite ($fp); fclose ($fp); echo ("<span style='color: #6FF;'>There are no alerts.</span> "); } } else { goto a; } Quote Link to comment https://forums.phpfreaks.com/topic/271369-write-and-erase-contents-of-a-file-after-a-length-of-time-has-expired/#findComment-1396248 Share on other sites More sharing options...
requinix Posted November 29, 2012 Share Posted November 29, 2012 Yes. That is a very bad thing to write and one of the numerous reasons why goto is bad. Don't use goto. Use a database for this, not a file. That will give you much more control over how these alert things work and it's much easier to deal with since you don't have to worry about all the little gotchas about dealing with files. Quote Link to comment https://forums.phpfreaks.com/topic/271369-write-and-erase-contents-of-a-file-after-a-length-of-time-has-expired/#findComment-1396257 Share on other sites More sharing options...
rad1964 Posted November 29, 2012 Author Share Posted November 29, 2012 I am trying to use as few resources as possible because I work for a very large compnay and each time I gain access to another level (aka PHP) there seems to be another level I need to take it to (aka database) They have they're own version of chrome browser and are very tight with giving access to stuff. So rather then try and gain CRON or Database access I need to come up with a solution with the tools I have. Is there not a way to calculate this very simple timed equation? Quote Link to comment https://forums.phpfreaks.com/topic/271369-write-and-erase-contents-of-a-file-after-a-length-of-time-has-expired/#findComment-1396276 Share on other sites More sharing options...
requinix Posted November 29, 2012 Share Posted November 29, 2012 Think of it not in terms of clearing the file but whether you show its contents. - When you display the alerts, if the file is too old then don't show anything - When you display the alerts, if the file is not too old then do show it - When you add alerts, if the file is too old then overwrite everything - When you add alerts, if the file is not too old then append into it Get how that works? Quote Link to comment https://forums.phpfreaks.com/topic/271369-write-and-erase-contents-of-a-file-after-a-length-of-time-has-expired/#findComment-1396283 Share on other sites More sharing options...
rad1964 Posted November 29, 2012 Author Share Posted November 29, 2012 I am having a hard time wrapping my mind around this... no matter if I hide or overwrite file I still need a way to compare the time passed and I can't think of a way to do it with a loop. I am a relative newbie when it comes to PHP. Quote Link to comment https://forums.phpfreaks.com/topic/271369-write-and-erase-contents-of-a-file-after-a-length-of-time-has-expired/#findComment-1396291 Share on other sites More sharing options...
requinix Posted November 29, 2012 Share Posted November 29, 2012 Perhaps an explanation of what these alerts are and when they're displayed is in order? Quote Link to comment https://forums.phpfreaks.com/topic/271369-write-and-erase-contents-of-a-file-after-a-length-of-time-has-expired/#findComment-1396302 Share on other sites More sharing options...
rad1964 Posted November 30, 2012 Author Share Posted November 30, 2012 The alerts are sent from a secure web page to a web app. When an alert is sent, the web app, when accessed, pops up an alert. There is a secondary field to send an alert which will stack multiple alerts. The only way at present time to remove either a solo alert or stacked alert is to clear all alerts, which overwrites the file with nothing, thus clearing the alert. I now added a timed alert where the admin can send between 1 hour and 48 hours which should expire the alert and overwrite a blank file. On the actual web app I have this javascript: <script type="text/javascript"> $(function() { $.get( "alertCheck.php", function(alertText) { if (alertText != "") { alert(alertText); } }, "text" ); }); </script> on the input side of the timed alert I have this form: <form action="processtime.php" method="post" name="timedalert"> <strong>Post a Timed Alert: </strong><input name="timedalertsingle" size="60" type="text" /><br /> <strong>How many hours this alert will be displayed: </strong> <select name="timedalertchoice" /> <option value=".0833">5 Minutes</option> <option selected="selected" value="1">1 hour</option> <option value="2">2 hours</option> <option value="4">4 hours</option> <option value="6">6 hours</option> <option value="8">8 hours</option> <option value="12">12 hours</option> <option value="18">18 hours</option> <option value="24">24 hours</option> </select> <input name="submit" type="submit" value="submit" /><br /> </form> And finally my processtime.php: <?php $filename = "alertCheck.php"; $timedalertsingle = $_POST['timedalertsingle']; $timedalerthours = $_POST['timedalertchoice']; $timestamp = time(); $timedalertseconds = $timedalerthours * 3600; // Testing the variables echo $timedalertsingle."<br />"; echo $timedalerthours."<br />"; echo $timestamp."<br />"; echo $timedalertseconds."<br />"; echo ($timestamp + $timedalertseconds)."<br />"; // write the file $fp = fopen ($filename, "w"); if ($fp) { fwrite ($fp, $timedalertsingle); fclose ($fp); echo ("<span style='color: #6FF;'>File written:</span> " . " This alert will last for " .$timedalerthours . " Hour(s)" . "<br />"); echo date('m-d-y') ."<br />"; } else { echo ("File was not written"); } // test the time a: if (time() > ($timestamp + $timedalertseconds)){ $fp = fopen ($filename, "w"); if ($fp) { // overwrite the file fwrite ($fp); fclose ($fp); echo ("<span style='color: #6FF;'>There are no alerts.</span> "); } } else { goto a; } ?> rad1964 tia Quote Link to comment https://forums.phpfreaks.com/topic/271369-write-and-erase-contents-of-a-file-after-a-length-of-time-has-expired/#findComment-1396511 Share on other sites More sharing options...
requinix Posted November 30, 2012 Share Posted November 30, 2012 Here's how I would probably do it. No formal database? Fine. Make your own rudimentary version: keep a JSON array in a file that holds all the alerts and their expiration times. Like $alerts = array( array(time() + 3600, "This alert will expire in one hour"), array(time() + 86400, "This alert will expire in 24 hours"), array(mktime(23, 59, 59), "This alert will expire at the end of today") ); [[1354304622,"This alert will expire in one hour"],[1354387422,"This alert will expire in 24 hours"],[1354348799,"This alert will expire at the end of today"]] To add an alert, 1. Read in the file and json_decode() it back into an array 2. Filter out the alerts that have expired 3. Add the new alert at the end 4. json_encode() the array and write it back into the file To show the alerts, 1. Read in the file and json_decode() it back into an array 2. Show the alerts that have not expired Quote Link to comment https://forums.phpfreaks.com/topic/271369-write-and-erase-contents-of-a-file-after-a-length-of-time-has-expired/#findComment-1396520 Share on other sites More sharing options...
rad1964 Posted November 30, 2012 Author Share Posted November 30, 2012 I am sure that would work, but I am wishing I never posted this question. my current alertCheck.php would be useless my single alerts and multiple alerts would also be useless, so this solution while perhaps providing a timing answer, negates what I have already done, thus making it pointless. No offence. Quote Link to comment https://forums.phpfreaks.com/topic/271369-write-and-erase-contents-of-a-file-after-a-length-of-time-has-expired/#findComment-1396525 Share on other sites More sharing options...
rad1964 Posted November 30, 2012 Author Share Posted November 30, 2012 I will ask again ... why cant PHP tell PHP when PHP is done running a countdown timer... Quote Link to comment https://forums.phpfreaks.com/topic/271369-write-and-erase-contents-of-a-file-after-a-length-of-time-has-expired/#findComment-1396526 Share on other sites More sharing options...
Psycho Posted November 30, 2012 Share Posted November 30, 2012 I am sure that would work, but I am wishing I never posted this question. my current alertCheck.php would be useless my single alerts and multiple alerts would also be useless, so this solution while perhaps providing a timing answer, negates what I have already done, thus making it pointless. No offence. You are over-thinking/over-engineering your problem. Requinix provided a the solution for a basic process that would give you the results you are after. I think you aren't seeing the forest through the trees. His process would allow you to manage the alerts coming from the external web process as well as ones that are manually added - either singular or stacked. I will ask again ... why cant PHP tell PHP when PHP is done running a countdown timer... You are asking the wrong question because there is no need to run a count-down timer. When someone access the page where alerts will be shown you simply need to see if there are any new alerts to process. If so, update the "master" file with the new alerts. If no new updates - then don't update the master file. Then, show any current alerts from that master file. Quote Link to comment https://forums.phpfreaks.com/topic/271369-write-and-erase-contents-of-a-file-after-a-length-of-time-has-expired/#findComment-1396527 Share on other sites More sharing options...
rad1964 Posted November 30, 2012 Author Share Posted November 30, 2012 OK, But I am still lost. I am new to this and your solution is aparently above my head because whether it is correct and right does not mean I can interpret it to code. Thats why I do not want to abadon the code I wrote. Because I wrote it, I understand what its doing. (Or is suppoed to do). <pre class="prettyprint"> 1. Read in the file and json_decode() it back into an array </pre> Lets start here... read in the file, - what the heck does that mean? json_decode() -??? <pre class="prettyprint"> 2. Filter out the alerts that have expired </pre> How do we know if they have expired or not. presently most alerts will be manually entered and manually erased. <pre class="prettyprint"> 4. json_encode() the array and write it back into the file </pre> without knowing how steps 1-3 run I have no clue here. <pre class="prettyprint"> 1. Read in the file and json_decode() it back into an array 2. Show the alerts that have not expired </pre> Again without knowing how steps above work, I am clueless for this Sorry for being a dumbass, we all start somewhere rad1964 Quote Link to comment https://forums.phpfreaks.com/topic/271369-write-and-erase-contents-of-a-file-after-a-length-of-time-has-expired/#findComment-1396558 Share on other sites More sharing options...
rad1964 Posted November 30, 2012 Author Share Posted November 30, 2012 (edited) delete me Edited November 30, 2012 by rad1964 Quote Link to comment https://forums.phpfreaks.com/topic/271369-write-and-erase-contents-of-a-file-after-a-length-of-time-has-expired/#findComment-1396559 Share on other sites More sharing options...
kicken Posted November 30, 2012 Share Posted November 30, 2012 1. Read in the file and json_decode() it back into an array Lets start here... read in the file, - what the heck does that mean? json_decode() -??? file_get_contents - Read the file json_decode - Decode the json stored in the file. 2. Filter out the alerts that have expired How do we know if they have expired or not. presently most alerts will be manually entered and manually erased. Whenever you add an alert to the array of alerts, you add a date indicating when it should expire. Check that date against the current date whenever you read the alerts to see if it has expired. If so, remove it. time - Get current date foreach - Loop the alerts unset - remove an alert 4. json_encode() the array and write it back into the file json_encode - Encode the alerts array as json data file_put_contents - Save the data to a file Quote Link to comment https://forums.phpfreaks.com/topic/271369-write-and-erase-contents-of-a-file-after-a-length-of-time-has-expired/#findComment-1396568 Share on other sites More sharing options...
Psycho Posted December 3, 2012 Share Posted December 3, 2012 OK, But I am still lost. I am new to this and your solution is aparently above my head because whether it is correct and right does not mean I can interpret it to code. Thats why I do not want to abadon the code I wrote. Because I wrote it, I understand what its doing. (Or is suppoed to do). Please see kicken's post above that should help you get started with coding it in a better way. We understand that you may be new, but the process you are trying to implement is a very poor one. It would reflect poorly on us if we were to encourage bad behaviors in someone because they have some code they think they understand. Let me break down they basics of what we are proposing so it will, hopefully, make sense to you. I think it would be best to break it down into three distinct processes. But, one thing to understand is that there will be a "master" alerts file that contains all of the current alerts. 1. There should be a process that checks for new alert files. based upon your statements above I assume the external web page writes files to a certain directory. So, there should be logic to check for any new alert files. If any are found, the script will read the contents of those files and update the "master" alerts file (see more on this below). Then delete the file that created the new alert so you don't process it again next time. 2. There needs to be a process to allow you to manually insert/remove alerts. This would be accomplished by providing a page to view current alerts and edit/remove them or to add new alerts. When this page is posted you would check what the user chose to do and then update the "master" alerts file (see more on this below). 3. Updating the master alerts file will be done by reading the file into memory and converting to an array then making updates as follows: a. If the user chose to make a manual change then perform the appropriate action: remove/edit the selected alert or add a new alert. b. If new alerts were processed from files, then add them to the array and delete the file (so you don't add them again.) Once all changes have been made you will then encode the array back to JSON and write the contents to the "master" alerts file. c. Also, you can iterate over the alerts in the array and remove any that are expired. Now, once all of the above is complete you will have all the current alerts as an array in memory. If there were any changes from the above them convert to a JSON encoded string and write the result back to the master alerts file. Then iterate over the array and show the current alerts to the user Quote Link to comment https://forums.phpfreaks.com/topic/271369-write-and-erase-contents-of-a-file-after-a-length-of-time-has-expired/#findComment-1397040 Share on other sites More sharing options...
rad1964 Posted December 3, 2012 Author Share Posted December 3, 2012 Thanks, I will make an attempt at coding this, then you all can laugh at my code when I post it! bad goto! rad1964 Quote Link to comment https://forums.phpfreaks.com/topic/271369-write-and-erase-contents-of-a-file-after-a-length-of-time-has-expired/#findComment-1397248 Share on other sites More sharing options...
rad1964 Posted December 6, 2012 Author Share Posted December 6, 2012 This works! yea! Thanks for the guidence people! class RadamsAlert { const ALERT_FILE = 'alerts.dat'; function addAlert($alertMessage, $expirationTime){ $alert = array('alert' => $alertMessage, 'expiration' => $expirationTime); $alerts = $this->readAlerts(); $alerts[] = $alert; $this->writeAlerts($alerts); } function readAlerts(){ $alerts = array(); if(file_exists(self::ALERT_FILE)){ $contents = file_get_contents(self::ALERT_FILE); $readAlerts = unserialize($contents); if(count($readAlerts) > 0){ foreach($readAlerts as $alert){ if($alert['expiration'] > time()){ $alerts[] = $alert; } } } } return $alerts; } function writeAlerts(array $alerts){ file_put_contents(self::ALERT_FILE, serialize($alerts)); } } $timedalertsingle = isset($_POST['timedalertsingle']) ? $_POST['timedalertsingle'] : false; $timedalertseconds = isset($_POST['timedalertchoice']) ? (int)$_POST['timedalertchoice'] : 0; $timestamp = time(); if($timedalertsingle !== false && $timedalertseconds > 0){ $radAlert = new RadamsAlert(); $radAlert->addAlert($timedalertsingle, $timedalertseconds + $timestamp); } header('Location: sec.html'); exit(); rad1964 Quote Link to comment https://forums.phpfreaks.com/topic/271369-write-and-erase-contents-of-a-file-after-a-length-of-time-has-expired/#findComment-1397992 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.