What code do I use to check for Cookie?

13 replies
Hi,

The code I am using only checks cookie name from the URL my browser is on.

How can I check for a cookie name no matter what URL it came from?

For example, if the cookie "popover" comes from "mindreality.com", how can I check for it even if it comes from "ubermanpower.com" or any other URL without being restricted to the URL that my browser is on?
#check #code #cookie
  • Profile picture of the author brutecky
    You cant with a server side script. That is from website A you can only read cookies that are from website A not from any other website:

    Same origin policy - Wikipedia, the free encyclopedia
    {{ DiscussionBoard.errors[8239917].message }}
    • Profile picture of the author MindReality
      What I mean is, if the browser (Firefox for e.g.) is already cookied with "popover" from "Ubermanpower.com" but right now it is viewing "MindReality.com", I want my code on "MindReality.com" to be able to check for the cookie "popover" even though it is from "Ubermanpower.com. Keep in mind that the browser is already cookied.

      How do I do that?

      My code is:

      Code:
      function getcookie(cookiename) {
       var cookiestring=""+document.cookie;
       var index1=cookiestring.indexOf(cookiename);
       if (index1==-1 || cookiename=="") return ""; 
       var index2=cookiestring.indexOf(';',index1);
       if (index2==-1) index2=cookiestring.length; 
       return unescape(cookiestring.substring(index1+cookiename.length+1,index2));
      
      function cookiecheck(){
      var currentvalue;
      var uservalue =1;
      var popoverdelay = 0;
      if(getcookie('popover') > 0){
      currentvalue = getcookie('popover');
      if(currentvalue == (uservalue+1))
      Signature
      Discover The Greatest Secrets Of The Mind And Reality That Will Get You Anything You Desire, Almost Like Magic! Visit: http://www.MindReality.com
      {{ DiscussionBoard.errors[8241779].message }}
  • Profile picture of the author seasoned
    Cookies are presented only for the site the browser is on, and to there. So if a user vists mysite.com, you can't get the cookies for google.com on mysite.com. Javascript is ALSO run from the site and subject to those constraints.

    Steve
    {{ DiscussionBoard.errors[8244958].message }}
  • Profile picture of the author bugtrack
    You can check and set cookies using php server side scripting i dont think without server side scripting you will be able to do it
    {{ DiscussionBoard.errors[8245479].message }}
    • Profile picture of the author MindReality
      Originally Posted by bugtrack View Post

      You can check and set cookies using php server side scripting i dont think without server side scripting you will be able to do it
      What is an example of how this work? The domains are on my server.
      Signature
      Discover The Greatest Secrets Of The Mind And Reality That Will Get You Anything You Desire, Almost Like Magic! Visit: http://www.MindReality.com
      {{ DiscussionBoard.errors[8246167].message }}
      • Profile picture of the author Brandon Tanner
        As brutecky and Steve have already mentioned, you can only check your visitors cookies that YOU have set. You can NOT check the cookies that your visitors have picked up from other websites (that would be a huge security issue if that was possible).

        So, if you're interested in doing the former...

        PHP Tutorial - Cookies

        You can also set/check cookies with Javascript, but PHP is more reliable, since a tiny percentage of your visitors will have Javascript disabled. <-- That's kind of a moot point though, because a certain percentage of your visitors are going to clear their cookies on a regular basis anyways (or set their browsers to block them altogether), so no matter what method you use, it's never going to be anywhere near 100% reliable.
        Signature

        {{ DiscussionBoard.errors[8246298].message }}
        • Profile picture of the author snoopie
          If it was possible to read a visitors cookies from another domain, you would be able to log into any website that your visitors are logged in to. Pretty huge security problem :/
          {{ DiscussionBoard.errors[8246862].message }}
  • Profile picture of the author wizwebtechno
    You can set cookie using any service side scripting you can use asp.net or php for setting up the cookies and then for the reading of the cookies
    {{ DiscussionBoard.errors[8249155].message }}
    • Profile picture of the author MindReality
      Originally Posted by wizwebtechno View Post

      You can set cookie using any service side scripting you can use asp.net or php for setting up the cookies and then for the reading of the cookies
      Can you show me an example of this?

      How can I set the cookie "popover" from one domain, and check for it on another?
      Signature
      Discover The Greatest Secrets Of The Mind And Reality That Will Get You Anything You Desire, Almost Like Magic! Visit: http://www.MindReality.com
      {{ DiscussionBoard.errors[8249410].message }}
      • Profile picture of the author seasoned
        Originally Posted by MindReality View Post

        Can you show me an example of this?

        How can I set the cookie "popover" from one domain, and check for it on another?
        Maybe there is a misunderstanding. When you set a cookie for a domain(ON THE SERVER), it is AUTOMATICALLY set for THAT DOMAIN! That is the domain they visit, that YOU control. And there is NO way to identify a domain, so the browser identifies it by the exact reference! So if you have mysite.com and www.mysite.com, they have DIFFERENT sets of cookies, EVEN IF THEY ARE THE SAME DOMAIN. If you want them to have the SAME cookies, the preferred way is to have a rewrite to change mysite.com to www.mysite.com, and set the cookies for www.mysite.com.

        You CAN setup javascript on the CLIENT to set cookies on different sites, but that is unethical! If they are affiliate sites, and they find out, you might even be SUED! It HAS happened before. Of course, they can EASILY frustrate it! AGAIN, it HAS been done before! ALSO, it has been tried SO many times that browsers now have code to disable it by default! It has been there for the past few versions.

        As far as reading? It can NOT be done on the server! It is NEVER passed to the server, so there is nothing for the server to read. Again, there are ways to disable it, but the most common ways, AGAIN, are frustrated, as MANY have tried them. AGAIN, if a company finds out, they may sue you!

        BTW there are TWO sets of cookies! Switching between them will give the vister a nasty warning, so they will KNOW if you switch between HTTP and HTTPS, or an HTTPS site switches between domains.

        Steve
        {{ DiscussionBoard.errors[8250901].message }}
        • Profile picture of the author MindReality
          Someone in digitalpoint forum said:

          "If you happen to own all the domains, then its a pretty easy thing to do. There are many ways you can share cookies between domains - something similar to what Google would do among it's services (gmail, youtube, etc..) or Microsoft's sites using live login.

          One of the ways you can do it is via an API, where the "master" domain set's the cookies/retrieves it and the "slave" domains request that cookie. This way itself can be done multiple ways, I personally find that using a distributed memory object caching system the fastest (something like Memcached)."


          I own all the domains btw. Does anyone know an example of the code of how to make this work?
          Signature
          Discover The Greatest Secrets Of The Mind And Reality That Will Get You Anything You Desire, Almost Like Magic! Visit: http://www.MindReality.com
          {{ DiscussionBoard.errors[8254669].message }}
          • Profile picture of the author Brandon Tanner
            Originally Posted by MindReality View Post

            How can I check for a cookie name no matter what URL it came from?
            Originally Posted by MindReality View Post

            I own all the domains btw.
            ^ Those are two completely different scenarios, obviously.

            IF you have control over all of the domains, there are some interesting answers at the following link (although I've never tried to do this, so I can't vouch for their effectiveness)...

            web applications - Cross-Domain Cookies - Stack Overflow
            Signature

            {{ DiscussionBoard.errors[8256333].message }}
          • Profile picture of the author seasoned
            Originally Posted by MindReality View Post

            Someone in digitalpoint forum said:

            "If you happen to own all the domains, then its a pretty easy thing to do. There are many ways you can share cookies between domains - something similar to what Google would do among it's services (gmail, youtube, etc..) or Microsoft's sites using live login.

            One of the ways you can do it is via an API, where the "master" domain set's the cookies/retrieves it and the "slave" domains request that cookie. This way itself can be done multiple ways, I personally find that using a distributed memory object caching system the fastest (something like Memcached)."


            I own all the domains btw. Does anyone know an example of the code of how to make this work?
            OK, WHERE is the question? We have ALL said you can read/write cookies on THAT domain. That IS what it is for.

            $%^&

            And HOW do you do it? How do you write an equation with an answer of 5? I did something kind of like this, by simply standardizing related tables.

            Steve
            {{ DiscussionBoard.errors[8257466].message }}

Trending Topics