Spudulene Posted January 20, 2023 Share Posted January 20, 2023 Hello. This is my first time here and I am an absolute novice at php. Someone wrote a code for me years ago. I am able to tinker with it a little bit to change file names, update paths, change what is printed or emailed and maybe a couple of other extremely simple tasks. Basically the script pulls a name from a list and deletes it. Then records the name along with the person who selected that name into a text file. What I want to do is a time stamp when a new entry is added to the text file, preferentially in the same line with the other information that is collected from that form. I did try to look up how to include a time stamp in php, but it broke my page. If anyone can help I would appreciate it. 310nobel_f22-time.txt Quote Link to comment https://forums.phpfreaks.com/topic/315820-including-a-time-stamp-in-my-code/ Share on other sites More sharing options...
ginerjm Posted January 20, 2023 Share Posted January 20, 2023 Can you give us the true code (not a picture) that is doing that output to which you wish to add this date value? That would give us something to actually modify for you Quote Link to comment https://forums.phpfreaks.com/topic/315820-including-a-time-stamp-in-my-code/#findComment-1604857 Share on other sites More sharing options...
Barand Posted January 20, 2023 Share Posted January 20, 2023 try $timestamp = date("Y-m-d H:i:s"); Quote Link to comment https://forums.phpfreaks.com/topic/315820-including-a-time-stamp-in-my-code/#findComment-1604858 Share on other sites More sharing options...
Spudulene Posted January 20, 2023 Author Share Posted January 20, 2023 2 hours ago, ginerjm said: Can you give us the true code (not a picture) that is doing that output to which you wish to add this date value? That would give us something to actually modify for you Thank you. I do not understand what you mean by "true code". I did attach a text file of the code because it would not allow me to attach a file with a .php extension. Below is a sample of what the output should look like. The top line is just a header row for when I import into excel. I will try to put the error line you suggest into my code and see what happens. nobeloutput_f22-310.txt Quote Link to comment https://forums.phpfreaks.com/topic/315820-including-a-time-stamp-in-my-code/#findComment-1604861 Share on other sites More sharing options...
Spudulene Posted January 20, 2023 Author Share Posted January 20, 2023 Hi everyone, For the moment, there is no need to act on this. I have a much bigger problem. The script has stopped working properly. They updated our web server recently and I am guessing there issue with that. Until I get the script working again there is no point trying to add additional code. Thank you for your help to this point. I will come back once I figure out what is going on with the original code, which functioned fine last week. Quote Link to comment https://forums.phpfreaks.com/topic/315820-including-a-time-stamp-in-my-code/#findComment-1604862 Share on other sites More sharing options...
mac_gyver Posted January 21, 2023 Share Posted January 21, 2023 the most likely thing that stopped the code from working are all the $_POST[unquoted associative index name here] in the main code (but not inside of overall double-quoted strings.) in the past, one of php's unfortunate choices assumed that if a defined constant by the given index name doesn't exist, the value was treated as a string. this no longer works and is a fatal run-time error. setting php's error_reporting to E_ALL and display_errors to ON, preferably in the php.ini on your system, will cause php to help you by reporting and displaying all the errors it detects. this code is insecure. by outputting the raw form data on the web page, in the emails, and saving it to a file/importing it into excel, any code in the values will allow cross site scripting. at a minimum, you need to apply htmlentities() to all the values when they are being output, to prevent any html, css, javascript, ... in them from being operated on. the code also has no validation of the input data before using it, could stand to be rewritten in an organized and simplified format, needs error handling and file-locking for the file operations, and needs the markup updated. Quote Link to comment https://forums.phpfreaks.com/topic/315820-including-a-time-stamp-in-my-code/#findComment-1604864 Share on other sites More sharing options...
Spudulene Posted January 21, 2023 Author Share Posted January 21, 2023 Thank you mac_gyver! I agree. I believe the problem is that our server updated their PHP last week and now the very old code has commands that do not work. It would love to reprogram it from scratch. Unfortunately, I do not have the skills to do that. ☹️ Quote Link to comment https://forums.phpfreaks.com/topic/315820-including-a-time-stamp-in-my-code/#findComment-1604875 Share on other sites More sharing options...
Spudulene Posted January 21, 2023 Author Share Posted January 21, 2023 (edited) I put in the code suggested by ginerjm and got the following error: Fatal error: Uncaught Error: Undefined constant "laureate_list" in <URL path>310nobel_f22-time.php:97 Stack trace: #0 {main} thrown in <URL path>310nobel_f22-time.php on line 97 Edited January 21, 2023 by Spudulene Quote Link to comment https://forums.phpfreaks.com/topic/315820-including-a-time-stamp-in-my-code/#findComment-1604878 Share on other sites More sharing options...
ginerjm Posted January 21, 2023 Share Posted January 21, 2023 And now show us line 97 of your code. True code is what you get when you open up your script in an editor and highlight the part you want to show us and you copy it and paste into the forum. Not some image that you create. That way if we wish we can copy it to our own editor and re-work it. Quote Link to comment https://forums.phpfreaks.com/topic/315820-including-a-time-stamp-in-my-code/#findComment-1604879 Share on other sites More sharing options...
mac_gyver Posted January 21, 2023 Share Posted January 21, 2023 the error message confirms that the problem is as stated in the first paragraph of my reply. if you are getting an error about an - 18 hours ago, mac_gyver said: unquoted associative index name here wouldn't the solution be to make it into a quoted associative index name, e.g. by adding quotes around it? Quote Link to comment https://forums.phpfreaks.com/topic/315820-including-a-time-stamp-in-my-code/#findComment-1604880 Share on other sites More sharing options...
ginerjm Posted January 21, 2023 Share Posted January 21, 2023 Basicall what MacGyver is saying is: When you have a variable name such as $arr[item] you need to wrap the (item) in quotes to become $arr['item']. In this case 'item' is an index value for the array named $arr. Quote Link to comment https://forums.phpfreaks.com/topic/315820-including-a-time-stamp-in-my-code/#findComment-1604882 Share on other sites More sharing options...
Spudulene Posted January 22, 2023 Author Share Posted January 22, 2023 Here is line 97 $listnum=$_POST[laureate_list]; Quote Link to comment https://forums.phpfreaks.com/topic/315820-including-a-time-stamp-in-my-code/#findComment-1604907 Share on other sites More sharing options...
ginerjm Posted January 22, 2023 Share Posted January 22, 2023 By now you have probably fixed this error or should have. As I posted 22 hours ago you need to put quotes around your index values Quote Link to comment https://forums.phpfreaks.com/topic/315820-including-a-time-stamp-in-my-code/#findComment-1604909 Share on other sites More sharing options...
Spudulene Posted January 22, 2023 Author Share Posted January 22, 2023 I made the correction and removed code related to the"clickercode," which is not used any more, and got further this time. Below are the warnings and the associated code Warning: fopen(nobeloutput_f22-310.txt): Failed to open stream: Permission denied in <URL path>310nobel_f22-time.php on line 117 line 117: $fp=fopen($outfile,"w"); Fatal error: Uncaught TypeError: fputs(): Argument #1 ($stream) must be of type resource, bool given in <URL path>310nobel_f22-time.php:120 Stack trace: #0 <URL path>310nobel_f22-time.php(120): fputs() #1 {main} thrown in <URL path>310nobel_f22-time.php on line 120 line120: fputs($fp,$studentinfo[$i]); Quote Link to comment https://forums.phpfreaks.com/topic/315820-including-a-time-stamp-in-my-code/#findComment-1604910 Share on other sites More sharing options...
ginerjm Posted January 22, 2023 Share Posted January 22, 2023 You specified a filename that apparently doesn't exist. THEN you tried to write to it. Whenever you do something that relies on success you must check on the action to be sure it happened. You are not. The more correct way is: if (!$fp = fopen($filename, 'w')) { echo "Could not open file $filename"; exit(); } Now you will see a message if the file isn't there and exit. You can figure out how to handle it some other way if necessary. The point is that when you want to rely on php doing something 'external' such as looking for a file or an image or executing a query or making a db connection, etc. you MUST check the result. When you look something up in the manual to learn how to use it and you see that there is a result such as a boolean one, you have to write code to check that result before continuing on with your program. Quote Link to comment https://forums.phpfreaks.com/topic/315820-including-a-time-stamp-in-my-code/#findComment-1604911 Share on other sites More sharing options...
Spudulene Posted January 22, 2023 Author Share Posted January 22, 2023 Thank you for your feedback. I have never done any real coding from scratch in php. So, everything is a learning experience for me. I appreciate your help and patience. We are getting closer. I fixed permission issues that I believe were causing some of my problems. Including the error code is so helpful. I had zero chance to debug at all before. I do not get the error "Could not open file $filename," although I do have the code included (lines 117-121). The script is now reading and writing both $input and $output files, as it should. The page is redirecting as I want, but the emails are not being sent. This is my only error now is Warning: Undefined array key 132 in <URL path>310nobel_f22-time.php on line 134 line 134: fputs($fp,$names[$i]); Quote Link to comment https://forums.phpfreaks.com/topic/315820-including-a-time-stamp-in-my-code/#findComment-1604913 Share on other sites More sharing options...
Spudulene Posted January 22, 2023 Author Share Posted January 22, 2023 How does one explain this behavior? All of my scripts, I create a new one each semester, are now populating the drop down menu from the output file! It just stared on the file I was working on and I was shocked. So I checked some other files and got the same behavior. Different browsers. Quitting and reopening browser. clearing history. Even from different computers! Quote Link to comment https://forums.phpfreaks.com/topic/315820-including-a-time-stamp-in-my-code/#findComment-1604915 Share on other sites More sharing options...
Spudulene Posted January 22, 2023 Author Share Posted January 22, 2023 Okay. I solved that problem. The input file was written over. Behavior is now normal. All I have left is the error Warning: Undefined array key 132 in <URL path>310nobel_f22-time.php on line 134 line 134: fputs($fp,$names[$i]); Any help would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/315820-including-a-time-stamp-in-my-code/#findComment-1604919 Share on other sites More sharing options...
Barand Posted January 22, 2023 Share Posted January 22, 2023 This code $names = [ 'prancer', 'dancer', 'dasher', 'vixen', 'comet', 'cupid', 'donner', 'blitzen' ]; unset($names[5]); for ($i=0; $i<count($names); $i++) { echo $names[$i] . '<br>'; } gives the following output, and is similar to what you are doing prancer dancer dasher vixen comet Warning: Undefined array key 5 in C:\inetpub\wwwroot\apps\olumide\noname5.php on line 16 When you use a for loop like that it assumes all keys from 0 to 7 will be present, but '5' was unset and no longer exists. Use foreach () when looping through arrays. Quote Link to comment https://forums.phpfreaks.com/topic/315820-including-a-time-stamp-in-my-code/#findComment-1604920 Share on other sites More sharing options...
Spudulene Posted January 23, 2023 Author Share Posted January 23, 2023 Thank you for the feedback, Barand. I kind of get it. But integrating that into my code is beyond my skill set. I have no formal programming experience. I am simply looking at existing code and trying to figure it out from there. Let me see if I understand this. For your example, because there are only seven items left in the list (Cupid is unset), it is having difficulty with array key number 5 (Cupid) and cannot write any further. Is that at all correct. Quote Link to comment https://forums.phpfreaks.com/topic/315820-including-a-time-stamp-in-my-code/#findComment-1604921 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.