I am having an issue with a PHP code. I want to set it so that when somebody submits a form, their IP address is written to a text file for later use. This works, but is not what I want $ip = $_SERVER['REMOTE_ADDR']; $fh = fopen("iplog.txt", "a"); fwrite($fh, $ip); fclose($fh); if (isset($_POST['submit'])) { Code (markup): This is what I want, but it doesn't work if (isset($_POST['submit'])) { $ip = $_SERVER['REMOTE_ADDR']; $fh = fopen("iplog.txt", "a"); fwrite($fh, $ip); fclose($fh); Code (markup): Please help. Thanks PS: Everything below this code snippet (there are other elements to the form DO work, it's just this code itself. In addition, iplog.txt is CHMOD 777, so it's not a permissions issue.
Have you simply tried: file_put_contents("iplog.txt", $_SERVER['REMOTE_ADDR'], FILE_APPEND); ? You might want to add a line break or other separator after each IP too. And make sure your script has write access to the directory you are writing to.
Just tried it, didn't work. Both iplog.txt and the directory it's in are CHMOD 777. I am only showing you a portion of my code.
$_POST['submit'] will not be set until you'll give your submit button a name. <form name="sample" action="index.php" method="POST"> <input type="submit" value="Login" name="login"> </form> HTML: if (isset($_POST['login'])) { echo "IP registered!"; $ip = $_SERVER['REMOTE_ADDR']; $fh = fopen("log.txt", "a"); fwrite($fh, $ip); fclose($fh); } else { echo "<i>submit</i> not received."; } PHP:
I would like to clarify that everything is set and works perfectly. The ONLY thing that seems to be having issues is the code snippet I gave you. It just simply doesn't want to work below the isset for some reason.
I have a code right below it: $count_my_page = ("hitcounter.txt"); $hits = file($count_my_page); $hits[0] ++; $fp = fopen($count_my_page , "w"); fputs($fp , "$hits[0]"); fclose($fp); Code (markup): It works perfectly.
So what if you replace a with w ( in the part which doesn't work ) ? According to what you said, it should work ..
Ridiculous. If it works in one place and doesn't in another, how can we help you ? Syntax is 100% valid - there are no errors or whatsoever. Can you access the server via SSH and show us the output of ls -l ( in the directory where your target file is ) ?