Jump to content

Including a time stamp in my code


Spudulene

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by Spudulene
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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]);

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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]);

 

Link to comment
Share on other sites

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!

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.