Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP get cookie by value
    primarykey
    data
    text
    <p>I've recently been trying to make this little modification on my website. Basically logged in users can add games to their favorite, and not logged in visitors will be directed to the register page.</p> <p>Although I thought I could try and work on my PHP a little bit and enable visitors to add games to their favorites too. (Games being flash games)</p> <p>So I made this JS script that will allow the user to click on an image, this one checks if the cookie already exists, if not it will add it, and if it does exist, it will remove the cookie (an image changes from "Add to favorites" to "Remove from favorites"</p> <p>Since there can be many different favorited games, I cannot use a simple cookie name, so I set it up like this: xx1200=set (so there is 2 x's, then the game id which makes the cookie title, and then the "set" being the cookie value. All the favorited games will have the same value, but different names, being their ID with xx in front.</p> <p>Now to print the cookies on another page, displaying their favorited games, I added PHP on a page so basically get the cookie, but there resides my problem: how to get a cookie name by its value (normally you will print <code>$_COOKIE["set"];</code> but now my cookie name changes.</p> <p>I use <code>print_r($_COOKIE);</code> to print the whole cookie, and I see my <code>[zz800] =&gt; set</code> in the array, but I cannot figure out how to get the ID (being 800 in the example above) to then get the game(s) corresponding to the given ID.</p> <p>Any help would be greatly appreciated! Thanks you:)</p> <h3>Update:</h3> <p>This is the code I have for now, been working on and off for the last couple hours, right now it does work alright, I didnt add the PHP tho, just the javascript.</p> <p>My only problem is when I try to delete 1 entry, it will delete the whole cookie, so I cannot seem to find where to fix that. Another issue would be to get the IDs out of the array, if anybody could help me there, it would be appreciated.</p> <p>Here's my code:</p> <pre><code>&lt;?php $globalCookie = '629'; ?&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt; &lt;title&gt;Document sans titre&lt;/title&gt; &lt;script&gt; function createCookie(value) { if(document.cookie.search(value)==-1){ if(document.cookie.indexOf('games') == -1) { document.cookie = 'games' + '=' + ',' + value + '; expires=Fri, 27 Jul 2011 02:47:11 UTC; path=/'; } else { var deagle = document.cookie; document.cookie = deagle + ',' + value + '; expires=Fri, 27 Jul 2011 02:47:11 UTC; path=/'; } } else { // Removes the cookie (making the clear cookie problem most probably document.cookie = 'games' + '=' +escape( value ) + '; expires=Fri, 27 Jul 2001 02:47:11 UTC; path=/'; } } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;script&gt; var globalCookie = &lt;?=$globalCookie?&gt;; &lt;/script&gt; &lt;?php $stuff = $_COOKIE["games"]; $game = $globalCookie; $pos = strpos($stuff, $game); if ($pos === false) { ?&gt; &lt;a href='javascript:;'&gt;&lt;img id="mygame1" src='images/add_mygames.gif' border="0" onClick="createCookie(globalCookie); (this.src=='images/add_mygames.gif')?(this.src='images/remove_mygames.gif'):(this.src='images/add_mygames.gif');"&gt;&lt;/a&gt; &lt;? } else { ?&gt; &lt;a href='javascript:;'&gt;&lt;img id="mygame1" src='images/remove_mygames.gif' border="0" onClick="createCookie(globalCookie); (this.src=='images/remove_mygames.gif')?(this.src='images/add_mygames.gif'):(this.src='images/remove_mygames.gif');"&gt;&lt;/a&gt; &lt;? } ?&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>I use the $globalCookie as a replacement for my games IDs for now.</p> <p>Thanks again for any help!</p> <p>Also the cookie that the above makes looks like:</p> <pre><code>games=,621,256,637,57354 </code></pre> <p>Only way I find to work with the explode(), since if the comma is after the number, it will not count the first one</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload