This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
|
en:web_development:sessions:sessions [2022/02/01 16:49] mag created |
en:web_development:sessions:sessions [2023/08/16 09:33] (current) |
||
|---|---|---|---|
| Line 5: | Line 5: | ||
| ==== Setup ==== | ==== Setup ==== | ||
| - | * We'll continue to use ' | + | * We'll continue to use ' |
| **login.php** | **login.php** | ||
| Line 142: | Line 142: | ||
| < | < | ||
| </ | </ | ||
| + | | ||
| + | **menu.php** | ||
| + | < | ||
| + | <ul> | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | </ul> | ||
| + | </ | ||
| ==== Create Session ==== | ==== Create Session ==== | ||
| Line 184: | Line 198: | ||
| * Save, upload and load the page. | * Save, upload and load the page. | ||
| * Your logs should include the new line with the details of your login. | * Your logs should include the new line with the details of your login. | ||
| - | * | + | * Let's display this in the sidebar menu. Add this code before the list in ' |
| + | < | ||
| + | * Save, upload and load ' | ||
| + | * You should see your user name at the top left of the menu. | ||
| + | * We can align it better by adding some padding to the left. Add this code (and any other styling you like) to ' | ||
| + | < | ||
| + | padding-left: | ||
| + | }</ | ||
| + | |||
| + | ==== Ending Sessions ==== | ||
| + | * What happens if a user tries to load the form without having logged in? | ||
| + | * To test that, we first need to log out. | ||
| + | * Create a file called ' | ||
| + | < | ||
| + | session_start(); | ||
| + | unset($_SESSION[' | ||
| + | header(" | ||
| + | ?></ | ||
| + | * < | ||
| + | * < | ||
| + | * The third line sends the user back to the login page. | ||
| + | * Add ' | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | </ | ||
| + | </ | ||
| + | * Save and upload all the changed files. | ||
| + | * Click on 'Log out' and check that you are returned to the login page. | ||
| + | * Now, without logging in, change the filename in the url to ' | ||
| + | * You should see the form, but your user name is missing from the top of the menu because you aren't logged in. | ||
| + | |||
| + | ==== Getting Current Filename ==== | ||
| + | |||
| + | * Now we want to redirect any user that hasn't logged in back to the login page automatically. | ||
| + | * Add the following code at the end of the PHP block in ' | ||
| + | < | ||
| + | header(" | ||
| + | }</ | ||
| + | * Save and upload the code, then refresh ' | ||
| + | * You should get an error saying that the page is in a redirect loop that will never end. | ||
| + | * This is because ' | ||
| + | * To avoid this, we can hard code the HTML from ' | ||
| + | * Instead, we can check which file is being viewed and if it's ' | ||
| + | * Add this code after < | ||
| + | < | ||
| + | error_log(" | ||
| + | * < | ||
| + | * < | ||
| + | * < | ||
| + | * Change my name for yours in the < | ||
| + | * Save and upload the code and try loading ' | ||
| + | * You'll still get the error, but now you can look at the PHP logs and see the filename ending in ' | ||
| + | * Now we can add a condition before our redirect. We want to redirect only if the we're not on ' | ||
| + | * Update the code accordingly. | ||
| + | < | ||
| + | header(" | ||
| + | }</ | ||
| + | * Save and upload the code, then make sure it all works correctly. | ||
| + | * If you're not logged in, you should be redirected to ' | ||
| + | * If you are logged in, you should be able to open ' | ||
| + | * If you navigate to ' | ||
| + | |||
| + | [[en: | ||