Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
  • Cookies are quite useful for certain things, but it is not always clear on how to set or use Cookies. In the documentation there is a short section on how to use the IPS JS Utilities when working with cookies:

    ips.utils.cookie.set( 'myKey', 'Hello world', true ); // Sets a sticky cookie
    ips.utils.cookie.get( 'myKey' );
    // -> Hello world
    ips.utils.cookie.unset( 'myKey' );
    ips.utils.cookie.get( 'myKey' );
    // -> undefined

    You can also use a similar way to work with Cookies in PHP using the following format:

    {{\IPS\Request::i()->setCookie( 'name', 'value' );}}

    This will result in an HTTP only cookie, but if you extend this a bit you can get something useful to fetch in your js calls as well.

    \IPS\Request::i()->setCookie('cookie_name', 'value', NULL, TRUE));

    Null refer to how long the cookie will remain and by defult NULL means that it will be alive for as long as the session is alive. You can add a time object there to make the cookie persist for a certain amount.

    \IPS\Request::i()->setCookie('cookie_name', 'value', \IPS\DateTime::create()->add(new \DateInterval('P7D')));

    In this example we add a 7day persistence to the cookie. You can also do this in JavaScript of course:
     

    var date = new Date();
    date.setDate(date.getDate() + 7);
    ips.utils.cookie.set('cookie_name', 'value', date.toUTCString());

    Here are some more information on the cookie classes in IPS:

     

    In PHP: \IPS\Request::i()->setCookie();

    	/**
    	 * Set a cookie
    	 *
    	 * @param	string				$name		Name
    	 * @param	mixed				$value		Value
    	 * @param	\IPS\DateTime|null	$expire		Expiration date, or NULL for on session end
    	 * @param	bool				$httpOnly	When TRUE the cookie will be made accessible only through the HTTP protocol
    	 * @param	string|null			$domain		Domain to set to. If NULL, will be detected automatically.
    	 * @param	string|null			$path		Path to set to. If NULL, will be detected automatically.
    	 * @return	bool
    	 */
    	public function setCookie( $name, $value, $expire=NULL, $httpOnly=TRUE, $domain=NULL, $path=NULL )

     

    In JavaScript: ips.utils.cookie.set()

    		/**
    		 * Set a cookie value
    		 *
    		 * @param	{string} 	cookieKey 	Key to set
    		 * @param 	{mixed} 	value 		Value to set in this cookie
    		 * @param 	{boolean} 	sticky 		Whether to make this a long-lasting cookie
    		 * @returns {void}
    		 */
    		set = function( cookieKey, value, sticky )

     


    User Feedback

    Recommended Comments

    There are no comments to display.



    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

×
×
  • Create New...