Web Design and Development Lecture 11 Session And
Web Design and Development Lecture # 11 Session And Login Instructor: Rida Noor Department of Computer Science
PHP Session
What is a PHP Session? �A normal HTML website will not pass data from one page to another. In other words, all information is forgotten when a new page is loaded. This makes it quite a problem for tasks like a shopping cart, which requires data(the user's selected product) to be remembered from one page to the next. �Session variables solve this problem by storing user information to be used across multiple pages (e. g. username, favorite color, etc). �By default, session variables last until the user closes the browser. 3
Session variables hold information about one single user, and are available to all pages in one application. 4
Start a PHP Session �A session is started with the session_start() function. �Session variables are set with the PHP global variable: $_SESSION. �Now, let's create “start. Session. php". a new page called �In this page, we will start a new PHP session and set some session variables: 5
Start a PHP Session �Browser Result: 6
Get PHP Session Variable Values Browser Result: 7
Get PHP Session Variable Values �We created another page called “get. Session. Values. php". From this page, we accessed the session information we set on the first page (" start. Session. php"). �Notice that session variables are not passed individually to each new page, instead they are retrieved from the session we open at the beginning of each page (session_start()). �Also notice that all session variable values are stored in the global $_SESSION variable: 8
Destroy a PHP Session �To remove all global session variables and destroy the session, use session_destroy(): �Browser Result: 9 session_unset() and
Create Database and Table in WAMP or XAMPP
Create a Database �Start Apache and My. SQL services on XAMPP. �Press Admin as pointed in image to go to php. My. Admin. 11
Create a Database 12
Create a Database �Enter Database name ‘testdb’ and press create button. 13
Create a Table �Enter table name ‘tbl_registration’ , number of fields ‘ 6’ and press GO button to create table. 14
Create a Table �Enter following fields and their types. Enter length of each field except for fld_date. Select CURRENT_TIMESTAMP value for fld_date in Default column. 15
Create a Table �Check the checkbox of A_I that is auto-increament for fld_reg_id press SAVE button. 16
Create a Table �Here you can see the table you have created. 17
PHP Connect to My. SQL
Why to establish a connection? �You should establish a connection to the My. SQL database. �This is an extremely important step because if your script cannot connect to its database, your queries to the database will fail. 19
How to establish a connection? �Create a new php file in Dreamweaver and save it as “connection. php”. �Add following code in connection. php file. 20
Check Connection �Execute connection. php page in browser and check if it displays success message or not. 21
PHP Login
HTML Login Form �Create a new html page. Save it as login. html. �Create login form in login. html as follow. 23
HTML Login Form Open the code view of login form and change followings: Browser Results: 24
PHP Login And Session (Start Session) Create a new php page, save it as login. php , add following: 25
Welcome Page (Get Session Values) Create a new php page and save it as welcome. php. Add following Code: 26
PHP Session Destroy Create a new php page and save it as logout. php. Add following Code: 27
PHP LOGIN My. SQL records: Enter Email and Password and press SIGN IN Button. 28
If Login Is Successful IF LOGIN IS NOT SUCCESSFUL SIGN OUT Click on Sign Out link welcome page will be redirected to login form page. 29
- Slides: 29