├── README.md ├── css ├── login.css ├── style.css └── validation.css ├── layout ├── page_footer.php └── page_header.php ├── phpinfo ├── php_custom.php ├── server_variables.php └── session_variables.php └── session ├── close_session.php ├── index.php ├── panel.php └── validation.php /README.md: -------------------------------------------------------------------------------- 1 | `#php-sessions` `#Assembler Institute` 2 | 3 | Pill Assignment for # Assembler Institute: [PHP SESSIONS WORKSHOP] Workshop 4 | 5 | Completed following the Assembler Institute workshop: 6 | (https://github.com/assembler-institute/php-sessions-workshop) 7 | 8 | ## Table of Contents 9 | 10 | - [Task organization](#taskorganization) 11 | - [Contents](#contents) 12 | - [Resources](#resources) 13 | 14 | ## Task organization 15 | 16 | - Practice pages covering server and session variables were completed first, as well as editing php.ini files. 17 | - Basic design using .css was included, as well as use of 'include/require' to repeated use of headers and footers throughout the assignment. 18 | - The login system was added next, starting with the login page, followed by validation, panel and close_session pages. 19 | 20 | 21 | ## Contents 22 | - Use of php, with external styles added in .css. 23 | 24 | 25 | ## Resources 26 | 27 | [PHP Sessions](https://www.php.net/manual/es/reserved.variables.session.php) 28 | [PHP Globals](https://www.php.net/manual/en/reserved.variables.globals.php) 29 | [Assembler Sessions Workshop](https://github.com/assembler-institute/php-sessions-workshop) 30 | -------------------------------------------------------------------------------- /css/login.css: -------------------------------------------------------------------------------- 1 | .login { 2 | margin-top: 3rem; 3 | width: 100%; 4 | } 5 | 6 | .login__form { 7 | width: 100%; 8 | flex-direction: column; 9 | margin: auto; 10 | } 11 | 12 | .login h1 { 13 | margin-bottom: 1.6rem; 14 | /* font-weight:400; */ 15 | font-size: 1.4rem; 16 | } 17 | 18 | fieldset { 19 | display: flex; 20 | flex-direction: column; 21 | align-items: center; 22 | border: none; 23 | width: 100%; 24 | } 25 | 26 | .label { 27 | display: none; 28 | } 29 | 30 | input { 31 | padding: 0.5rem; 32 | min-width:14rem; 33 | max-width: 30%; 34 | } 35 | 36 | input:focus::placeholder { 37 | color: transparent; 38 | } 39 | 40 | .form__footer { 41 | flex-direction: column; 42 | } 43 | 44 | .btn { 45 | margin-top: 1.6rem; 46 | width: fit-content; 47 | padding: 0.4rem 3rem; 48 | background-color: var(--color--lines); 49 | color: white; 50 | border: 0.1rem solid var(--color--header); 51 | border-radius: 0.25rem; 52 | font-size: 1rem; 53 | } 54 | 55 | .btn:hover { 56 | border: 0.3rem solid var(--color--header); 57 | } 58 | 59 | .error__msg { 60 | margin-top: 1rem; 61 | font-size: 1.4rem; 62 | color: firebrick; 63 | } 64 | 65 | -------------------------------------------------------------------------------- /css/style.css: -------------------------------------------------------------------------------- 1 | @import url('./login.css'); 2 | @import url('./validation.css'); 3 | 4 | 5 | * { 6 | margin: 0; 7 | padding: 0; 8 | font-family: sans-serif; 9 | } 10 | 11 | :root { 12 | --color--header: #61A4bc; 13 | --color--instr: #6b8ab8; 14 | --color--lines: #2d3e58; 15 | --color--answer: #7db15b; 16 | --border-style: solid 0.3rem var(--color--instr); 17 | 18 | } 19 | 20 | .main { 21 | display: flex; 22 | flex-direction: column; 23 | position: relative; 24 | min-height: 100vh; 25 | } 26 | 27 | .nav { 28 | display: flex; 29 | flex-direction: row; 30 | background-color: var(--color--header); 31 | position: sticky; 32 | top: 0; 33 | height: 8vh; 34 | margin: 0; 35 | padding: 0; 36 | width: 100%; 37 | } 38 | 39 | .nav__ul { 40 | list-style: none; 41 | display: flex; 42 | justify-self: flex-start; 43 | position: absolute; 44 | left: 2rem; 45 | background-color: var(--color--header); 46 | } 47 | 48 | .nav__text { 49 | color: white; 50 | text-decoration: none; 51 | text-transform: capitalize; 52 | font-weight: bold; 53 | padding: 0 1rem; 54 | background-color: var(--color--header); 55 | 56 | } 57 | 58 | .nav__ul li+li:before { 59 | color: white; 60 | content: ">"; 61 | font-weight: bold; 62 | } 63 | 64 | .nav__text:hover { 65 | color: var(--color--lines); 66 | text-decoration: underline; 67 | } 68 | 69 | .nav__header { 70 | position: absolute; 71 | right: 2rem; 72 | font-weight: bold; 73 | font-size: 2.2rem; 74 | color: var(--color--lines); 75 | } 76 | 77 | .content { 78 | margin: 3rem 3rem; 79 | display: flex; 80 | flex-direction: column; 81 | padding-bottom: 10vh; 82 | 83 | } 84 | 85 | .task { 86 | padding: 1rem; 87 | background-color: var(--color--lines); 88 | color: white; 89 | font: bolder; 90 | font-size: larger; 91 | border-radius: 0.25rem 0.25rem 0 0 ; 92 | } 93 | 94 | .instr { 95 | background-color: #DdDDDD; 96 | margin-top: 0; 97 | margin-bottom: 1rem; 98 | padding: 2rem 3rem; 99 | border-radius: 0.25rem; 100 | } 101 | 102 | .answer { 103 | margin-top: 1rem; 104 | overflow-x: scroll; 105 | background-color: rgb(240, 240, 240); 106 | border-left: solid 0.4rem var(--color--answer); 107 | padding: 1rem 2rem; 108 | box-shadow: 0 0.05rem var(--color--answer); 109 | } 110 | 111 | .mark { 112 | font-weight: bold; 113 | color: var(--color--answer); 114 | } 115 | 116 | .flex { 117 | display: flex; 118 | align-items: center; 119 | justify-content: center; 120 | } 121 | 122 | .page__footer { 123 | background-color: var(--color--header); 124 | position: absolute; 125 | bottom: 0; 126 | display: flex; 127 | align-items: center; 128 | justify-content: center; 129 | height: 10vh; 130 | width: 100%; 131 | color: white; 132 | font-weight: bold; 133 | font-size: 1.2rem; 134 | 135 | } 136 | 137 | -------------------------------------------------------------------------------- /css/validation.css: -------------------------------------------------------------------------------- 1 | 2 | /* Validation and Panel */ 3 | .panel { 4 | flex-direction: column; 5 | } 6 | 7 | .validation__msg { 8 | flex-direction: column; 9 | } 10 | 11 | .validation { 12 | margin-top: 1rem; 13 | font-size: 1.6rem; 14 | text-align: center; 15 | width: fit-content; 16 | padding: 1rem 3rem; 17 | } 18 | 19 | .success { 20 | border: solid 0.2rem var(--color--answer); 21 | } 22 | 23 | .fail { 24 | border: solid 0.2rem firebrick; 25 | } 26 | -------------------------------------------------------------------------------- /layout/page_footer.php: -------------------------------------------------------------------------------- 1 | 5 | 8 | 9 | 10 | 11 | '; 12 | 13 | echo $pageFooter; 14 | 15 | ?> -------------------------------------------------------------------------------- /layout/page_header.php: -------------------------------------------------------------------------------- 1 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | PHP Sessions Pill 13 | 14 | 15 |
16 | 27 |
'; 28 | 29 | echo $pageHeader; 30 | 31 | ?> 32 | 33 | 34 | -------------------------------------------------------------------------------- /phpinfo/php_custom.php: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 |
Find the function that shows your php.ini path.
7 |
8 |
    9 |
  • Change the time zone to Europe / Madrid.
  • 10 |
  • Maximum execution time "max_execution_time" from 30 to 60 seconds.
  • 11 |
  • Add a comment at the end of the file.
  • 12 |
13 |
14 | '; 16 | $inipath = php_ini_loaded_file(); 17 | 18 | if (!$inipath) { 19 | echo 'Your php.ini cannot be loaded.'; 20 | } else { 21 | echo "Your php.ini path is:

$inipath"; 22 | } 23 | echo ''; 24 | 25 | echo '
'; 26 | echo "
27 |     echo ini_get('date.timezone');
28 |     ini_set('date.timezone');
29 |     echo ini_get('date.timezone');

"; 30 | echo ini_get('date.timezone') . ' changes to...'; 31 | ini_set('date.timezone', 'Europe/Madrid'); 32 | echo '

'; 33 | echo ini_get('date.timezone'); 34 | echo '

'; 35 | echo '
other options: use date_default_timezone_set(), or edit file manually.'; 36 | echo '

'; 37 | 38 | echo '
'; 39 | echo "
40 |     echo ini_get('max_execution_time');
41 |     ini_set('max_execution_time', '60');
42 |     echo ini_get('max_execution_time');

"; 43 | echo ini_get('max_execution_time') . ' changes to...'; 44 | ini_set('max_execution_time', '60'); 45 | echo '

'; 46 | echo ini_get('max_execution_time'); 47 | echo '

'; 48 | echo '

'; 49 | 50 | echo '
'; 51 | echo "echo phpinfo();

"; 52 | // $phpinfo = print_r(phpinfo()); 53 | // echo 'Readout of phpinfo() would appear here.'; 54 | echo phpinfo(); 55 | echo '
'; 56 | 57 | require '../layout/page_footer.php'; -------------------------------------------------------------------------------- /phpinfo/server_variables.php: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 |
Using local PHP server variable $_SERVER
7 | 8 | '; 10 | 11 | echo '
';
12 |     print_r($_SERVER);
13 |     echo '
'; 14 | echo ''; 15 | 16 | 17 | require '../layout/page_footer.php'; -------------------------------------------------------------------------------- /phpinfo/session_variables.php: -------------------------------------------------------------------------------- 1 | 4 | 5 |
Start a session and dump the content of the $_SESSION variable.
6 |
Add this to script:
7 |
 8 |         session_start();
 9 |         $_SESSION['browser'] = $_SERVER['HTTP_USER_AGENT'];
10 |         $_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];
11 |         $_SESSION['time'] = time();
12 |         echo '<pre>;
13 |         print_r($_SESSION);
14 |     
15 | 16 | '; 19 | 20 | $_SESSION['browser'] = $_SERVER['HTTP_USER_AGENT']; 21 | $_SESSION['ip'] = $_SERVER['REMOTE_ADDR']; 22 | $_SESSION['time'] = time(); 23 | 24 | echo '
';
25 |     print_r($_SESSION);
26 |     
27 |     echo '';
28 |    
29 | 
30 |     require '../layout/page_footer.php';
31 | 


--------------------------------------------------------------------------------
/session/close_session.php:
--------------------------------------------------------------------------------
 1 | 
 2 | 


--------------------------------------------------------------------------------
/session/index.php:
--------------------------------------------------------------------------------
 1 | 
13 | 
14 |         
Creating a login page in PHP
15 |
16 |
    17 |
  • require user and password fields and a login button.
  • 18 |
  • The fields must be in a form and point the action to a validation script.
  • 19 |
  • If the user is already logged in, they will be redirected to the panel.
  • 20 |
21 |
22 |
'; 40 | } else if (isset($logout)) { 41 | echo '
42 |                             
43 |

You are currently logged out.

44 |
'; 45 | } else if (isset($redirect)) { 46 | echo '
47 |                             
48 |

That is an internal page. You have been redirected.

49 |
'; 50 | }; 51 | ?> 52 | 53 |
54 | 55 | 56 | -------------------------------------------------------------------------------- /session/panel.php: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 |
Create a panel.php file. This panel should:
8 |
9 | 14 |
15 | 23 |
24 |
25 |

Welcome $currentEmail!

26 |

You are currently logged in.

27 |
28 | 29 |
30 |
31 |
"; 32 | } 33 | 34 | require '../layout/page_footer.php'; 35 | ?> 36 | 37 | 38 | -------------------------------------------------------------------------------- /session/validation.php: -------------------------------------------------------------------------------- 1 | 2 | $value) { 10 | ${$key} = $value; 11 | } 12 | 13 | 14 | $dbEmail = "user@user.com"; 15 | $dbPass = "user123"; 16 | $dbPassEnc = password_hash($dbPass, PASSWORD_DEFAULT); 17 | 18 | if ($email === $dbEmail && password_verify($dbPass, $dbPassEnc)) { 19 | // pw is good 20 | session_start(); // start session 21 | $_SESSION["email"] = $email; // create session variable we can use from panel page 22 | header('Location: ./panel.php'); 23 | // move to panel page 24 | } else { 25 | header('Location: ./index.php?loginerr=true'); 26 | } 27 | 28 | 29 | require '../layout/page_footer.php'; 30 | ?> --------------------------------------------------------------------------------