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

Комментарии • 86

  • @anvinawalgaria
    @anvinawalgaria 6 лет назад +87

    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 🙏💕

  • @alexanderer_
    @alexanderer_ Год назад +4

    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

  • @no_short_circuit
    @no_short_circuit 8 лет назад +65

    Daniel....great set of videos, thanks for taking the time to make them. Small point - the number after time() is seconds not mili-seconds

    • @Dani_Krossing
      @Dani_Krossing  8 лет назад +10

      +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

    • @viniciusvalente312
      @viniciusvalente312 6 лет назад

      So should I use 86.400 or 86.400.000 for one day?

    • @nrnugteren
      @nrnugteren 6 лет назад +2

      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.

    • @danish6192
      @danish6192 5 лет назад +1

      I actually was coming down to to comments section to say the same, but found this already :p

    • @WaliBD1
      @WaliBD1 6 месяцев назад

      ​​@@viniciusvalente312 86400 for 1 day 👍

  • @esu9591
    @esu9591 7 лет назад +7

    This guy is on point. I've been trying to learn this stuff for a while now. Great job on explaining the semantics.

  • @funixer
    @funixer 4 года назад +1

    Now as I watched your video , I understand now what is cookie. Thanks!

  • @senju31
    @senju31 5 лет назад +13

    You've said my name so many times.

  • @yannik8271
    @yannik8271 2 года назад +1

    Great explanation, bro! Best PHP series on RUclips man! :)

  • @rgryczan83
    @rgryczan83 7 лет назад +2

    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!

  • @starkandjack6411
    @starkandjack6411 3 года назад +1

    Thanks for all the videos man, one of the best channels explaining PHP really good ..

  • @Born2DoubleUp
    @Born2DoubleUp 2 года назад

    I'm here! 😎 Great video, just posting a comment to show some support! Keep up the good work. 🤙

  • @samuelkaka1350
    @samuelkaka1350 4 года назад +1

    Thanks man for sharing your knowledge. Cheers! God bless you!

  • @ricekrissy9769
    @ricekrissy9769 4 года назад +1

    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?

  • @atmian
    @atmian 6 месяцев назад

    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.

  • @seifcocksucker5457
    @seifcocksucker5457 7 лет назад

    I´ve been watching your videos for a week and seems to be longer! great job!

  • @ExplosionBoxes
    @ExplosionBoxes 5 лет назад +1

    Awesome Tutorial!Thanks for sharing!

  • @jackywong941
    @jackywong941 6 лет назад +1

    if u want delete session you can use
    session_unset('your session name');
    or
    session_unset();
    clear everything

  • @adriandinca5682
    @adriandinca5682 7 лет назад +3

    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!

    • @kreut8817
      @kreut8817 4 года назад +2

      hello, if you are still here, how are you doing three years from now? lol

    • @adriandinca5682
      @adriandinca5682 4 года назад +3

      @@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?

    • @kreut8817
      @kreut8817 4 года назад +1

      @@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.

    • @biskutnation
      @biskutnation 4 года назад

      @@adriandinca5682 , are you ditching PHP completely? did you find the JS much more easier to use?

    • @janx963
      @janx963 Год назад

      You wrote it wrongly, here is the correct one to start SESSION named room with a value of $room:
      $_SESSION["room"] = $room;

  • @a.yashwanth
    @a.yashwanth 4 года назад +1

    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.

  • @collegestudy5977
    @collegestudy5977 4 года назад +1

    sessions are stored in server then how can closing chrome delete session?

  • @cmd9183
    @cmd9183 4 года назад +3

    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?

    • @cmd9183
      @cmd9183 4 года назад +2

      @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

    • @cmd9183
      @cmd9183 4 года назад

      @HOTBOINICK lol we learn as we go

  • @sharifabahar6257
    @sharifabahar6257 7 лет назад +1

    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 ?

    • @nrnugteren
      @nrnugteren 6 лет назад +2

      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.

  • @traciss
    @traciss 2 года назад

    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.

  • @RagHelen
    @RagHelen 5 лет назад +1

    How is PHP on my server able to write a cookie in the client's browser?

  • @ASouza-ut9kb
    @ASouza-ut9kb 5 лет назад

    Excellent videos!

  • @yongjung5433
    @yongjung5433 5 лет назад +1

    thank you!

  • @rizwanhamidrandhawa8090
    @rizwanhamidrandhawa8090 6 лет назад +32

    In one day there are 86400 seconds and 86400000 milliseconds.

    • @simopelle
      @simopelle 4 года назад +1

      yeah, the third setcookie() parameter is set in seconds, not in milliseconds as he said

    • @xiaoti8035
      @xiaoti8035 3 года назад

      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

  • @1699107
    @1699107 4 года назад

    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.

  • @mariac7772
    @mariac7772 5 лет назад

    Thanks Daniel

  • @lakshanchamod1208
    @lakshanchamod1208 2 месяца назад

    Nice

  • @GohersWay
    @GohersWay 4 года назад

    For me own-self X video with lesson 27 and 28 also ( search login system by mmtuts or something similar ).

  • @siddharthgaglani4445
    @siddharthgaglani4445 3 года назад

    Which keyboard are you using ?

  • @sstream17
    @sstream17 6 лет назад

    I would have appreciated explanations on retrieving the variables and performing other actions with them.

    • @nrnugteren
      @nrnugteren 6 лет назад +1

      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

  • @ericmammburu1886
    @ericmammburu1886 Год назад

    Hello mr Dani, Iam having a problem its like $_SESSIONS dont work in google chrome any help please

  • @SuperTherock12345
    @SuperTherock12345 6 лет назад +1

    session changed from page to page. how to rectify this problem.

    • @djleisheng
      @djleisheng 6 лет назад

      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'];

  • @smacdsmaccers
    @smacdsmaccers 3 года назад

    nice, digital baking

  • @tvs3497
    @tvs3497 3 года назад

    Programming is a very powerful tool. You can create universes with it... and destroy them as well.

  • @eosapril
    @eosapril 8 лет назад +7

    I'm stuck here, offfff...

  • @VeronaLynne
    @VeronaLynne 3 года назад

    I don't get it.. Why should we make the time negative when deleting a cookie instead of just removing the line of code??

    • @Dani_Krossing
      @Dani_Krossing  3 года назад +1

      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”.

  • @tusharjajodia9077
    @tusharjajodia9077 6 лет назад +1

    in 3:58.
    You saying milliseconds and adding 86400 second??? a day has 86400*1000 milliseconds

    • @nrnugteren
      @nrnugteren 6 лет назад +1

      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.

  • @Star-hw8bh
    @Star-hw8bh 2 года назад

    when the course gonna update?

  • @shruti2981
    @shruti2981 9 месяцев назад

  • @devonvermaak4101
    @devonvermaak4101 5 лет назад +1

    Did I just learn how to steal to steal client data. Holy shit.

  • @mdz7372
    @mdz7372 4 года назад

    How to make cookie logger

  • @iamchiragarora
    @iamchiragarora 6 лет назад

    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.

  • @iamnsk8811
    @iamnsk8811 6 лет назад

    Pls help me i want make an asmin panel !

  • @CastleShield
    @CastleShield 3 года назад

    I am watching how to make cookies in php, meanwhile my wife is watching how to make chocolate cookies. I'm kidding.

  • @SMAKIEVISUALMEDIA
    @SMAKIEVISUALMEDIA 4 года назад

    how can i download all video at once

    • @Dani_Krossing
      @Dani_Krossing  4 года назад +1

      On my Patreon I have a link where you can download all my videos :)

  • @Freelancer19841
    @Freelancer19841 3 года назад

    Millisecond?

  • @rafiqjarjous7542
    @rafiqjarjous7542 2 года назад

    but how do we actually use it tho

    • @Dani_Krossing
      @Dani_Krossing  2 года назад +1

      My login system project video later on shows how :)

  • @LeonSafal
    @LeonSafal 5 лет назад

    u said lock in or login ? haha anyways thanks for the tutorial

  • @HarryPotter-nc3vz
    @HarryPotter-nc3vz 3 года назад

    wait, he doesn't actually set a cookie since the time is in negative

    • @Dani_Krossing
      @Dani_Krossing  3 года назад

      That's exactly right. This is how you "unset a cookie" 🙂 I explained that in the video.

  • @kmtsvetanov
    @kmtsvetanov 5 лет назад +1

    Start a session:
    session_start();

    • @mibrahim4245
      @mibrahim4245 5 лет назад

      Yea he missed this, but he targeted to manifest the main difference between cookies and sessions...

  • @iamnsk8811
    @iamnsk8811 6 лет назад +1

    /

  • @tusharveeramani3742
    @tusharveeramani3742 6 лет назад

    heck...
    errrr........???!!