You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This component is useful for implementing redirects after a form submission,
6
-
or to redirect users to a login page if they are not logged in.
7
-
8
-
Contrary to the http_header component, this component completely stops the execution of the page after it is called,
9
-
so it is suitable to use to hide sensitive information from users that are not logged in, for example.
10
5
11
-
Since it uses an HTTP header to redirect the user, it is not possible to use this component after the page has started being sent to the browser.',
6
+
This component helps you:
7
+
1. Send users to a different page
8
+
1. Stop execution of the current page
9
+
10
+
### Conditional logic
11
+
12
+
There is no `IF` statement in SQL. Even when you use a [`CASE` expression](https://modern-sql.com/caniuse/case_(simple)), all branches are always evaluated (and only one is returned).
13
+
14
+
To conditionally execute a component or a [SQLPage function](/functions.sql), you can use the `redirect` component.
15
+
A common use case is error handling. You may want to process with the rest of a page only when certain pre-conditions are met.
16
+
17
+
```sql
18
+
SELECT
19
+
''redirect'' AS component,
20
+
''error_page.sql'' AS link
21
+
WHERE NOT your_condition;
22
+
23
+
-- The rest of the page is only executed if the condition is true
24
+
```
25
+
### Technical limitation
26
+
27
+
You must use this component **at the beginning of your SQL file**, before any other components that might send content to the browser.
28
+
Since the component needs to tell the browser to go to a different page by sending an *HTTP header*,
29
+
it will fail if the HTTP headers have already been sent by the time it is executed.
30
+
31
+
> **Important difference from [http_header](?component=http_header)**
32
+
>
33
+
> This component completely stops the page from running after it''s called.
34
+
> This makes it a good choice for protecting sensitive information from unauthorized users.
0 commit comments