简体   繁体   中英

How to grab IP address from a form-mailer

I have the line of code below in my contact form with the intention of grabbing the IP address of my visitors via the form. It instead returns the default value.How do I get it to return the visitor's IP? My Potential clients us the form to contact me but spammers do too.

<input type=hidden name="env_report" value="REMOTE_HOST,REMOTE_ADDR">

What exactly do you expect this to do?

If you want the IP address then look at the client address in the HTTP request the form generates. If the form is dynamically creating an email on the client (ie not sending the form data back in an HTTP request) which you want to populate with some values then you'd need to set the values from the code which generates the HTML (javascript doesn't know about IP addresses) eg with PHP....

<input type=hidden name="env_report" value="<?php print $_SERVER['REMOTE_ADDR']; ?>">

Only it's trivial for someone to amend the contents of the email unless you also include some tamper detection....

<input type=hidden name="anti_tamper" value="<?php 
    print md5('s3cr3t' . $_SERVER['REMOTE_ADDR']); 
?>">

...and validate on receipt.

You cannot get it directly from HTML. The best way to get it using server side language, like PHP. In PHP use can use $_SERVER['REMOTE_ADDR'] to get the the Clients IP address. Here is a blog post that I had written a while ago on Getting real client IP address in PHP .

There is no reliable way to get IP address using JavaScript, but you can see this question on more details about it.

So you would want to check the IPaddress when the form was submitted to your server and then identify it there whether the submitted was a spammer or a regular user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM