Check if JavaScript is enabled with PHP

perhaps a more simple option...

<html>
<head>
 <noscript>
   This page needs JavaScript activated to work. 
   <style>div { display:none; }</style>
 </noscript>
</head>
<body>
<div>
my content
</div>
</body>
</html>

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,


Technically no because as the other answers have said, PHP is strictly server-side, but you could do this...

In the PHP page on the server, output (a lot of HTML has been deleted for brevity)

<html>
   <head>
      <script type="text/javascript" src="jquery1.4.4.js"></script>
      <script type="text/javascript">
        $(document).ready(function(){
           $.get("myPage.php");
         });
      </script>
   </head>
 </html>

Then in myPage.php set a session variable to indicate the client supports JS

<?php
   session_start();
   $_SESSION['js'] = true;
?>

But really, just use <script></script><noscript></noscript> tags, much, much less effort...


//Here is a solution: //it works perfect

<?php

if(!isset($_SESSION['js'])||$_SESSION['js']==""){
  echo "<noscript><meta http-equiv='refresh' content='0;url=/get-javascript-status.php&js=0'> </noscript>";
   $js = true;

 }elseif(isset($_SESSION['js'])&& $_SESSION['js']=="0"){
   $js = false;
   $_SESSION['js']="";

 }elseif(isset($_SESSION['js'])&& $_SESSION['js']=="1"){
   $js = true;
   $_SESSION['js']="";
}

if ($js) {
    echo 'Javascript is enabled';
 } else {
    echo 'Javascript is disabled';
}

?>

//And then inside get-javascript-status.php :

$_SESSION['js'] = isset($_GET['js'])&&$_GET['js']=="0" ? "0":"1";
header('location: /');