Jon
Jon

Reputation: 2584

Writing logs to a text file doesn't work

I have this code in /var/www/index.php:

<html>
<head>
<title>Chipperyman573</title>
<link rel="shortcut icon" href="/fav.ico" />
</head>
<body>
I honestly don't know what you expected.
</body>
</html>

<?php
//get visitors ip address
$ipaddress = $REMOTE_ADDR;
//get visit date
$vdate = date("m-d-y");
//UserAgent
$agent = getenv("HTTP_USER_AGENT");
//Set whole string
$str = $ipaddress.." visited on "..$vdate.." using user agent "..$agent..".";
//Set log name
$fi = "/logs/mainLog.txt";
file_put_contents($fi, $str);
?>

I have made a text file called mainLog.txt in /logs however after visiting chipperyman573.com in my browser nothing appears there. All files (the /index.php, /logs and /logs/mainLog.txt all have 777 file perms).

Why, and how do I fix this?

Upvotes: 0

Views: 776

Answers (3)

Machavity
Machavity

Reputation: 31614

Your problem is possibly here

$fi = "/logs/mainLog.txt";

In Linux, / is the root of the server. So it's looking in the root for your log file (which you probably don't have permissions to access). Instead, try making your path relative to your file, or include the full correct path

$fi = "./logs/mainLog.txt";

Upvotes: 1

elliotanderson
elliotanderson

Reputation: 452

When you are working in linux/UNIX based environments, a / at the beginning of a file path tells the computer to go from the root directory. Take the / out and it should work.

Upvotes: 0

Angus Walker
Angus Walker

Reputation: 398

Try changing

$fi = "/logs/mainLog.txt";

to

$fi = $_SERVER['DOCUMENT_ROOT']."/logs/mainLog.txt";

Upvotes: 0

Related Questions