zang8027 Posted February 12, 2009 Share Posted February 12, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/144961-solved-requestbrowser/ Share on other sites More sharing options...
rhodesa Posted February 12, 2009 Share Posted February 12, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/144961-solved-requestbrowser/#findComment-760681 Share on other sites More sharing options...
zang8027 Posted February 12, 2009 Author Share Posted February 12, 2009 thank you i will try that. Do i need to put noscript tags inside the javascript block? Quote Link to comment https://forums.phpfreaks.com/topic/144961-solved-requestbrowser/#findComment-760683 Share on other sites More sharing options...
rhodesa Posted February 12, 2009 Share Posted February 12, 2009 you can put them anywhere on the page...if JavaScript is disabled, it will show what's inside, otherwise, it will stay hidden Quote Link to comment https://forums.phpfreaks.com/topic/144961-solved-requestbrowser/#findComment-760685 Share on other sites More sharing options...
zang8027 Posted February 12, 2009 Author Share Posted February 12, 2009 cool, got it to work. Lol @ macgyver in your avatar. Quote Link to comment https://forums.phpfreaks.com/topic/144961-solved-requestbrowser/#findComment-760691 Share on other sites More sharing options...
gizmola Posted February 12, 2009 Share Posted February 12, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/144961-solved-requestbrowser/#findComment-760694 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.