note 124089 added to function.setcookie
| From: | VenkyMadurai@php.net | Date: | Fri, 02 Aug 2019 06:03:20 +0000 |
| Subject: | note 124089 added to function.setcookie | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-211770@lists.php.net to get a copy of this message | ||
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; ?>
----
Server IP: 208.43.231.9
Probable Submitter: 119.81.81.244 (proxied: 106.51.18.144)
----
Manual Page -- http://php.net/manual/en/function.setcookie.php
Edit -- https://master.php.net/note/edit/124089
Del: integrated -- https://master.php.net/note/delete/124089/integrated
Del: useless -- https://master.php.net/note/delete/124089/useless
Del: bad code -- https://master.php.net/note/delete/124089/bad+code
Del: spam -- https://master.php.net/note/delete/124089/spam
Del: non-english -- https://master.php.net/note/delete/124089/non-english
Del: in docs -- https://master.php.net/note/delete/124089/in+docs
Del: other reasons-- https://master.php.net/note/delete/124089
Reject -- https://master.php.net/note/reject/124089
Search -- https://master.php.net/manage/user-notes.php