27: Session and Cookies in PHP | PHP Tutorial | Learn PHP Programming | PHP for Beginners
HTML-код
- Опубликовано: 26 авг 2024
- Session and Cookies in PHP | PHP Tutorial | Learn PHP Programming | PHP for Beginners. Cookies and sessions are used when we want to collect or store data from users. An example could be if we want to make a login system or a shopping cart since these require user interaction. So if you are interested in having users on your website, then you need to learn this.
➤ GET ACCESS TO MY LESSON MATERIAL HERE!
First of all, thank you for all the support you have given me!
I am really glad to have such an awesome community on my channel. It motivates me to continue creating and uploading content! So thank you!
I am now using Patreon to share improved and updated lesson material, and for a small fee you can access all the material. I have worked hard, and done my best to help you understand what I teach.
I hope you will find it helpful :)
Material for this lesson: / lesson-material-42361704
I had my exam today and I got a question on session and cookies. I watched this video in the morning. Because of your video I could answer that question.
Thank you 🙏💕
i was like number 69.
A tip for writing the expiration-time is to, instead of writing it in seconds, write it in a more comprehendable manner:
SYNTAX:
time() + seconds * minutes * hours * days
Example for a cookie that lasts a week (7 days):
time() + 60 * 60 * 24 * 7
do you know how to expire cookies for the next 8 years??
@@WahyuArtka time() + 60 * 60 * 24 * 365 * 8
Daniel....great set of videos, thanks for taking the time to make them. Small point - the number after time() is seconds not mili-seconds
+Bill Baird Thank you for pointing that out Bill :) I was calculating it on the side as I was recording the tutorial and must have gotten it wrong
So should I use 86.400 or 86.400.000 for one day?
Good question +Vinicius Valente, was wondering the same.
24 hours * 60 minutes * 60 seconds is 86.400 seconds, but how does PHP interpret that number?
EDIT (found the answer):
Just found the answer on php.net/manual/en/function.setcookie.php
"expire
The time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch. In other words, you'll most likely set this with the time() function plus the number of seconds before you want it to expire."
In other words:
Daniel has set the value correctly in this tutorial, if he wants the cookie to be expired after 1 day.
I actually was coming down to to comments section to say the same, but found this already :p
@@viniciusvalente312 86400 for 1 day 👍
This guy is on point. I've been trying to learn this stuff for a while now. Great job on explaining the semantics.
Now as I watched your video , I understand now what is cookie. Thanks!
You've said my name so many times.
Great explanation, bro! Best PHP series on RUclips man! :)
Your tutorials look very good; i have know some PHP and JS but i think when i watch all of your tutorials(about js/php) i would be so much better :p thanks!
Thanks for all the videos man, one of the best channels explaining PHP really good ..
I'm here! 😎 Great video, just posting a comment to show some support! Keep up the good work. 🤙
Thanks man for sharing your knowledge. Cheers! God bless you!
Great videos 👍 In this video you mentioned that usernames/passwords should be stored in sessions and that sessions end as soon as you close the browser. But When I go to a website that needs me to login, I can close the site and then go back and still be logged in. Does that mean that site is using cookies to store my username and password? Or is there something else they're using to remember my username and password?
Good info. However, 1 day = 86400 seconds, not milliseconds as you said. So should seconds or milliseconds be used for cookie time? If milliseconds, you should enter 86400000 instead.
I´ve been watching your videos for a week and seems to be longer! great job!
Awesome Tutorial!Thanks for sharing!
if u want delete session you can use
session_unset('your session name');
or
session_unset();
clear everything
First of all, you have the best php tutorials on youtube! :D
Secondly, I was wondering if it's possible to share a session between two or more users. Here is what I mean.
Let's say we have a $_SESSION['room'] (which stores a multidimensional array) is it possible to share a reference to this session between two users?
I tried something like generating a random name for the room and store it inside a variable called $room_id and than I started a session $_SESSION[$room_id] but of course it didn't work :).
Thanks in advance!
hello, if you are still here, how are you doing three years from now? lol
@@kreut8817 Hello AJ, I have to say your comment was a pleasant surprise, I almost shed a tear :,D. I am doing great, thanks for asking! Looking back I never expected actually become a web developer, it was more of a passion project. Although I switched from PHP to JavaScript, this channel is where my passion for web development started. I am currently working for a fin tech company and I love it. How are you doing?
@@adriandinca5682 Hello Adrian, I am doing great! It's nice to know after three years, you have found a place where you can do what you love. I feel like I am where you once were before, in a stage of learning, hoping to become something in the future. Next year, I'll be a senior in high school, so I've been spending my time binge watching programming videos like these as well as coding projects. I'm so happy to hear from you! I never expected a response.
@@adriandinca5682 , are you ditching PHP completely? did you find the JS much more easier to use?
You wrote it wrongly, here is the correct one to start SESSION named room with a value of $room:
$_SESSION["room"] = $room;
I am using xampp on windows. I restarted my computer and started xampp but my previous logged in session stayed. Why is that so? Also it is fresh install of xampp.
sessions are stored in server then how can closing chrome delete session?
I use a cookie to remember the user so they dont have to log in every time they visit the website and I store there username in that cookie is this wrong?
@HOTBOINICK i found out its bad because if you know another accounts username you can simpally change the cookie to there username and you would be logged in
@HOTBOINICK lol we learn as we go
Thank you Danial for such great series. Do we you what is the id of a user when we assign value for session ? If yes how do we know ?
If I had to guess (not there yet, but from my previous slight experiences with MySQL):
The ID (for example '12') corresponds with an ID in a database, they you either create manually or that is being assigned automatically upon user creation.
In that table you will have one row with ID's (i.e. 1 to 20) and the row next to it has the usernames corresponding to those ID's.
Anyone feel free to correct me if I'm wrong, but I think that's how it will work when we arrive at the tutorial about databases and login systems.
I'm completely new to php, well programming on a whole. However we were asked to make a cookie for our store page. We use the MVC approach, how would this be implemented using that approach.
How is PHP on my server able to write a cookie in the client's browser?
Excellent videos!
thank you!
In one day there are 86400 seconds and 86400000 milliseconds.
yeah, the third setcookie() parameter is set in seconds, not in milliseconds as he said
bullshit, 86400 seconds and 86400000 milliseconds would be 172,800 seconds. there are only 86400 seconds in a day OR 86400000 milliseconds. Please,man your in the coding world, syntax is important. JK
Would there be a reason why a session may drop before a session destroy and don't close the browser? Im using PHP 5.5.4 on xamp package 1.8.3, and I cannot for the life of me work out why sessions could drop without either of the aforementioned events happening.
Thanks Daniel
Nice
For me own-self X video with lesson 27 and 28 also ( search login system by mmtuts or something similar ).
Which keyboard are you using ?
I would have appreciated explanations on retrieving the variables and performing other actions with them.
Are you perhaps referring to the stuff that was explained in previous lessons?
Hopefully you'll find there what you were looking for.
The whole course starts here:
ruclips.net/video/qVU3V0A05k8/видео.html
And here the explanations about variables begin:
ruclips.net/video/CoorcqbkpI0/видео.html
Hello mr Dani, Iam having a problem its like $_SESSIONS dont work in google chrome any help please
session changed from page to page. how to rectify this problem.
Turns out you have to start a session on each new page that is executed and in the page where you are setting session variables BEFORE you set them (I think he shows this in the next video, but anyway..)
-----index.php-----
if (!isset($_SESSION)) {
session_start();
}
$_SESSION['name'] = 'Daniel;
-----test.php-----
if (!isset($_SESSION)) {
session_start();
}
echo $_SESSION['name'];
nice, digital baking
Programming is a very powerful tool. You can create universes with it... and destroy them as well.
I'm stuck here, offfff...
I don't get it.. Why should we make the time negative when deleting a cookie instead of just removing the line of code??
If you delete a line of code, it will get deleted for all the users using the site. And because PHP is a language used to make dynamic content on web sites, it defeats the purpose. 🙂
Look at it this way...
If you have vending machine that people can get a variety of snacks from. Deleting the line of code would be the same as stopping the machine from being able to sell, let’s say chips, to ALL customers. Not just to some of the them.
Instead we need the customers to be able to buy chips, but also cancel their purchase if needed.
And that’s why we have statements in PHP to determine “if the customer wants chips” as well “if they want to cancel their order”.
in 3:58.
You saying milliseconds and adding 86400 second??? a day has 86400*1000 milliseconds
PHP works with seconds indeed:
Just found the answer on php.net/manual/en/function.setcookie.php
"expire
The time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch. In other words, you'll most likely set this with the time() function plus the number of seconds before you want it to expire."
In other words:
Daniel has set the value correctly in this tutorial, if he wants the cookie to be expired after 1 day.
when the course gonna update?
❤
Did I just learn how to steal to steal client data. Holy shit.
How to make cookie logger
at 5:50 there's a sound. Can you describe that? I mean i am not mocking you with this comment but just curious to know.
Probably rain in the background
Pls help me i want make an asmin panel !
I am watching how to make cookies in php, meanwhile my wife is watching how to make chocolate cookies. I'm kidding.
how can i download all video at once
On my Patreon I have a link where you can download all my videos :)
Millisecond?
but how do we actually use it tho
My login system project video later on shows how :)
u said lock in or login ? haha anyways thanks for the tutorial
wait, he doesn't actually set a cookie since the time is in negative
That's exactly right. This is how you "unset a cookie" 🙂 I explained that in the video.
Start a session:
session_start();
Yea he missed this, but he targeted to manifest the main difference between cookies and sessions...
/
heck...
errrr........???!!