PHPDaemon\Servers\WebSocket\Connection::header PHP Method

header() public method

Send the header
public header ( string $s, boolean $replace = true, boolean $code = false ) : boolean
$s string Header. Example: 'Location: http://php.net/'
$replace boolean Optional. Replace?
$code boolean Optional. HTTP response code
return boolean Success
    public function header($s, $replace = true, $code = false)
    {
        if ($code) {
            $this->status($code);
        }
        if ($this->headers_sent) {
            throw new RequestHeadersAlreadySent();
        }
        $s = strtr($s, "\r\n", '  ');
        $e = explode(':', $s, 2);
        if (!isset($e[1])) {
            $e[0] = 'STATUS';
            if (strncmp($s, 'HTTP/', 5) === 0) {
                $s = substr($s, 9);
            }
        }
        $k = strtr(strtoupper($e[0]), Generic::$htr);
        if ($k === 'CONTENT_TYPE') {
            self::parseStr(strtolower($e[1]), $ctype, true);
            if (!isset($ctype['charset'])) {
                $ctype['charset'] = $this->upstream->pool->config->defaultcharset->value;
                $s = $e[0] . ': ';
                $i = 0;
                foreach ($ctype as $k => $v) {
                    $s .= ($i > 0 ? '; ' : '') . $k . ($v !== '' ? '=' . $v : '');
                    ++$i;
                }
            }
        }
        if ($k === 'SET_COOKIE') {
            $k .= '_' . ++$this->cookieNum;
        } elseif (!$replace && isset($this->headers[$k])) {
            return false;
        }
        $this->headers[$k] = $s;
        if ($k === 'CONTENT_LENGTH') {
            $this->contentLength = (int) $e[1];
        } elseif ($k === 'LOCATION') {
            $this->status(301);
        }
        if (Daemon::$compatMode) {
            is_callable('header_native') ? header_native($s) : header($s);
        }
        return true;
    }

Usage Example

Example #1
0
 /**
  * Set the cookie
  * @param  string  $name     Name of cookie
  * @param  string  $value    Value
  * @param  integer $maxage   Optional. Max-Age. Default is 0.
  * @param  string  $path     Optional. Path. Default is empty string.
  * @param  string  $domain   Optional. Domain. Default is empty string.
  * @param  boolean $secure   Optional. Secure. Default is false.
  * @param  boolean $HTTPOnly Optional. HTTPOnly. Default is false.
  * @return void
  */
 public function setcookie($name, $value = '', $maxage = 0, $path = '', $domain = '', $secure = false, $HTTPOnly = false)
 {
     $this->client->header('Set-Cookie: ' . $name . '=' . rawurlencode($value) . (empty($domain) ? '' : '; Domain=' . $domain) . (empty($maxage) ? '' : '; Max-Age=' . $maxage) . (empty($path) ? '' : '; Path=' . $path) . (!$secure ? '' : '; Secure') . (!$HTTPOnly ? '' : '; HttpOnly'), false);
 }