PHP Session and Pass values through Session
From w3cyberlearnings
Contents |
Session
- You can store information in session
- And you also can retrieve information in session
- Session can be used in many ways
Where to place session?
- Session must be place on the top of the page.
Example 1
student.php
- In this page, we assign the session values that will be used in the student_list.php page.
- $_SESSION['name']='Bob Maat';
- $_SESSION['authorized']=1;
<?php session_start(); $_SESSION['name'] = 'Bob Maat'; $_SESSION['authorized'] = 1; ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>All User</title> </head> <body> <a href="student_list.php">Click Here</a> </body> </html>
student_list.php
- Check the user session has been assigned. If not, will generate an error message and exit.
<?php session_start(); if ($_SESSION['authorized'] != 1) { echo 'Sorry, you do not have permission!'; exit(); } ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>List User</title> </head> <body> <?php echo 'Student name: ' . $_SESSION['name'] . '<br/>'; ?> </body> </html>