PHP fwrite() not writing to log file - php

as one of my first PHP projects I'm creating an IP logging script that logs a user's IP address. For some reason my fwrite() function doesn't seem to be writing to my logfile.
Can someone help me out?
<?php
// IP Logger Script
// By Sam Lev
// sam#levnet.us
$iplogfile = 'iplog.txt';
$ipaddress = $_SERVER['REMOTE_ADDR'];
$webpage = $_SERVER['SCRIPT_NAME'];
$timestamp = date('m/d/Y h:i:s');
$browser = $_SERVER['HTTP_USER_AGENT'];
$fp = fopen($iplogfile, 'a+');
chmod($iplogfile, 0777);
fwrite($fp, '['.$timestamp.']: '.$ipaddress.' '.$webpage.' '.$browser. "\r\n");
fclose($fp);
echo "IP ADDRESS: $ipaddress <br />\n";
echo "TIMESTAMP: $timestamp <br />\n";
echo "BROWSER: $browser <br />\n";
echo "Information logged to server. <br />\n";
?>
iplog.txt is still blank after running the script. Everything echos out fine.
Thanks

Shouldn't
$fp = fopen($file, 'a');
be
$fp = fopen($iplogfile, 'a');
? Because I don't see the definition of $file.

Your code checks out and it's a permissions issue.
Either manually chmod your file to 0777
or add chmod($iplogfile, 0777); after $fp = fopen($iplogfile, 'a');
chmod is a standard server command which is not exclusive to PHP.
http://en.wikipedia.org/wiki/Chmod

heres my code for logging if this helps but after adding code chmod the log files to 0777 or it wont work as adding the code doesn't make it work and will give php error hence why its not in the code btw you may have to create log files manually but easy enough this was for my site www.nzquakes.maori.nz thank you for the help
<!-- Below Code Logs ONLY User's IP Address & Time Stamp & Browser Info To http://www.example.com/logs/ip-address-mainsite.txt -->
<?php
$iplogfile = 'full path to your logs goes here eg http://www.example.com/logs/ip-address-mainsite.txt';
$ipaddress = $_SERVER['REMOTE_ADDR'];
//load the file
$file = file_get_contents($iplogfile);
//check to see if the ipaddress is already in the file
if ( ! preg_match("/$ipaddress/", $file )) {
//nope, log it!
$webpage = $_SERVER['SCRIPT_NAME'];
$timestamp = date('d/m/Y h:i:s');
$browser = $_SERVER['HTTP_USER_AGENT'];
$fp = fopen($iplogfile, 'a+');
fwrite($fp, '['.$timestamp.']: '.$ipaddress.' '.$browser. "\r\n");
fclose($fp);
}
?>

Related

Website visitors details

I have made a PHP code that can log visitor's IP addresses, port, date, browser name in a txt file. But it doesn't shows the latest visitors details at the top. So everytime I need to scroll down a lot for seeing the users details. Is there any way to show up the visitors details at the top of the log.txt file so I will not have to scroll down everytime? Here is the full PHP code:
<?php
$protocol = $_SERVER['SERVER_PROTOCOL'];
$ip = $_SERVER['REMOTE_ADDR'];
$port = $_SERVER['REMOTE_PORT'];
$agent = $_SERVER['HTTP_USER_AGENT'];
$ref = $_SERVER['HTTP_REFERER'];
$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
$dateTime = date('Y/m/d G:i:s');
$fh = fopen('log.txt', 'a');
fwrite($fh, 'IP Address: '."".$ip ."\n");
fwrite($fh, 'Hostname: '."".$hostname ."\n");
fwrite($fh, 'Port Number: '."".$port ."\n");
fwrite($fh, 'User Agent: '."".$agent ."\n");
fwrite($fh, 'HTTP Referer: '."".$ref ."\n");
fwrite($fh, 'Date: '."".$dateTime ."\n\n");
fclose($fh);
?>
The key here is the second parameter on the fopen function.
Look at http://php.net/manual/en/function.fopen.php
You are using:
$fh = fopen('log.txt', 'a');
a means ..
Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it. In this mode, fseek() has no effect, writes are always appended.
You can choose whichever option you want.
For example, r+ means:
Open for reading and writing; place the file pointer at the beginning of the file.
Hope this helps.
You can:
$toFile = IP;
$toFile .= file_get_contents('log.txt');
file_put_contents('log.txt', $toFile );
If you don't want to load the entire contents of the file into a variable, you can use PHP's Streams feature:

My php script won't open files that aren't in its root directory

This is a php script for a user login system that I am developing.
I need it to read from, and write to, the /students/students.txt file, but it won't even read the content already contained in the file.
<?php
//other code
echo "...";
setcookie("Student", $SID, time()+43200, "/");
fopen("/students/students.txt", "r");
$content = fread("/students/students.txt", filesize("/students/students.txt"));
echo $content;
fclose("/students/students.txt");
fopen("/students/students.txt", "w");
fwrite("/students/students.txt", $content."\n".$SID);
fclose("/students/students.txt");
//other code
?>
You are not using fopen() properly. The function returns a handle that you then use to read or edit the file, for example:
//reading a file
if ($handle = fopen("/students/students.txt", "r"))
{
echo "info obtained:<br>";
while (($buffer = fgets($handle))!==false)
{ echo $buffer;}
fclose($handle);
}
//writing/overwriting a file
if ($handle = fopen("/students/students.txt", "w"))
{
fwrite($handle, "hello/n");
fclose($handle);
}
Let me know if that worked for you.
P.S.: Ty to the commentators for the constructive feedback.
There are many ways to read/write to file as others have demonstrated. I just want to illustrate the mistake in your particular approach.
fread takes a file handle as param, NOT a string that represents the path to the file.
So your line:
$content = fread("/students/students.txt", filesize("/students/students.txt")); is incorrect.
It should be:
$file_handle = fopen("/students/students.txt", "r");
$content = fread($file_handle, filesize("/students/students.txt"));
Same thing when you write contents to file using fwrite. Its reference to the file is a File Handle opened using fopen NOT the filepath. when opening a file using fopen() you can also check if the $file_handle returned is a valid resource or is false. If false, it means the fopen operation was not successful.
So your code:
fopen("/students/students.txt", "w");
fwrite("/students/students.txt", $content."\n".$SID);
fclose("/students/students.txt");
Needs to be re-written as:
$file_handle = fopen("/students/students.txt", "w");
fwrite($file_handle, $content."\n".$SID);
fclose($file_handle);
You can see that fclose operates on file handles as well.
File Handle (as per php.net):
A file system pointer resource that is typically created using fopen().
Here are a couple of diagnostic functions that allow you to validate that a file exists and is readable. If it is a permission issue, it gives you the name of the user that needs permission.
function PrintMessage($text, $success = true)
{
print "$text";
if ($success)
print " [<font color=\"green\">Success</font>]<br />\n";
else
print(" [<font color=\"red\">Failure</font>]<br />\n");
}
function CheckReadable($filename)
{
if (realpath($filename) != "")
$filename = realpath($filename);
if (!file_exists($filename))
{
PrintMessage("'$filename' is missing or inaccessible by '" . get_current_user() . "'", false);
return false;
}
elseif (!is_readable($filename))
{
PrintMessage("'$filename' found but is not readable by '" . get_current_user() . "'", false);
return false;
}
else
PrintMessage("'$filename' found and is readable by '" . get_current_user() . "'", true);
return true;
}
I've re-written your code with (IMO) a cleaner and more efficient code:
<?php
$SID = "SOMETHING MYSTERIOUS";
setcookie("Student", $SID, time()+43200, "/");
$file = "/students/students.txt"; //is the full path correct?
$content = file_get_contents($file); //$content now contains /students/students.txt
$size = filesize($file); //do you still need this ?
echo $content;
file_put_contents($file, "\n".$SID, FILE_APPEND); //do you have write permissions ?
file_get_contents
file_get_contents() is the preferred way to read the contents of a
file into a string. It will use memory mapping techniques if supported
by your OS to enhance performance.
file_put_contents
This function is identical to calling fopen(), fwrite() and
fclose() successively to write data to a file. If filename does not
exist, the file is created. Otherwise, the existing file is
overwritten, unless the FILE_APPEND flag is set.
Notes:
Make sure the full path /students/students.txt is
correct.
Check if you've read/write permissions on /students/students.txt
Learn more about linux file/folder permissions or, if you don't access to the shell, how to change file or directory permissions via ftp
Try to do this:
fopen("students/students.txt", "r");
And check to permissions read the file.

PHP create a text file - difficulty in creating a text file

busy studying php with a book called "php and mysql web development 4th edition". i have trouble with the following code. i am trying to create a text file. i'm testing all code on a live server
i get the following errors:
Warning: fopen(/home/truevvky/public_html/../orders/orders.txt): failed to open stream: No such file or directory in /home/truevvky/public_html/test/processorder.php on line 60
Warning: flock() expects parameter 1 to be resource, boolean given in /home/truevvky/public_html/test/processorder.php on line 62.
the idea is to create a new text file
<?php
//create short variable names
$tireqty = $_POST['tireqty'];
$oilqty = $_POST['oilqty'];
$sparkqty = $_POST['sparkqty'];
$address = $_POST['address'];
$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
$date = date('H:i, jS F Y');
?>
<html>
<head>
<title>Bob's Auto Parts - Order Results</title>
</head>
<body>
<h1>Bob's Auto Parts</h1>
<h2>Order Results</h2>
<?php
echo "<p>Order processed at ".date('H:i, jS F Y')."</p>";
echo "<p>Your order is as follows: </p>";
$totalqty = 0;
$totalqty = $tireqty + $oilqty + $sparkqty;
echo "Items ordered: ".$totalqty."<br />";
if ($totalqty == 0) {
echo "You did not order anything on the previous page!<br />";
} else{
if ($tireqty > 0) {
echo $tireqty." tires<br />";
}
if ($oilqty > 0) {
echo $oilqty." bottles of oil<br />";
}
if ($sparkqty > 0) {
echo $sparkqty." spark plugs<br />";
}
}
$totalamount = 0.00;
define('TIREPRICE', 100);
define('OILPRICE', 10);
define('SPARKPRICE', 4);
$totalamount = $tireqty * TIREPRICE
+ $oilqty * OILPRICE
+ $sparkqty * SPARKPRICE;
$totalamount = number_format($totalamount, 2, '.',' ');
echo "<p>Total of order is $".$totalamount."</p>";
echo "<p>Address to ship to is ".$address."</p>";
$outputstring = $date."\t".$tireqty." tires \t".$oilqty." oil\t"
.$sparkqty." spark plugs\t\$".$totalamount."\t".$address."\n";
// open file for appending
# $fp = fopen("$DOCUMENT_ROOT/../orders/orders.txt", 'ab');
flock($fp, LOCK_EX);
if (!$fp) {
echo "<p><strong> Your order could not be processed at this time.
Please try again later.</strong></p></body></html>";
exit;
}
fwrite($fp, $outputstring, strlen($outputstring));
flock($fp, LOCK_UN);
fclose($fp);
?>
</body>
</html>
I'm working on the same book and found the solution to his problem. I know this is old and maybe he figured this out, but wanted to put my answer for anyone else who encounters this.
for some reason in the book he puts flock before we even know if the file exist that's what is causing the error. flock() just puts a lock so that we can write LOCK_EX and fwrite() then writes whatever we output and LOCK_UN releases the lock.
for $DOCUMENT_ROOT mine with WAMP looked like this "C:/wamp/book/orders/orders.txt" so in the code remove the "..".
// open file for appending
# $fp = fopen("$DOCUMENT_ROOT/orders/orders.txt", 'ab');
if (!$fp) {
echo "<p><strong> Your order could not be processed at this time.
Please try again later.</strong></p></body></html>";
exit;
}
//you move flock down here
flock($fp, LOCK_EX);
fwrite($fp, $outputstring, strlen($outputstring));
flock($fp, LOCK_UN);
fclose($fp);
Opening file for appending works with file that exists. I would personally do something like
$path = "$DOCUMENT_ROOT/../orders/orders.txt";
$content = "Okay here are my contents";
$fp = null;
if(file_exists($path))
{
$fp = fopen($path, 'ab');
}
else
{
$fp = fopen("myText.txt","wb");
}
fwrite($fp,$content);
fclose($fp);
Maybe you did not create "orders" folder in "$DOCUMENT_ROOT/../" path. You should understand fopen('filepath', 'ab') just can create the specific file. If the path was not complete (can't find the "orders" folder), it won't work. So you can make the folder manually first, then test the .php
echo $DOCUMENT_ROOT
you could see your server's root address, mine is D:/AppServ/www.
If you use "$DOCUMENT_ROOT/../orders/orders.txt" you will get address: D:/AppServ/orders/orders.txt. Notice that you should be sure that you have a file folder named orders. So we can see that .. is to mean the parent directory of the document root directory, the parent directory is D:/AppServ.
Please make sure that the web server user can write a file on '/home/truevvky/public_html/../orders/' directory.
The fopen statement should be:
$fp = fopen("$DOCUMENT_ROOT/../orders/orders.txt", 'w');
Your way assumes the file exists.
By the way the 'b' you included in the second argument means you want to write the data in binary. If that's the case then fopen should be:
$fp = fopen("$DOCUMENT_ROOT/../orders/orders.txt", 'wb');

fwrite() Isn't creating nor writing to a file that I specify it to

"LoggedIn" does = true and "CurrentUser" isn't "ManselD", yet it's still not writing or creating the file :(
And yes my /accounts/logs folder is CHMOD 702
if(!$user == "ManselD"){
ini_set('date.timezone', 'Europe/London');
$ip = $_SERVER['REMOTE_ADDR'];
$txt = "$user Logged In With The Ip: $ip At ".date("h:i")."\n";
$url = "/accounts/logs/".trim(date('F')."-".date('d')."-".date('Y').".txt");
$file = fopen($url, "a");
fwrite($file, $txt);
fclose($file);
echo $file;
if(is_writable("/accounts/logs")){
echo "It is writable";
} else{
echo "It isn't writable";
}
}
I'm literally stumped and baffled at why this doesn't work :S
FIXED VERSION:
if($user != "ManselD"){
ini_set('date.timezone', 'Europe/London');
$ip = $_SERVER['REMOTE_ADDR'];
$txt = "$user Logged In With The Ip: $ip At ".date("h:i")."\n";
$url = getcwd() . '/accounts/logs/'.trim(date('F')."-".date('d')."-".date('Y').".txt");
file_put_contents($url, $txt, FILE_APPEND);
}
I think the problem (that you are asking about, because there are more if you read the comments) is that you write
$url = "/accounts/logs/".trim(date('F')."-".date('d')."-".date('Y').".txt");
The file paths are not URLs. And while the above is obviously not a URL, it should probably not have the starting slash (/).
A good approach would be to always use absolute paths, possibly using the __FILE__ constant.

PHP: Download a file from web to local machine

I have searched the web for 2 days and can not find the answer.
I am trying to create a routine which displays the files on a site I control, and allows the user to download a selected file to a local drive.
I am using the code below. When I uncomment the echo statements, it displays the correct source and destination directories, the correct file size and the echo after the fclose displays TRUE.
When I echo the source file ($data), it displays the correct content.
The $FileName variable contains the correct filename, which is either .doc/.docx or .pdf. I have tested both and neither saves anything into the destination directory, or anywhere else on my machine.
The source path ($path) is behind a login, but I am already logged in.
Any thoughts on why this is failing to write the file?
Thanks,
Hank
Code:
$path = "https://.../Reports/ReportDetails/$FileName";
/* echo "Downloading: $path"; */
$data = file_get_contents($path); /* echo "$data"; */
$dest = "C:\MyScans\\".$FileName; /* echo "<br />$dest"; */
$fp = fopen($dest,'wb');
if ( $fp === FALSE ) echo "<br />Error in fopen";
$result = fwrite($fp,$data);
if ( $result === FALSE ) echo "<br />Can not write to $dest";
/* else echo "<br />$result bytes written"; */
$result = fclose($fp); /* echo "<br />Close: $result"; */
I think (!) that you're a bit confused.
You mentioned
allows the user to download a selected file to a local drive.
But the path "C:\MyScans\\".$FileName is is the path on the webserver, not the path on the user's own computer.
After you do whatever to retrieve the desired file from the remote website:
Create a file from it and redirect the user to the file by using header('Location: /path/to/file.txt');
Insert the following header:
header('Content-disposition: attachment; filename=path/to/file.txt');
It forces the user to download the file. And that's probably what you want to do
Note: I have used the extension txt, but you can use any extension
you can use php Curl:
<?php
$url = 'http://www.example.com/a-large-file.zip';
$path = '/path/to/a-large-file.zip';
$fp = fopen($path, 'w');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);
$data = curl_exec($ch);
curl_close($ch);
fclose($fp);

Categories