php_freak
php_freak

Reputation: 16

DIfferent behaviour on Edge/Chrome and Firefox on save filename

I work on a project and i am facing a strange behaviour between edge/chrome and firefox. I'm trying to save a file with filename like file_[ip address]-[username].txt.

Sending data from html page through ajax ..

...
<form id="frmChat" action="#">
    <table>
      <tr>
        <td>User:</td>
        <td>
          <input class="input-data" type="text" id="txtUser" name="user" size="20" value="" />
        </td>
        <td rowspan="2">
            <input class="input-data-submit" type="submit" name="send" value="Send Value" />
        </td>
      </tr>
      <tr>
        <td>Message:</td>
        <td>
            <input  type="text" name="message" id="txtMessage" size="20" />
        </td>
      </tr>
    </table>
</form>
...  
var data = $(this).serialize();
$.ajax({
    url: 'xxx.php',
    dataType: 'text',
    data: data,
    type: 'post',
    ...
    }
  });
...

I get my data to my php file as follows:

...
$postVars = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
extract($postVars); //contains $user, $message and some more fields
...

Then i try to save the file like

...
$userip = $_SERVER['REMOTE_ADDR']."-".$user;
$msg = $userip."-".$message;

$fp=fsockopen($host,$port,$errstr,$errno,300)
fputs($fp,$msg);
fclose($fp);
...

So far so good eveything working as supposed to. But when i open the page from edge/chrome i get "null" as $user and $_SERVER['REMOTE_ADDR'] and when i open the page from firefox i get the $user and $_SERVER['REMOTE_ADDR'] correctly.

So the saved file created on edge/chrome looks like: file_-.txt

And from firefox is like: file_127.0.0.0-test.txt

What am i missing here?

Upvotes: 0

Views: 213

Answers (2)

php_freak
php_freak

Reputation: 16

Mystery solved. All the trouble caused by the way each of browsers handles the localhost ip.

  • The firefox returns the localhost ip:

127.0.0.1

  • The chrome/edge returns the localhost ip as:

::1

So in later time the sanitation "nulled" my variable, and caused the problem. Thank you all for your answers.

Upvotes: 0

sayou
sayou

Reputation: 913

Either the access provider uses a google proxy, or the visitor uses a google proxy, then you have to look in the variables $_SERVER['HTTP_X_FORWARDED_FOR'] or $_SERVER['HTTP_CLIENT_IP']

PS: these variables can be changed easily by the client, so you can use them but they should not be considered reliable.

Upvotes: 0

Related Questions