I cant figure out why it wont write the client IP address to the file, everything else works.
<?php
$myFile = "ips.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
fwrite($fh, $_SERVER['REMOTE_HOST']);
fclose($fh);
echo $_SERVER['REMOTE_ADDR'];
?>
Thanks in advance.
Maybe fwrite($fh, $_SERVER['REMOTE_ADDR']); ?
Why do you think that if echo $_SERVER['REMOTE_ADDR']; works,
fwrite($fh, $_SERVER['REMOTE_HOST']); should too?
Try echo $_SERVER['REMOTE_HOST']; first and see if it outputs something.
My guess: just change _HOST to _ADDR in fwrite as I said in the beginning.
Related
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:
Hi I need to receive a post data which will be in an xml that is encoded in a base64 format.
I'll be receiving this from a payment gateway. Now all i get is this. My code creates a txt file but is empty. Is there anything wrong with the code? The output should be an xml envelope in a text file.
$body = '';
$fh = #fopen('php://input', 'r');
if ($fh)
{
while (!feof($fh))
{
$s = fread($fh, 1024);
echo $s;
if (is_string($s))
{
$body .= $s;
}
}
fclose($fh);
}
$body = base64_decode($body);
$ourFileName = "testFile.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $body;
fwrite($fh, $stringData);
fclose($fh);
I tried to contact the payment gateway and they are telling me that they are getting this error "The remote server returned an error: (417) Expectation failed." where could the problem exist us or them?
Since your file is returning blank, I would recommend verifying the specifications from the payment gateway for your fopen() function. In addition, if you are properly getting data back from them, then I would check the base64_decode() function. I have seen situations where there may be a header or other data at the top of the actual payload data that fouls up the base64_decode and ruins your day.
It looks like you are mixing up your file handlers, can you see if the following codes run:
$body = $HTTP_RAW_POST_DATA;
$ourFileName = "testFile.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
$stringData = $body;
fwrite($ourFileHandle, $stringData);
fclose($ourFileHandle);
I have just tested the above code myself successfully.
What I suggest you do is trying the following:
Try to put a static piece of data into $body (eg: "$body = 'test';")- and see if that saves - if not then it is an issue at your end.
Delete the file (to remove any permission issues).
Double check the url that the payment gateway is sending to is correct.
I have a little issue while trying to kill time. I am writint to a text file the contents of the filled in form so it is easy to read. At the moment everything gets put int a single line, and therfore I can not tell where msgs begin or end. I wrote a php like this:
$from = $_POST[from];
$friend = $_POST[friend];
$carrier = $_POST[carrier];
$message = stripslashes($_POST[message]);
if ((empty($from)) || (empty($friend)) || (empty($message))) {
header ("Location: sms_error.php");
}
else if ($carrier == "orange_mobile_network") {
$formatted_number = $friend."#orange_mobile_network.co.uk";
mail("$formatted_number", "SMS", "$message");
header ("Location: sms_success.php");
}
Then the SMS/Text message will be sent. After this I wanted to write/store/append the message on a txt file. So I wrote:
$myFile = "sms_Numbers_Mgs.txt";
$fh = fopen($myFile, 'a+') or die("can't open file");
$stringData = $_POST[from];
fwrite($fh, $stringData);
$stringData = $_POST[friend];
fwrite($fh, $stringData);
$stringData = $_POST[message];
fwrite($fh, $stringData);
fclose($fh);
But like I said. Everything works. I just want to be able to read the file and put everything in a new line and possibly format it nicely for easy reading. I do not want to use a DB for storing the TXT as my host is going to charge me.
change this:
fwrite($fh, $stringData);
with this:
fwrite($fh, $stringData . PHP_EOL);
or:
fwrite($fh, $stringData . "\r\n");
You can append PHP_EOL when you write for a platform aware newline. I.e. $stringData = <string> . PHP_EOL;
If don't need it to be platform aware, going with the windows newline is a safe option.
define('CRLF', "\r\n");
$stringData = <string> . CRLF;
fwrite($fh, $stringData);
I do not know exactly Cause you're using. txt instead of a bank, but my tip for this specific case is to work with json format.
Example:
$myFile = "sms_Numbers_Mgs.txt";
$fh = fopen($myFile, 'a+') or die("can't open file");
//assembles a string of type json
$data = json_encode(array(
'from' => $_POST[from],
'friend' => $_POST[friend],
'message' => $_POST[message]
));
//Write in the file
fwrite($fh, $data);
fclose($fh);
//reads the file
$content = file_get_contents($myFile);
$object = json_decode($content);
var_dump($object);
good it is at least better organized.
a hug!
Does the php's echo wait to send data to the screen, until the rest of the script is complete?
So if I have:
<?
echo "Hello World";
$xmlData = file_get_cotents("www.webpage.com");
$myFile = "data_backup.xml";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $xmlData;
fwrite($fh, $stringData);
fclose($fh);
?>
So does the above sample what to show Hello World until the contents from the url are retrived and saved locally?
Yep. unless you flush the data with flush();
All of the scripting in PHP occurs before the page loads, so the answer to your question is: yes, it would wait until the file is retrieved and saved before showing "Hello World".
I have a PHP generated page containing the results of a submitted form, what I would like to do is save this as a .doc file on the server.
After some googling I came across this code which I adapted:-
$myFile = "./dump/".$companyName."/testFile.doc";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Bobby Bopper\n";
fwrite($fh, $stringData);
$stringData = "Tracy Tanner\n";
fwrite($fh, $stringData);
fclose($fh);
But the problem with this is that I would have to recreate the results in order to manually write them to the file and it doesn't seem very efficient.
So I continued to google and found the PHP manual which left me scratching my head frankly, however I eventually found this:-
ob_start();
// code to generate page.
$out = ob_get_contents();
ob_end_clean();
// or write it to a file.
file_put_contents("./dump/".$companyName."/testFile.doc",$out);
Which will create the file, but doesn't write anything to it. However this seems to be the way to do what (Based on the PHP manual) I want even if I can't get it to work!
Any advice? I don't mind googling if I can figure out a decent search term :)
This sould do it for you:
$cache = 'path/to/your/file';
ob_start();
// your content goes here...
echo "hello !"; // would put hello into your file
$page = ob_get_contents();
ob_end_clean();
$fd = fopen("$cache", "w");
if ($fd) {
fwrite($fd,$page);
fclose($fd);
}
It's also a great way to cache dynamic pages. Hope it helps.