Hi I am using fopen php function to maintain log. I can write to the file within my own computer. But how to make PHP able to write file over the local network. Other computer in the network access my computer PHP application through IP but fails with permission when writing to the log file. My code is:
function hotellog($txt) {
date_default_timezone_set("Asia/Kathmandu");
$newfile = '\\localhost\gandaki/wp-content/hotel_log.json';
if (file_exists($newfile)) {
$myfile = fopen($newfile, "a") or die("Unable to open file!");
$txt = date("Y-m-d h:i:s") . "\r\t\t" . $txt . "\n\n\n";
fwrite($myfile, $txt);
fclose($myfile);
} else {
$myfile = fopen($newfile, 'w') or die("Can't create file");
$txt = date("Y-m-d h:i:s") . "\r\t\t" . $txt . "\n\n\n";
fwrite($myfile, $txt);
fclose($myfile);
}
}
Other computers over networks get the error as 'permission denied'.
PHP can not (well, should not) be able to write files over the network onto your computer under normal circumstances. You would be better off writing the log file on your local computer and then scripting a file transfer via sftp, or something similar, from your local computer to the remote one.
Related
I want to make a page like
www.example.com/redirect.php
above page must redirect to another page like
www.google.com
But whenever I open www.example.com/redirect.php?changeurl=www.yahoo.com
from now onwards www.example.com/redirect.php
page should start redirecting to yahoo.com
if changeurl=youtube.com
from now onwards
the page www.example.com/redirect.php must star redirecting to youtube.com
How can I do that without using SQL database, only by using a single file "redirect.php"
Please ask if you need more information about this question
First off you should at least have some code already written.
But try using this:
redirect.php:
<?php
if(!file_exists("redirect_url.txt")) {
$createfile= fopen("redirect_url.txt", "w") or die("Unable to open file!");
$txt = "https://www.google.com";
fwrite($createfile, $txt);
fclose($createfile);
}
if(isset($_GET['changeurl'])) {
$editfile= fopen("redirect_url.txt", "w") or die("Unable to open file!");
$txt = $_GET['changeurl'];
fwrite($editfile, $txt);
fclose($editfile);
header("Location: " . $_GET['changeurl']);
} else {
$myfile = fopen("redirect_url.txt", "r") or die("Unable to open file!");
$url = fgets($myfile);
fclose($myfile);
header("Location: " . $url);
}
?>
Also make sure in the ?changeurl You always use http:// or https://, Since you are not using a database you can use a .txt file.
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:
I'm creating a tool like Pastebin for train myself in PHP (I'm a beginner yet).
Everyone without log in/sign up can create .txt file and share it easily.
Anyway, I'm facing a problem:
How can a guest edit the .txt file when he created it?
<?php
$titleFile = $_POST['title'];
$textFile = $_POST['text'];
$newFile = fopen($titleFile . ".txt", "w") or die("Unable to open file!");
fwrite($newFile, $textFile);
$url = $titleFile . ".txt";
header("Location: $url");
?>
http://mattewdev.altervista.org/
I have a PHP-Code like:
$timestamp = time();
echo time();
echo "<br>";
$datenbank = "timestamp.txt";
$datei = fopen($datenbank, "w") or die("Unable to open file!");
fwrite($datei, $timestamp) or die("Unable to write file!");
but I forget fclose(). Now, I have no more memory on the server. Could this be the reason?
Unlikely. It is not good practice to leave files open like that, but PHP will close the file when your script ends.
I have a PHP script that looks like:
if(file_exists("temp.txt")){
$myfile = fopen("temp.txt", "a") or die("Unable to open file!");
}
else{
$myfile = fopen("temp.txt", "w") or die("Unable to open file!");
}
date_default_timezone_set("America/New_York");
$date = date('m/d/Y h:i:s a', time());
$text = $date . PHP_EOL;
fwrite($myfile, $text);
fclose($myfile);
When I run the above script in Powershell using
php myscript.php
A text file is created and written into.
If I try to run the same file with
Start-Job ScriptBlock {php myscript.php}
I'll get a response like
But my text file is never written into. It seems like Start-Job never starts my PHP script.
How do I get Start-Job to start running PHP scripts?
Ok I'll write an answer about all of this:
From test (with your script as origin) I added some echo to get something in Receive-Job in powershell.
It was obvious the script was running as intended, but not in the expected directory.
So I added a getcwd() within an echo and this show me the working dir for the job (or scriptblock) is my documents directory) and I found the file there wihtin my home\documents.
Here the final script I eneded with:
echo "I'm running in ".getcwd()." !\n";
if(file_exists("temp.txt")){
$myfile = fopen("temp.txt", "a") or die("Unable to open file!");
}
else{
$myfile = fopen("temp.txt", "w") or die("Unable to open file!");
}
echo "Ok, file was opened: \n".print_r(fstat($myfile),1)."\n";
date_default_timezone_set("America/New_York");
$date = date('m/d/Y h:i:s a', time());
$text = $date . PHP_EOL;
$r=fwrite($myfile, $text);
echo "Write in file returned $r \n";
fclose($myfile);
echo "File closed\n";
And that is the output is with Powershell:
Start-Job -ScriptBlock { D:\wamp\bin\php\php5.4.3\php.exe D:\wamp\bin\php\php5.4.3\test.php }
Id Name State HasMoreData Location Command
-- ---- ----- ----------- -------- -------
51 Job51 Running True localhost D:\wamp\bin\php\php5....
And retrieving the output:
Receive-Job 51
I'm running in C:\Users\my_name\Documents !
Ok, file was opened:
Write in file returned 24
File closed
Hope it will help.