/ /

XtScript Function: Cookies

Learn to use cookies feature in XtScript

What is Cookies? Cookies is a form of small data and informations stored in visitors browsers.

It's just like when you signing in to your Google account, Google then stored your login credentials in a cookie in your browsers. So the next time you access Google services like Gmail, you don't need to sign in anymore because Google found that you have been signed in before using certain account on the same browser and same device from the Cookie stored in your browser at previous sign in.

We can also use simple cookies function using XtScript. We can get our Xtgem site to be more attractive by make use of the XtScript Cookies function.

XtScript Cookies basic syntax:

<!--parser:xtscript-->
# Set a cookie
call cookie::set $name=...; $val=...; $expire=...; $force_current=...

# Get a cookie and store it into a variable
var $variable = call cookie::get $name=...; $default=...

# Delete a cookie
call cookie::delete $name=...; $force_current=...
<!--/parser:xtscript-->

Below is the explanation of the available arguments used in XtScript cookies basic syntax above:

ArgumentsValueMandatoriesExplanation
$nameAlphanumeric and underscores only.RequiredThe cookie name represent the data identity.
$valAlphanumeric and underscores only.RequiredThe cookie value or the data itself.
$expireNumbersRequiredThe period in seconds of when the cookie will be expired.
$defaultAlphanumeric and underscores only.OptionalSpecified a value to be returned if no called cookie found on visitor's browser.
$force_current1OptionalSpecified if the cookie's change should affect immediately. If this argument is not been set, then the cookie's change will affect after browser refresh/reload since the change has been made.

Learn XtScript Cookies from examples

Create a simple form to store visitor's name

<h1>XtScript Cookie Demo 1: Simple Visitor Identity Form</h1>
<!--parser:xtscript-->
	# Get the variable submitted in the form
	get name
	get age

	# Set the expire date to 1 day
	var $exp = (60*60*24)

	# Set the cookie if visitor submitted to the form
	if $name
		call cookie::set $name=test_name; $val=$name; $expire=$exp; $force_current=1
		call cookie::set $name=test_age; $val=$age; $expire=$exp; $force_current=1
	endif

	# Delete cookie if there is "delete" parameter in the url
	get delete
	if $delete
		call cookie::delete $name=test_name; $force_current=1
		call cookie::delete $name=test_age; $force_current=1
	endif	

	# Get the cookies value
	var $name = call cookie::get $name=test_name; $default=0
	var $age = call cookie::get $name=test_age; $default=0

	# Greet visitor if the cookie has been found in his browser
	if $name != 0
print <p>Hello <b>$name</b>. You are <b>$age</b> years old. Welcome back! Try to close this tab and revisit this page again, we will still remember your name and age from the cookie stored in your browser when you submitted to the form.</p>
	else
		print <p>Hello guest! This is your first visit to this page today. Please fill the form below!</p>
	endif
<!--/parser:xtscript-->

<p>
<form action="<xt:url type="path" />" method="post">
Name:<br/>
<input type="text" name="name" value="" />
<br/><br/>
Age:<br/>
<input type="number" name="age" value="" /><br/><br/>
<input type="submit" value="Submit" />
</form><br/><br/>
<a href="<xt:url type="path">?delete=1"><button>Delete my cookie identity</button></a>
</p>

To view live demo of this codes, you can visit The demo page.


XtGem Forum catalog