Viewed   70 times

Is there a way to check if JavaScript is enabled with PHP? If so, how?



No, that is not possible, because PHP is a server side language, it does not access the client's browser in any way or form (the client requests from the PHP server).

The client may provide some meta info through HTTP headers, but they don't necessarily tell you whether the user has JavaScript enabled or not and you can't rely on them anyway,

Saturday, November 5, 2022

If you're using mod_php, you can use apache_get_modules(). This will return an array of all enabled modules, so to check if mod_rewrite is enabled, you could simply do

in_array('mod_rewrite', apache_get_modules());

Unfortunately, you're most likely trying to do this with CGI, which makes it a little bit more difficult.

You can test it using the following, though

strpos(shell_exec('/usr/local/apache/bin/apachectl -l'), 'mod_rewrite') !== false

If the above condition evaluates to true, then mod_write is enabled.

Tuesday, September 13, 2022

You can detect it, but it isn't pretty.

First, you need a new controller with an action that updates a timeout in the session:

class JavascriptController < ApplicationController
  def confirm
    session[:javascript_updated] =

Next you need to include a javascript action in all your pages so this controller action is called on each page load. The easiest way is to include it on a "javascript-confirm.js" file included in your layout (on this particular example I used Prototype's Ajax.Request, so you need to have it included on your javascripts, too):

function confirmJavascript()
    // Assuming you are using Prototype
    new Ajax.Request('/JavascriptController/confirm');

setInterval(myTimeoutFunction, 10000); // invoke each 10 seconds

This will invoke the confirm action in all your page views. Finally, you have to control how much time did it pass since your last confirmation in your application controller.

class ApplicationController < ActionController::Base


before_filter :prepare_javascript_test

def prepare_javascript_test
  if (session[:javascript_updated].blank? or - session[:javascript_updated] > ApplicationController::JAVASCRIPT_TIME_LIMIT)
    @javascript_active = true
    @javascript_active = false


You will now have a variable called @javascript_active in all your controllers.

It should work even when the user activates/deactivates javascript, with a precision of 10 seconds. It might not work if some of your pages take longer than 10 pages to load (i.e. with lots of images). Increase the time limit in that case (on applicationcontroller and your javascript)

Disclaimer: I haven't tested this code, some bugs might be lurking - but it should point you on the right direction.

Monday, August 29, 2022

If you don't want to use javascript, you can handle it via php. Take a look at this lib: And then you could do something like:

include 'Mobile_Detect.php';
$detect = new Mobile_Detect();

if ($detect->isMobile()) {
    header('Location: yourpage.php');
Friday, October 21, 2022

Cookies are not the way to transfer variables between client and server. you should append key/variables pairs to your request URL using either a get (querystring) or post method.

jQuery ajax example;

$.get('' + NewCookieValue);
Monday, October 31, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :