How to Retrieve User IP Addresses for CAPTCHA Verification in App Engine Golang
Introduction:
Integrating reCAPTCHA into web applications is essential for spam and fraud protection. To verify CAPTCHA solutions, obtaining the user's IP address is crucial. This article demonstrates how to fetch the IP address from form posts in Google App Engine (GAE) using Golang.
Retrieve User IP Address:
To obtain the user's IP address from a form post in GAE Golang, follow these steps:
The net.SplitHostPort function parses the client's network address, extracting the IP address (ip).
Code Example:
<code class="go">import "net" func GetUserIP(r *http.Request) string { ip, _, _ := net.SplitHostPort(r.RemoteAddr) return ip }</code>
Usage:
Once you have retrieved the IP address, you can use it to verify reCAPTCHA solutions as follows:
<code class="go">challenge := r.FormValue("g-recaptcha-response") ip := GetUserIP(r) resp, err := http.Get("https://www.google.com/recaptcha/api/siteverify?secret=" + recaptchaSecret + "&response=" + challenge + "&remoteip=" + ip) // Validate reCAPTCHA response using retrieved IP and challenge</code>
The above is the detailed content of How to Retrieve User IP Addresses for reCAPTCHA Verification in App Engine Golang?. For more information, please follow other related articles on the PHP Chinese website!