Placeholder Image

字幕列表 影片播放

  • So let's talk about how you can set and read from cookies using PHP.

  • So basically, with cookies, what you're doing is you're able to set variables that remain persistent among different visits to the exact same website using the same Web browser.

  • So this is a way that when you go to a website and it says your name, So let's say you log into a website with your account, and then it says, You know, welcome back, Eli.

  • Something like that.

  • One of the ways that's ableto happen is because of cookies.

  • So basically, when you go to that website and then the user name is asked for instead of it reading directly from a database or directly from the page that you're visiting, it can actually go to a cookie and ask the cookie.

  • What is the user name of the person that previously came to the site?

  • And then it's able to plug that information in.

  • So basically, whenever you go to a website and you customize almost anything, if a customized the font size of you customized colors of you customize again, you log in and so you have a user name any kind of stuff like that that could be stored in the cookie, and then it can be persistent.

  • Ah, over numbers of visits to the website.

  • So every time you come back to the website, that information will still be there.

  • And so it will be a bit dynamically, right?

  • The website based off of those variables.

  • So that's what basically cookies allow you to do now.

  • It is important understand with cookies, especially if you're going to be developing Web sites in the modern world that if for some reason somebody has cookies turned off, then if you're your code relies too heavily on cookies that may cause a problem or something to remember.

  • If people go through and they delete their cookies than all the variables that are stored within, the cookies will go away.

  • So this is where you have to have a fine balancing act with cookies, where you use cookies enough to make it easy.

  • When people come back to the website, they don't have to pull from the database or they don't have to pull from a data source on your servers.

  • They can just pull from that.

  • The local local cookie files on the other hand.

  • If you put too much information to cookies, let's say you store 100 different variables in those cookies.

  • Well, somebody deletes all their cookies than all those different variables go away, and they start from square one.

  • And so this is something you have to think about with your design.

  • How many cookies do you actually want to store?

  • Variables?

  • What, what is that balance between ease of use for the user load on the server, the whole nine yards.

  • But cookies air basically just a great way that you can store specific variables.

  • So when people come back to your website things air configured, or you can store information that you may need to know.

  • So with that, let's go over the computer and I'll show you a basic script that I created for setting a cookie and reading from it.

  • So I think you could get a better idea of how these cookies work.

  • So I've created this script simply called cookie dot PHP.

  • And basically, with this script, I'm kind of thinking about the bones of some type of tracking cookies so you'll hear about tracking cookies.

  • And what tracking cookies do is they allow website owners to track how people use websites.

  • And so with this one, I'm kind of thinking about building out the bones of a tracking cookie by showing when the last time you visited this particular website, WAAS.

  • So the idea being is will show the last time you visited the website and what the current time is.

  • So with certain, if you're gonna do some kind of math function with this, maybe you could show how long it's been since you've been to the website.

  • Things like that.

  • So let's say this is a health website, so you would want people to should come more often.

  • So the health tracking website, tracking your weight, tracking your exercise, that type of thing.

  • You would want people to come every day, or maybe a couple of times a day.

  • Well, by creating this kind of cookie, this little time time cookie that have created what you could do is you can show how long it's been since you've been to the website.

  • So if it's been over a week since he'd been to the website, maybe you could have, and if you know it's been that long, then you can have some kind of red banner of the top saying, Hey, you need to be come back more often.

  • So that's kind of the idea of where we might be going with a cookie like this.

  • And so basically, with this cookie, all I'm going to be doing with this cookie is going to show the last time you visited the website and it's going to show you the current time when you've come to the website.

  • And so, in order to do that, all we've done is, of course, we open the PHP script, and then the first thing that we need to do is we need to set the name of the cookie.

  • So we're gonna do is we're going to use this set cookie function and with a set foot cookie, folks, in the two important things you can put in multiple values.

  • But the two values that you have to put in is, you have to put in what is the name of the cookies you're gonna have to name the cookie.

  • Gonna have to name it something.

  • Uh, and then what you're gonna have to do is you're gonna have to put in the value of the cookie.

  • So what is the value of the cookie?

  • So this could be this could be user name.

  • And this could be what your user name is.

  • You know, this could be font size of what the font size is currently.

  • What all I'm doing here is I'm creating a cookie named and so what we have is we have the dollar sign, Cookie.

  • We're gonna have a equal, and we're just gonna have this called SD cookie.

  • So for silicon does Joe, it's STD cookie.

  • So that's going to be the name of the cookie that we put in here.

  • And then I want to set the time.

  • So basically, when the script fires off, it's going to figure out what the current time is.

  • It's going to set that value to the variable dollar side time, and then that's going to set the value within the cookie itself.

  • So we're doing is we're creating the variable dollar sign time we're going to set a equal to, and there's a function.

  • So the function time reads the computer current computer time.

  • And so we're going to set the value of the variable time to whatever the current computer time is.

  • And then what we do now is we go down here to set cookie.

  • So again, the set cookie is the function that allows you to set a cookies.

  • You're going to set cookie open parentheses.

  • The first thing that you dio is you do the cookie name, so whatever the name of the cookie is going to be, and then you do a comma and then you do whatever the value of the cookie is going to be, and then you close parentheses.

  • You can also put a couple of other values in here.

  • You can put a timeout value, so if you only want the cookie doing to exist for a certain amount of time, you could put that in.

  • You can also put in a domain value is so that this cookie will only work on certain domains.

  • I left that out just to make this little bit easier for you.

  • So we have the name of the cookie and we have the value of the cookie.

  • And then, of course, we close out with the semi colon now.

  • So for this particular script, the first thing that we're going to dio is we're going to print.

  • And then basically, what we're going to do is we're just going to print what the current time is.

  • So double quotation marks, current time colon and then dollar sign time.

  • So this is going to print out current time colon and whatever the value is of time at this moment that we're close against Semi colon, then print break.

  • So all this is going to do is go on break to a new line and then we're going to print.

  • And so this we're now going to read from that cookie that we created a double quotation and we're gonna say previous time, visit Colon.

  • It's just basic text, basic string here.

  • And then this is how we're going to read from the cookie.

  • So we do dollar sign, underscore cookie.

  • All in uppercase letters, get remember, uppercase and lower case does matter.

  • So all uppercase letters, then we're going to do bracket, and then we're going to give the name of the cookie that we're looking for.

  • So s D.

  • Cookie s the cookie up here.

  • So basically, that's what we're looking for.

  • And then we're going to close the bracket, and then we're gonna close the double quotation marks.

  • So this is all going to print out.

  • So when you're going be reading from the cookie.

  • All you do is you dollar sign, underscore uppercase cookie, open the bracket and then give the name of the cookie that you're looking for the value.

  • And so what this will do is it will return in the value of whatever is currently in SD cookie.

  • That of course, you close the bracket, close it.

  • Oh, quotation marks.

  • Then you do the semi colon.

  • And of course, you finish off with closing out the PHP script.

  • And so, with this particular script, what it's going to dio is it's going to say what the current time is and that is going to say what the previous visit time Waas again.

  • This kind of starts flushing you out with some kind of like time tracking cookie.

  • Now that we're gonna d'oh, of course, we're going to go to Google Chrome, make sure everything works how he wanted to work.

  • One of things that we need to do is be nude in the clear browsing data.

  • So remember cookies are stored in that the Web browser itself.

  • So this isn't stored on the server.

  • This is stored somewhere else.

  • It's stored within the particular Web browser.

  • It's all we got to do here to make life easy, as I'm going to clear all data.

  • Now, if you're doing this on your particular computer, you want to screw things up.

  • You may just want uncheck the other things.

  • You only clear the cookies or the other thing.

  • If you're really playing with cookies, remember cookies generally our browser specific.

  • So if you have safari and chrome and fire Fox and Microsoft Edge, what I would suggest is, while you're playing around with PHP, you use a Web browser.

  • You don't normally you right?

  • So when you delete the cookies or what do you delete the data in the Web browser?

  • You're not deleting something that you may need in the future.

  • So with me, I personally always use safari So far is my Web browser.

  • One of reasons I do all these demonstrations on Google Chrome is so it doesn't mess up my configurations and safari, but Iost with that, we're gonna clear all the data, and then once all the data is cleared, then we're just going to go to silicon come forth.

  • This is and see This is the problem.

  • See, now I've got a type All of this end because it's no longer stored.

  • We're going to do cookie dot PHP now when I hit return.

  • Okay, So what we're going to see with this the first time we run this script is going to say the current time, and it's going to give us the current time in computer time.

  • So this is just computer time.

  • This is just kind of a placeholder dynamic thing to show you what's going on.

  • But this is what the computer time is.

  • Now you'll see is it says previous visit time, and that is now blank.

  • The reason being is I deleted my previous cookies, and so when I run this the very first time, the cookie has not yet been set.

  • So basically the first time this gets run, the cookie is set.

  • But then there's nothing to read from the cookie because it's the first time that it's been run, if that makes any sense.

  • So now if all we do is we go up and refresh when we refresh, will now be able to read from the cookie, So I refresh.

  • And now we see that the previous time is 15 blah, blah, blah.

  • 1181 So the current time is this.

  • And the previous time I was at the site is this Now what you'll see is when I refresh this again, the current time is going to end up being the previous time.

  • So see 12 to 7.

  • So I refresh.

  • So the previous visit time was at 1227 And now this is the current time.

  • And so this is just kind of a way you can look at those cookies and it shows you kind of like that time tracking mechanism to show you when somebody comes to the site, it will log the time that came to the site within the cookie, and then you can read from that later.

  • It's just that, you know, it's a basic little stupid script just to kind of give you an idea of the cookies.

  • The important things with the cookies is the set cookie.

  • And then what you're going to do is the name of the cookie and the value of the cookie.

  • And in order to read from the cookie.

  • It's a dollar sign.

  • Underscore cookie bracket, and then you give the name of whatever the cookie is, and then close the bracket that will give you at the value of the cookie to be a printout.

  • So that's the basic idea of setting and reading from cookies.

  • Ah, and it's really pretty simple.

  • Just just like everything else.

  • You have functions set cookie.

  • You give the cookie a name, you give the cookie of value and then you're able to read from it.

  • Ah, with dollar side underscored cookie Um, and then you give it the cookie name, and that's able to read the value.

  • And that that's that's basically how you're able to use cookies is really, is that easy?

  • Now again, there is a little bit more to cookies.

  • You can set a time out value for the cookie, so let's say you only want the cookie to last for a day.

  • Maybe you want people you want basically forced people to law against.

  • You'll notice that with some sessions, if you go to some websites, you know how if you go to a website a day after or three days after you long dead you'll still be able to access the website.

  • One of the reasons is basically that timeout function.

  • All of the cookies and everything are still set, so you're still able to access everything in the website.

  • Ah, that's good from a user perspective because it means it's easy to use website once you've logged in.

  • It is horrible from a security standpoint because basically, if you're already loaded the website and that session remains active, everything remains available.

  • Then.

  • If somebody then is able to access your computer three or four days from now, there'll be a beginning that website without using any kind of password or anything.

  • And so this is where it gets a little tricky with the whole idea of cookies, where again cookies themselves are very easy to set it very easy to read from.

  • The question then becomes things like time outs or you can set.

  • Do you want cookies to only work on particular domains or sub domains?

  • There's some different information there, and the problem that you run into is in order to make using your website easier for the end user, it may make it less secure, which can cause some problems at the end of the day again to using cookies to store variables.

  • This is a great way to reduce demand on your server.

  • Produce demand on accessing the database, that type of thing.

  • On the other hand, again, if somebody sets, let's say, 100 cookies and then you know their kid goes in trying to tune up their computer and deletes all their cookies for your website.

  • Well, that's 100 different variables that have been said that now they have to re go back and reconfigure and do things dynamically or do things manually.

  • So that's That's the kind of place where cookies can really become a problem.

  • But again, with all this kind of stuff, it's whatever you have to figure out.

  • So that's how you set and read from cookies and PHP.

  • That's why it matters.

So let's talk about how you can set and read from cookies using PHP.

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

B1 中級

PHP - 設置和讀取Cookies - setcookie() (PHP - Set and Read Cookies - setcookie())

  • 7 0
    林宜悉 發佈於 2021 年 01 月 14 日
影片單字