Core\Request::scheme
Core\Request::scheme(): tainted stringReturns the scheme of the request that your program is answering. The scheme is "http" or "https". It tells you if the connection was encrypted. The result is never null, because every request arrives over one of the two.
Often a proxy receives the encrypted connection and sends the request on to your server over plain http. The proxy then adds the header X-Forwarded-Proto: https. Novis believes this header only when the server configuration lists the proxy in trusted_proxies. Without that setting, scheme returns the scheme of the connection itself, so a client cannot claim https by sending the header.
The result is tainted. A tainted string came from the client, so Novis makes you check it before you use it in a query or a file path. A command-line program answers no request, so scheme throws a LogicError there.
The examples below show the full address of a page, a header that scheme does not believe, and a login page that is only shown over https.
Return value
Returns tainted string. "http" or "https", as a tainted string. The result is never null, because every request arrived over one of them. A server that trusts no proxy always returns the scheme of the connection itself.
Errors
LogicErrorThis program is not handling a request. For example, it is a command-line program, a scheduled script, a job worker or a test.
Examples
The full address of a page
<?nvs// Shows how to write the full address of the page that the client asked for.
// `Core\Request::scheme()` returns "https" for this request.tainted string $scheme = Core\Request::scheme();tainted string $host = Core\Request::host() ?? 'localhost';
// This prints "https://shop.example.com/orders".echo $scheme, '://', $host, Core\Request::path(), "\n";https://shop.example.com/ordersA header that is not believed
<?nvs// Shows that a client cannot change the scheme by sending a header.
// The request came over `http` and has the header `X-Forwarded-Proto: https`.// The server trusts no proxy, so it ignores the header. This prints "http".echo Core\Request::scheme(), "\n";
// You can still read the header yourself. This prints "https".echo Core\Request::header('x-forwarded-proto') ?? 'none', "\n";httphttpsA login page only over https
<?nvs// Shows a login page that is only shown when the connection is encrypted.
// A password sent over plain `http` can be read by anybody on the network.// This request came over `http`, so the program shows a message instead of the form.if (Core\Request::scheme() == 'https') { echo "<form method=\"post\">password: <input type=\"password\"></form>\n";} else { // This prints the message below. echo "Please open https://shop.example.com/login instead.\n";}Please open https://shop.example.com/login instead.