-3

So i am trying to create a form that captures the users name (input) and the users IP (Hidden Input) and then writes it to a txt file. The IP should also display on the page showing the user the IP we are recording but the IP does not show up and the form does not write to the file but I can not figure out why.

Index.html:

<html>
<body>
<br />
<center>

<form action="writetotxt.php" method="POST">
    <input name="field1" type="text" />
    <input type="hidden" name="field2" value="<?php echo $_SERVER['REMOTE_ADDR']; ?>" />
    <input type="submit" name="submit" value="Save Data">
</form>
Your IP:<?php echo $_SERVER['REMOTE_ADDR']; ?> will be recorded!
</center>
<br />
</body>
</html>

and writetotxt.php:

<?php
if(isset($_POST['field1']) && isset($_POST['field2'])) {
    $data = $_POST['field1'] . '-' . $_POST['field2'] . "\n";
    $ret = file_put_contents('mydata.txt', $data, FILE_APPEND | LOCK_EX);
    if($ret === false) {
        die('There was an error writing this file');
    }
    else {
        echo "$ret bytes written to file";
    }
}
else {
   die('no post data to process');
}?>

any ideas?

Edit: Form at: http://tsukino.forgewareinc.com/index.php

Edit2: So the real reason the form would not work correctly was due to file ownership that has now been fixxed

BlackMage
  • 57
  • 8
  • firstly, did you instruct Apache to treat `.html` files as PHP? Second, if and when you do, you're going to get an undefined variable notice for it in `Index.html` - check to see that both folders and file have permissions to be written to. Error reporting will tell you. – Funk Forty Niner Aug 24 '15 at 02:08
  • error log contains nothing relevant to this, even tried clearing my error log and running the form again – BlackMage Aug 24 '15 at 02:12
  • 1
    you didn't answer the first *important* part of my question - `.html` by default do NOT parse PHP directives. Plus using `/tmp/` assumes you want to write outside your public domain and may not have permissions set for it to write to it. Full system path could be `/var/usr/htdocs/public/folder/` - so ask yourself why this isn't working. – Funk Forty Niner Aug 24 '15 at 02:13
  • No Apache is not set to treat .html as PHP and /tmp/ was a test i corrected the post to show the correct path – BlackMage Aug 24 '15 at 02:17
  • 1
    `@$REMOTE_ADDR` ? Don't invent new PHP syntax. – Raptor Aug 24 '15 at 02:33

1 Answers1

2

Seeing your page and HTML source containing unparsed PHP directives, you need to change the file extension of index.html to index.php or instruct Apache to treat HTML files as PHP.

This is the HTML source that was pulled from your link:

<html>
<body>
<br />
<center>

<form action="writetotxt.php" method="POST">
    <input name="field1" type="text" />
    <input type="hidden" name="field2" value="<?php echo @$REMOTE_ADDR; ?>" />
    <input type="submit" name="submit" value="Save Data">
</form>
Your IP:<?php echo @$REMOTE_ADDR; ?> will be recorded!
</center>
<br />
</body>
</html>

Notice the <?php - ?> and PHP directives in the source? Those wouldn't have shown had they been parsed properly. Had it been parsed as PHP, then you/I would have seen an IP address in the hidden field, and an undefined REMOTE_ADDR variable notice.

However, $REMOTE_ADDR is undefined.

Use a ternary operator:

For your hidden input:

value="<?php echo !empty($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : "EMPTY"; ?>"

or without a ternary operator:

value="<?php echo $_SERVER['REMOTE_ADDR']; ?>"

In your echo on the index file:

Your IP:<?php echo $remote=!empty($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : ""; ?> will be recorded!

Here is an article on Stack on how to treat HTML file as PHP:

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.

When you fix that index file, you're going to get undefined variables notices.

You need to check if they are set/not empty.

  • @ symbols are error suppressors.
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141