---
warning: This file is auto generated by `yarn web:inject`, do not edit by hand
examples:
- setcookie('author_name', 'Kevin van Zonneveld')
returns:
- 'true'
dependencies:
- php/network/setrawcookie
authors:
original by:
- Jonas Raoni Soares Silva (https://www.jsfromhell.com)
improved by:
- Kevin van Zonneveld (https://kvz.io)
bugfixed by:
- Andreas
- Onno Marsman (https://twitter.com/onnomarsman)
notes: []
parityVerified:
- PHP 8.3
isTypescript: true
type: function
layout: function
title: PHP's setcookie in TypeScript
description: >-
Here's what our current TypeScript equivalent to PHP's setcookie
looks like.
function: setcookie
category: network
language: php
permalink: php/network/setcookie/
alias:
- /functions/php/setcookie/
- /functions/network/setcookie/
- /php/setcookie/
- /functions/setcookie/
---
{% codeblock lang:typescript %}import { setrawcookie } from '../network/setrawcookie.ts'
export function setcookie(
name: string,
value: string,
expires?: string | number | Date | null,
path?: string | null,
domain?: string | null,
secure?: boolean,
): boolean {
// discuss at: https://locutus.io/php/setcookie/
// parity verified: PHP 8.3
// original by: Jonas Raoni Soares Silva (https://www.jsfromhell.com)
// bugfixed by: Andreas
// bugfixed by: Onno Marsman (https://twitter.com/onnomarsman)
// improved by: Kevin van Zonneveld (https://kvz.io)
// example 1: setcookie('author_name', 'Kevin van Zonneveld')
// returns 1: true
return setrawcookie(name, encodeURIComponent(value), expires, path, domain, secure)
}
{% endcodeblock %}
{% codeblock lang:javascript %}import { setrawcookie } from '../network/setrawcookie.ts'
export function setcookie(name, value, expires, path, domain, secure) {
// discuss at: https://locutus.io/php/setcookie/
// parity verified: PHP 8.3
// original by: Jonas Raoni Soares Silva (https://www.jsfromhell.com)
// bugfixed by: Andreas
// bugfixed by: Onno Marsman (https://twitter.com/onnomarsman)
// improved by: Kevin van Zonneveld (https://kvz.io)
// example 1: setcookie('author_name', 'Kevin van Zonneveld')
// returns 1: true
return setrawcookie(name, encodeURIComponent(value), expires, path, domain, secure)
}{% endcodeblock %}
{% codeblock lang:typescript %}// php/network/setrawcookie (Locutus dependency module)
type CookieExpires = string | number | Date | null | undefined
function setrawcookie(
name: string,
value: string,
expires?: CookieExpires,
path?: string | null,
domain?: string | null,
secure?: boolean,
): boolean {
// discuss at: https://locutus.io/php/setrawcookie/
// original by: Brett Zamir (https://brett-zamir.me)
// original by: setcookie
// improved by: Kevin van Zonneveld (https://kvz.io)
// input by: Michael
// note 1: This function requires access to the `window` global and is Browser-only
// bugfixed by: Brett Zamir (https://brett-zamir.me)
// example 1: setrawcookie('author_name', 'Kevin van Zonneveld')
// returns 1: true
if (typeof window === 'undefined') {
return true
}
let normalizedExpires = expires
if (typeof normalizedExpires === 'string' && /^\d+$/.test(normalizedExpires)) {
normalizedExpires = parseInt(normalizedExpires, 10)
}
if (normalizedExpires instanceof Date) {
normalizedExpires = normalizedExpires.toUTCString()
} else if (typeof normalizedExpires === 'number') {
normalizedExpires = new Date(normalizedExpires * 1e3).toUTCString()
}
const parts = [name + '=' + value]
if (normalizedExpires) {
parts.push('expires=' + normalizedExpires)
}
if (path) {
parts.push('path=' + path)
}
if (domain) {
parts.push('domain=' + domain)
}
if (secure) {
parts.push('secure')
}
window.document.cookie = parts.join(';')
return true
}
// php/network/setcookie (target function module)
function setcookie(
name: string,
value: string,
expires?: string | number | Date | null,
path?: string | null,
domain?: string | null,
secure?: boolean,
): boolean {
// discuss at: https://locutus.io/php/setcookie/
// parity verified: PHP 8.3
// original by: Jonas Raoni Soares Silva (https://www.jsfromhell.com)
// bugfixed by: Andreas
// bugfixed by: Onno Marsman (https://twitter.com/onnomarsman)
// improved by: Kevin van Zonneveld (https://kvz.io)
// example 1: setcookie('author_name', 'Kevin van Zonneveld')
// returns 1: true
return setrawcookie(name, encodeURIComponent(value), expires, path, domain, secure)
}{% endcodeblock %}
{% codeblock lang:javascript %}// php/network/setrawcookie (Locutus dependency module)
function setrawcookie(name, value, expires, path, domain, secure) {
// discuss at: https://locutus.io/php/setrawcookie/
// original by: Brett Zamir (https://brett-zamir.me)
// original by: setcookie
// improved by: Kevin van Zonneveld (https://kvz.io)
// input by: Michael
// note 1: This function requires access to the `window` global and is Browser-only
// bugfixed by: Brett Zamir (https://brett-zamir.me)
// example 1: setrawcookie('author_name', 'Kevin van Zonneveld')
// returns 1: true
if (typeof window === 'undefined') {
return true
}
let normalizedExpires = expires
if (typeof normalizedExpires === 'string' && /^\d+$/.test(normalizedExpires)) {
normalizedExpires = parseInt(normalizedExpires, 10)
}
if (normalizedExpires instanceof Date) {
normalizedExpires = normalizedExpires.toUTCString()
} else if (typeof normalizedExpires === 'number') {
normalizedExpires = new Date(normalizedExpires * 1e3).toUTCString()
}
const parts = [name + '=' + value]
if (normalizedExpires) {
parts.push('expires=' + normalizedExpires)
}
if (path) {
parts.push('path=' + path)
}
if (domain) {
parts.push('domain=' + domain)
}
if (secure) {
parts.push('secure')
}
window.document.cookie = parts.join(';')
return true
}
// php/network/setcookie (target function module)
function setcookie(name, value, expires, path, domain, secure) {
// discuss at: https://locutus.io/php/setcookie/
// parity verified: PHP 8.3
// original by: Jonas Raoni Soares Silva (https://www.jsfromhell.com)
// bugfixed by: Andreas
// bugfixed by: Onno Marsman (https://twitter.com/onnomarsman)
// improved by: Kevin van Zonneveld (https://kvz.io)
// example 1: setcookie('author_name', 'Kevin van Zonneveld')
// returns 1: true
return setrawcookie(name, encodeURIComponent(value), expires, path, domain, secure)
}{% endcodeblock %}