The redirect component
Redirects the user to another page.
This component helps you:
- Send users to a different page
- Stop execution of the current page
Conditional logic
There is no IF
statement in SQL. Even when you use a CASE
expression, all branches are always evaluated (and only one is returned).
To conditionally execute a component or a SQLPage function, you can use the redirect
component.
A common use case is error handling. You may want to proceed with the rest of a page only when certain pre-conditions are met.
SELECT
'redirect' AS component,
'error_page.sql' AS link
WHERE NOT your_condition;
-- The rest of the page is only executed if the condition is true
Technical limitation
You must use this component at the beginning of your SQL file, before any other components that might send content to the browser. Since the component needs to tell the browser to go to a different page by sending an HTTP header, it will fail if the HTTP headers have already been sent by the time it is executed.
Important difference from http_header
This component completely stops the page from running after it's called. This makes it a good choice for protecting sensitive information from unauthorized users.