Skip Navigation

[Resolved] Save user IP address when form is submitted

This thread is resolved. Here is a description of the problem and solution.

Problem:

I am trying to save user IP address while he submits form.

Solution:

It needs some custom codes, see the solution here:
https://toolset.com/forums/topic/save-user-ip-address-when-form-is-submitted/#post-922304

Relevant Documentation:

http://php.net/manual/en/reserved.variables.server.php

https://developer.wordpress.org/reference/functions/update_post_meta/

This support ticket is created 6 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 2 replies, has 2 voices.

Last updated by ivicaV 6 years, 7 months ago.

Assisted by: Luo Yang.

Author
Posts
#922182

Tell us what you are trying to do?

I am trying to save user IP address while he submits form.

Added shortcode in functions.php

add_shortcode('get_client_ip', 'get_client_ip');

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;
}

and it works, my IP address is noted. Then created generic field:

[cred_generic_field field='ip-address' type='hidden' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":"[get_client_ip]"
}
[/cred_generic_field]

ip-address is slug for custom field created by toolset and after that in same php file I aded this function:

add_action('cred_save_data', 'save_ip_address',10,2);
function save_ip_address($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==236)
{
if (isset($_POST['ip-address']))
{
// add it to saved post meta
add_post_meta($post_id, 'wpcf-ip-address', $_POST['ip-address'], true);
}
}
}

ID for CRED form is 236 and slug for custom field id is wpcf-ip-address. What I failed to understand is if I am correct with ip-address parameter here $_POST['ip-address']

Is there any documentation that you are following?

Saw this:
https://toolset.com/forums/topic/ip-address-with-registration-from/

Is there a similar example that we can see?

I don't know

What is the link to your site?

hidden link

#922304

Hello,

In your case, the [cred_generic_field] is not needed, I suggest you remove the [cred_generic_field], and modify your PHP codes as below:

add_action('cred_save_data', 'save_ip_address',10,2);
function save_ip_address($post_id, $form_data)
{
// if a specific form
	if ($form_data['id']==236)
	{
		$ip = $_SERVER['REMOTE_ADDR'];
		//The IP address from which the user is viewing the current page. 

		update_post_meta($post_id, 'wpcf-ip-address', $ip);
		//Update post meta field
	}
}

More help:
hidden link
https://developer.wordpress.org/reference/functions/update_post_meta/

#922342

It works. If I want multiple forms to be included I just need to modify if statement. Thanks a lot.