Create a template such as site/save_preference
with the following code - adjust the regex to suit your needs:
{exp:switchee variable="{segment_3}" parse="inward"}
{!-- allow an alphanumeric name (and - or _) for the preference key only --}
{case value="#^[a-zA-Z0-9-_]+$#"}
{!-- allow a numeric value only --}
{exp:stash:set name="{segment_3}" type="snippet" save="yes" scope="user" match="#^[0-9]+$#"}{segment_4}{/exp:stash:set}
{/case}
{/exp:switchee}