Hi
I am trying to store the IP address of a user when they register, I followed this article here:
https://toolset.com/forums/topic/ip-address-with-registration-from/
But it didn't work any ideas why this no longer works?
Thanks
Hi, that ticket includes information about getting the IP using a custom shortcode and placing it in a generic hidden field in the Form. However, it does not include a code sample showing how to save that generic field into some custom field in the database using the cred_save_data hook. If the problem is the IP is not included as the value the hidden field (you would be able to tell by inspecting the generic hidden field value in the browser elements console when you load the page containing the Form), that indicates a problem in the shortcode that determines the IP. If the problem is the IP is included in the generic field value, but not stored in the database, that indicates a problem in the cred_save_data hook.
Can you tell me if the problem is the IP is not visible in the generic field's value attribute, or is the problem that the IP is visible in the generic field but not stored in the database anywhere? If it's the latter, please include your cred_save_data code here for me to review.
Oh right I see, I was just trying to use a Toolset custom field to save it straight to this.
function get_client_ip() {
$ipaddress = '';
if (isset($_SERVER['HTTP_CLIENT_IP']))
$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
else if(isset($_SERVER['HTTP_X_FORWARDED']))
$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
else if(isset($_SERVER['HTTP_FORWARDED']))
$ipaddress = $_SERVER['HTTP_FORWARDED'];
else if(isset($_SERVER['REMOTE_ADDR']))
$ipaddress = $_SERVER['REMOTE_ADDR'];
else
$ipaddress = 'UNKNOWN';
return $ipaddress;
}
I setup this for a shortcode and enabled in Front end settings.
Based on what you said I thought ah OK I need to insert the custom field instead of using a generic field. So I placed the following instead:
[cred_field field='ip-address' force_type='field' class='form-control' output='bootstrap' value='[get_client_ip]']
However this still didn't work?
Thanks.
What do you see if you place the shortcode outside of the field, for testing purposes? Also, it's always a good idea to close a custom shortcode explicitly, like so:
Client IP shortcode: [get_client_ip][/get_client_ip]<br />
[cred_field field='ip-address' force_type='field' class='form-control' output='bootstrap' value='[get_client_ip][/get_client_ip]']
Is the IP written out before the field correctly on the front-end of the site? If not, check your shortcode PHP. The snippet you shared (https://toolset.com/forums/topic/storing-ip-address/#post-1861689) includes the shortcode function, but not the line that actually initializes the shortcode using add_shortcode. There should be something like this in your code:
add_shortcode( 'get_client_ip', 'get_client_ip' );