Jump to content

Recommended Posts

I dont know if this is ASP, AJAX, or JS but I hear there is a code that finds out if cookies and javascript are enabled or not.

 

How would I add this code to check and then display yes or no in an IF?

 

is it like

 

if(Request.Browser.Cookies)
{
     var msg="cookies enabled"
     alert(msg);

}else{
     var msg="No cookies"
     alert(msg);
}

 

I am using PHP to display a page so I would like to say at the top, if cookies are enabled, load the page.. if not... display like.. Please turn on cookies.

 

 

Link to comment
https://forums.phpfreaks.com/topic/144961-solved-requestbrowser/
Share on other sites

for showing a message if JS is disabled, use:

<noscript>Please enable JavaScript</noscript>

once JavaScript is enabled, you can use it to test for cookies:

<script type="text/javascript">
if(!window.navigator.cookieEnabled){
  alert('You must enable cookies');
}
</script>

That code is javascript.  Hence it runs on the client.  If it's satisfactory for your solution, you could code using javascript and use the DOM to hide the form and display a box indicating that cookies are required. 

 

If you want to do a serverside solution, then it's more complicated.  To understand why you need to understand how cookies work.  When the browser makes a request the server sends a response to that request. 

 

Client Request -> to Server

Server Response -> to Client

 

Since this is the HTTP protocol, in each case the sender includes an HTTP Header, which is some control data.  Cookies (either the cookie data itself, or the request from the server to set a cookie, go in the Header.

 

So as you can see, the first chance the Server has to tell the Client to set a cookie, is when it responds to the client's request.  So for the server to actually determine whether or not the client actually set the cookie, the server needs the client to make another request.  How can it do that?  It can tell the client to redirect to another page (which also goes in the header usually via the "Location:" header.

 

THere are various approaches to this.  One of the best in PHP is to use PHP's built in session capability, with sessions configured to use cookies.  What you can then do is have some generic session code on your site that sets/checks session variables.  You can use this to set a session variable that indicates that cookies are working for that user.  This takes some planning because you don't want to send the client off in an infinite loop, so you typically use a url param like ?cookiecheck=1

 

Pseudocode of this:

 

1. start session

2. check cookie value.  If value not set AND $_GET['cookiecheck'] == 1  display an error -- sorry client you need cookies or this doesn't work 

2a.  else start session, set session cookie value  (perhaps $_SESSION['cookiesok'] = true; and redirect to SELF appending ?cookiecheck=1.

 

 

HTH.

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.