note 124089 deleted from function.setcookie by cmb

From: Date: Fri, 02 Aug 2019 06:40:38 +0000
Subject: note 124089 deleted from function.setcookie by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-211771@lists.php.net to get a copy of this message
Note Submitter: Venky Madurai ---- In order to read a set cookie, there needs to be a 2nd server call, but you could work with it :-) Let us say visitor lands on page.html. On the contents served by page.html, let us say that something needs to be done depending on whether the cookie has been set or not. One can always do a http redirect with a location header and add a GET parameter. But then, that page may get indexed on search engines, and that gives rise to more complications, creating additional headaches. So, simple solution, as an example: a. let us say that you have "some content" in page.html that is cookie dependent. b. move this content to a separate file that has got just "some content" in it, let us call this "some_content.html" c. in page.html, have an empty div with an id d. in page.html, include the javascript code for jquery. e. in page.html, load "some_content.html" via jquery into the div created in "step c" above that's it! No reload needed, and on the "first" access to the page, you will know if the visitor has cookies enabled or not and send "some content" accordingly This creates headaches. Simple solution: do something from inside the page that triggers a http call from the browser. examples: Assuming "some_content.html" is in the root html folder like public_html on apache ______________________________________________ <?php session_start(); ?> <!-- assuming defaults... cookies enabled for php session --> <html> <head> <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script> $(document).ready(function(){ $("#some_content").load("/some_content.html", function(responseTxt, statusTxt, xhr){ if(statusTxt == "success") console.log("file loaded successfully!"); if(statusTxt == "error") console.log ("file Error: " + xhr.status + ": " + xhr.statusText); }); }); </head> <body> <p>lorem ipsum....</p> <div id="some_content"></div> </body> </html> ------------------------------------------------------------ contents of some_contents.html (just check if the cookie you have exists: (session_start with default cookie settings creates a PHPSESSID cookie) <?php if($_COOKIE["PHPSESSID"] != null): ?> Browser does not support cookies <?php else: ?> Browser supports cookies, at least the session cookies are supported <?php endif; ?>

« previous php.notes (#211771) next »