PHP Tutorial (& MySQL) #36 - Sessions

Net Ninja · Beginner ·🔧 Backend Engineering ·7y ago

Key Takeaways

Creates a simple form using PHP and utilizes sessions to persist data across multiple pages

Full Transcript

all right then gang so imagine this you're on a web page and on that web page up in the PHP somewhere we have access to a variable like age or something now if we redirect to another page and request a different file that variable is not going to be carried over okay so sometimes we would like to carry over a variable and for that we could use sessions so if we start a session on our website during that session we can access special session variable between different pages now a session does not store data on a user's computer like a cookie would but instead it stores data on the server between requests between loading different pages and this is essentially what a session is the server keeping track of information as you do different things on a website submit forms go to new pages Etc Now by default after we start a session the session will last until we close the browser so we could use this idea of sessions and session variables to keep track of data as user navigates around a website on different pages until they close the web page off all right so we're going to take a look at this idea now by creating a simple form on this sandbox page where a user can enter in their name then we're going to store that in a variable on this same page now we're going to push that to our session variable and then we can access that on different pages so I'm going to show you how all this works works now so first of all let's create this little form for a user to enter the name so then first of all let's create a form tag and inside this we need an input and that's going to be a type text the name is going to be name and then we need a button to submit so we'll say another input the type is submit and the name is submit and the value which is what is shown on the button itself is also going to be submit oops we made an error somehow submit okay so here we need text and we need an action on the form that is going to be equal to this page currently which is sandbox. PHP or we could use the other thing where we say PHP Echo server PHP self so in fact let's do that I'm going to copy and paste that from my repo just paste that in like so remember that means we're going to the current page okay PHP self on This Global the super global means we're going to have the action be the current page all right so then now we have that we need to handle that post request up here so first of all we need to see if this is set if the user has submit the form so we'll say if is set and then we want to check the post super Global and in there we want the submit button that's the name of this thing so if that's it if a user's clicked it then the first thing we want to do is say session start this starts a session and before we access any session variables or set them this has to be called to start the session then we can access the session super Global and add different variables to it so I'm going to add in a name property we need that in quotes So name is equal and that is going to be equal toore poost and it's going to be the name that we entered right here we're getting that from the post super Global okay we've seen all that before so now we're storing that in a session variable called name now what I'd like to do at this point is just Echo out the name so I'm going to Echo session and then we want the name property so let's see if this works so let's go to the sandbox page and just say Yoshi submit and and when we go forward to this page we're not echoing anything out and that's because stupidly we've not given this a method we want to say that this is going to be a post method now let's try this okay so refresh so we'll say Yoshi again submit and now we see Yoshi Echo to the browser cool so this is working now instead of echoing this right here what I'm going to do is redirect the user so we say header then Lo location and the location is going to be to index.php so we're going to that homepage from the project now now when we get there I want to access this session variable but I'm not going to do it inside the index I want to access it regardless of what page we go to now on every page in the project the header is included so we could access it in this PHP file instead and that's what we're going to do so if we come to the top we need our PHP tags first of all and inside again whenever we're accessing the session variables we need to say session uncore start like so first of all then we can get the session variables now I'm going to store this in a local variable for this file so I'll say name is equal to underscore session and then we want the name that's what we set it as over here and we're accessing it now here because we've stored it in this session super Global we have a session on the go so now we have that we could output it down in the template if we wanted to so I'm going to Output it down here I'm going to do another Li tag and in that Li tag I'm going to say hello and then I'm going to Echo the name so PHP tags and inside we Echo and because the user entered this I'm going to use HTML special chars and then I'm going to Output the name like so the variable we just created right here all right so let me just give this a class as well of gray text and then save that I'm going to refresh now over here and let's cancel that I'm just going to press enter if I enter in now Mario that's going to store it in the session variable redirect to the homepage and in the header because that's loaded in the homepage we're seeing that session variable right there and no matter which page we go to now we're seeing that variable up there okay because it's stored in that session super Global all right so that's how we set them and access them now what if we want to somehow override them well we can do that as well so I could say right here that the session and then the name variable inside that is going to be now equal to Yoshi so save that and now if I refresh you can see that now it's Yoshi and it doesn't matter if I go back to the sandbox if I enter in Shan and submit it doesn't matter that I've submit a new name because whenever we load up a different file now we're overriding that in the header okay so that's how we just override something now imagine we want to delete the name what I'm going to do is a little live check first of all and I'm going to say dollar signore server so we're using the server super Global here and I'm going to check the query uncore string remember the query string is the thing that's on the end of the URL so it's like question mark and then something or other a key and a value so I'm checking that query string and I want to see if that is equal to No Name so if it was like this question mark on the end and then no name okay now if that's the case what I want to do is delete that session variable because I'm saying I don't want a name no name now the way we delete or unset a session variable is by using the unset method and then we pass in the variable the session variable that we want to unset or remove and that is going to be the name okay so I'm going to save that and I'm going to go back to this place I'm going to say Sean submit it and now we see the name there if we go to a new page we still see Shan but now if I go to question mark no name that's the query string we're looking for if I press enter now we can see that we get an error here and it's not there either and the reason we're getting this error as well is because we're trying to access that down here and it doesn't exist anymore because we've unset it okay then so that's how we unset a single session variable now if we wanted to unset them all we could just use session unet like so and that would clear them all that would do the same thing all right so that's a quick introduction to sessions there is another way to do all this and that's using cookies and there is a difference between sessions and and cookies I'll explain that later and that's going to come in a couple of tutorials time

Original Description

Hey gang, in this PHP tutorial I'll talk about sessions and how we can use them to persist data from one page to another. ---------------------------------------- 🐱‍💻 🐱‍💻 Course Links: + Course files - https://github.com/iamshaunjp/php-mysql-tutorial + VS Code editor - https://code.visualstudio.com/ + Materialize Playlist - https://www.youtube.com/watch?v=gCZ3y6mQpW0&list=PL4cUxeGkcC9gGrbtvASEZSlFEYBnPkmff 🤑🤑 Donate + https://www.paypal.me/thenetninja 🎓🎓 Find me on Udemy + https://www.udemy.com/user/47fd83f6-5e4a-4e87-a0f0-519ac51f91b6/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

Related AI Lessons

Up next
This Cop Was Held Accountable For His Brutality! #police #lawyer
Hampton Law
Watch →