Writing to a text file with PHP

Discussion in 'PHP' started by Guthix121, Nov 23, 2009.

  1. #1
    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.
     
    Guthix121, Nov 23, 2009 IP
  2. MartiCode

    MartiCode Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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.
     
    MartiCode, Nov 23, 2009 IP
  3. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #3
    You never closed the if statement. And why would the IP be set?
     
    Pudge1, Nov 23, 2009 IP
  4. Guthix121

    Guthix121 Well-Known Member

    Messages:
    1,078
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    105
    #4
    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.
     
    Guthix121, Nov 23, 2009 IP
  5. ColdMoon

    ColdMoon Peon

    Messages:
    26
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    $_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:
     
    ColdMoon, Nov 23, 2009 IP
  6. new2seoo

    new2seoo Peon

    Messages:
    143
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Please Check the form you are submitting is using POST or GET method.
     
    new2seoo, Nov 23, 2009 IP
  7. Guthix121

    Guthix121 Well-Known Member

    Messages:
    1,078
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    105
    #7
    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.
     
    Guthix121, Nov 23, 2009 IP
  8. ColdMoon

    ColdMoon Peon

    Messages:
    26
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Then the problem is your server ( permissions ), not PHP.
     
    ColdMoon, Nov 23, 2009 IP
  9. Guthix121

    Guthix121 Well-Known Member

    Messages:
    1,078
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    105
    #9
    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.
     
    Guthix121, Nov 23, 2009 IP
  10. ColdMoon

    ColdMoon Peon

    Messages:
    26
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    So what if you replace a with w ( in the part which doesn't work ) ? According to what you said, it should work ..
     
    ColdMoon, Nov 23, 2009 IP
  11. Guthix121

    Guthix121 Well-Known Member

    Messages:
    1,078
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    105
    #11
    Doesn't work, and it's not what I want anyways.
     
    Guthix121, Nov 23, 2009 IP
  12. ColdMoon

    ColdMoon Peon

    Messages:
    26
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #12
    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 ) ?
     
    ColdMoon, Nov 23, 2009 IP