TIP: Saving the IP Address on a FormRender Form

Topics: General, Standard packages
Sep 22, 2011 at 6:29 AM

Thought I'd share this tip. Many times when people submit data, you may want to store their IP address.

This is one way of doing it. 

PART A

  1. Create an Empty Inline C# Function with no parameters (you could create an external one, but for simplicity's sake, we'll do it as internal)
  2. Name: GetIPAddress
  3. Namespace: MyNamespace.Utils
  4. Description: Get's User's IP Address

SOURCE TAB:

Copy the following into the source tab.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Composite.Data;
using Composite.Data.Types;
using System.Web;

namespace MyNamespace.Utils
{
	public static class InlineMethodFunction
	{
		public static string GetIPAddress()
		{
                  string ipaddress;
                  
                  // Look for a proxy address first
                  ipaddress = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                  
                  // If there is no proxy, get the standard remote address
                  if (String.IsNullOrWhiteSpace(ipaddress))
                    ipaddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; 
                  
                  return ipaddress;
		}
	}
}


 

ASSEMBLY REFERENCES:

Add System.Web

Preview and then Save

 

PART B

  1. Add a field to your datatype:
    • Name IPAddress
    • Label: Your IP Address
    • Description: Your IP Address
    • Field Type: String [32 character maximum]
  2. Advanced Tab
    1. Field default value: 
      1. Delete anything that's there
      2. Add New function
      3. Add MyNamespace.Utils.GetIPAddress

PART C (Optional)

If you don't want to show the IP Address to the user, then...

  1. HighLight DataType and Edit Form Markup
  2. Find <TextBox Label="Your IP Address" Help... 
    1. Delete it or comment it out. NOTE: It will also not show in the console, but will still exist in the database or XML Store.

NOTE: Once you edit the Form markup, you lose the ability to automatically add Form elements when you add new fields. So, make sure you do this step once you're pretty sure you have all the fields added and modified. Otherwise, you have to do it manually. If you want to return the form to default, no problem, Just delete the dynamic file in /APP_Data/Composite/DynamicTypeForms/Path/To/My/DataType.xml which is created when you Edit Form Markup.