1

guys! I'm setting a cookie after my Gravity Form is submitted. The name is based on the current page, so each page that has this form will have its own cookie. Some pages with urls ending such as /324234-2/ will not set the cookie and return a error message (Warning: Cookie names cannot contain any of the following '=,; \t\r\n\013\014), while others with more regular names will. I'm trying to sanitize this cookie names so that I don't get any errors at all.

This is my function:

add_action( 'gform_after_submission_6', 'contentCookie', 10, 2);
function contentCookie($entry, $form) {
    $from_page = rgar( $entry, '6' );
    setcookie( 'unrestrict_'.$from_page, 1, strtotime( '+30 days' ), COOKIEPATH, COOKIE_DOMAIN, false, false);
};

How do I sanitize my cookie name? Thank you!

2

Assuming $from_page is a string value and not an array or object, sanitize_key() should do the trick, it allows only a-z0-9_- and I believe is used for permalink.

$cookiename = sanitize_key( 'unrestrict_'.$from_page );
setcookie( cookiename, 1 ...

there's a whole bunch of wordpress sanitizing functions, reference available in the docs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.