Skip to content

httpSetResponseCookie

Server-side
Caution

This function works only in the HTTP interface, meaning in .html files - it is not available in .lua files.

This function sets the value for the specified HTTP cookie of the current HTML page.

Syntax

bool httpSetResponseCookie ( ​string cookieName, ​string cookieValue )
Required arguments
  • cookieName: The HTTP cookie whose value is being set.
  • cookieValue: The new value for the specified cookie.

Returns

Returns true if the cookie value was set successfully, false otherwise.

  • bool: result

Code Examples

server

This example retrieves the player's loginTime element data, sets it as a cookie, and then displays it on the page when visiting the resource's web page.

<*
local timestamp = getElementData(getPlayerFromName(getAccountName(user)), 'loginTime') or ''
local date = os.date("%Y-%m-%d %H:%M:%S", timestamp)
httpSetResponseCookie("login_time", tostring(date))
*>
<!DOCTYPE html>
<html>
<head>
<title>Hello</title>
</head>
<body onLoad="getLoginTime();">
<p>Logged into the server: <span id='time_span'></span></p>
<script>
// getCookie function from w3schools.com
function getCookie(cname) {
let name = cname + "=";
let decodedCookie = decodeURIComponent(document.cookie);
let ca = decodedCookie.split(';');
for(let i = 0; i <ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function getLoginTime()
{
var time = document.getElementById('time_span');
var login_time = getCookie('login_time');
time.innerHTML = (!login_time || login_time == '') ? 'Unknown' : login_time;
}
</script>
</body>
</html>