Jump to content

Using document.cookie


Gilbert

Recommended Posts

I'm just starting to use cookies for personalizing my website and I need a little clarification about the DOM.  When a cookie is sent along with the request for a website, does it become available to the whole site (like every page you might load) or just to a specific page included in your website.   I am planning on using the setcookie, getcookie and checkcookie functions from w3 - can I put them in any page of the website or do I have to use them from the initial page of my site?  Any guidance would be appreciated.

Link to comment
Share on other sites

Hey justsomeguy,  thanks.  But I really am just starting out - can you give me a boilerplate of what the directory path would look like and to what it is referring.  I'm guessing it would look like this:   "www.mywebsite.com/nameofsomefolder/nameofanotherfolder/"    Am I close?  I was wondering how that 'path="/"' fit into the scheme of things.   But if I leave out the path definition will the cookies be available to the whole site to use on whatever page and in whatever js or php I want to write?     Thanx,  Gil

 

Link to comment
Share on other sites

The path part of a URL starts after the domain name.  When the path is just "/" it means to send it to any path.  If you made the path, for example, "/admin", then the cookie would only be sent to any request under "domain.com/admin".  

Link to comment
Share on other sites

O.I.C.   I got it.  I have another question about cookies since I've been trying to find the best way to 'get' and 'check' them.  I see some pretty complicated examples of cookies and what a program would have to do to decipher them, but if you are setting the cookies yourself, and supposedly no one else can deliver a cookie with your website, shouldn't you be able to make it as simple as you want.   Of course I realize I'm dreaming about a perfect world, but is the coding & decoding of the cookies string really necessary?  I've seen code where it starts with 'document.cookie' and works just fine.  Can somebody show me the simplest and easiest way to set & get just a singal name-value pair on a simple website?  Thanx, I'm getting frustrated @#$%^&*(), Gil

 

Link to comment
Share on other sites

The document.cookie property is just a long string with all the cookies and their properties in it. The only way to get a cookie out of it is using string manipulation functions to get the pieces of the string that you need. There is no easier way to do it.

If you use the localStorage API instead of cookies you'll have an easier time. Here's a page describing how to use the localStorage API: https://www.w3schools.com/jsref/prop_win_localstorage.asp

Link to comment
Share on other sites

Thanks for the input, Ingolme.  I had just started reading about local storage and had come to the same conclusion, that it would be easier to use.  I am experimenting with it now to make sure I understand the whole concept.  I was learning about session vars also and I wanted to confirm  that the Session variables you set with php can be used by javascript using setItem & getItem, and if they're set with js you can retrieve them with $_SESSION.  I haven't started experimenting with them yet, but it appears they are the same variable, just accessed differently.   Do I have that concept correct.   I would use that concept to retrieve data from a database table and store it in a $_SESSION with php, and then use sessionStorage.getitem to set vars in js and vice versa.   Thanks for helping,  Gil

Link to comment
Share on other sites

I was learning about session vars also and I wanted to confirm  that the Session variables you set with php can be used by javascript using setItem & getItem, and if they're set with js you can retrieve them with $_SESSION.

No, the PHP session is not available to Javascript, and Javascript cannot modify the PHP session.

Link to comment
Share on other sites

Hey justsomeguy,  thanx for the response.  I guess it was just wishful thinking - I see now that the $_SESSION vars are probably kept on the server and the js vars are put in the browser.  I'm learning a lot and really appreciate all the help from all you guys!!   Gil

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...