Jump to content

chubbychub (no, this thread isn't spam)


chubbychubus
Go to solution Solved by chubbychubus,

Recommended Posts

i got the below code from google, need to adapt it to use, it works, but i just need it to over write on the file instead of appending to the file , please help, i am not a coder nor understand much of this, if someone could change it for me please:

function logText($text)
{
    $ts = date("D d-m-y h:i A",time());
    $toLog = "[" . $ts . "] " . $text;
    $fh = fopen("logFile.txt", 'a');
    fwrite($fh, $toLog."\n");
    fclose($fh);
}
Edited by requinix
Link to comment
Share on other sites

Look here. Read and try to understand. It's a very small change.
But you really don't want to do that. It means only one message will ever be in the file at a time. The most recent message. Everything else got lost.

So forget that code. Where did it come from? Whoever wrote it doesn't know about PHP, and getting PHP code some someone who doesn't know much about it is a pretty bad idea.

What problem are you trying to solve?

In unrelated news, I edited your thread title to put a little more effort into it than you did. Please try to write something useful in there next time.

Link to comment
Share on other sites

  • requinix changed the title to chubbychub (no, this thread isn't spam)

yeah, i am just to get my home computers post 1 line, so it can read 1 message.  when an program starts, that way i know its running and what time it started.

also, i read on that link prior to posting, but those modes are either prepend, or append to the file, not sure how to overwrite it though

Edited by chubbychubus
Link to comment
Share on other sites

its just a server script (in ahk) that starts a server and it will just post to that message, but i am not sure how its all going to work out, and just going step by step (so i dont have the full details of how i want the things to be yet) i could also login via temviewer and see if the program started, but this way i can just see it on the logs, instead of loginto my home computer( sometimes my wife uses it and dont need to interrupt her while in the middle of work)

Link to comment
Share on other sites

So there's a small issue. If you just write when the server starts up, you can't actually know if it's still running. All the log really tells you is the last time it started - who knows if it shut down since then.

But you say this is a server? Like an actual server running something you can connect to? Wouldn't the easiest way to know if it's running be to simply try to connect to it?

Link to comment
Share on other sites

no, its not an actual server, its just a server script, to receive a from a client script (via sockets)... but that is not relevant to this part of the script, i dont think. when the server script runs, it will post this message to let me know that its started the last time, (as you said).. i dont really need to see logs, just need to know the last time it started. so instead of appending or prepending to the file, i dont need to see all those logs, just the single one line. 

Link to comment
Share on other sites

I was on the phone, be patient.

"w" should have done the job, but forget that code. The author doesn't know PHP.

file_put_contents("logFile.txt", date("r"));

That's all you need. If it's not creating the file then you're probably facing a file permissions issue; create the file yourself, change the permissions to 0666, and try running again.

Link to comment
Share on other sites

  • Solution

thank you requinix!, that helped, i ended up using the below code:



function logText($text)
{
    $ts = date("D d-m-y h:i A",time());
    $toLog = "[" . $ts . "] " . $text;
    file_put_contents( "logFile.txt",$toLog);
    //fwrite($fh, $toLog."\n");
    //fclose($fh);

this has time stamp in a formatt that i want to easily read.

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.