?
Solved

How to detect Javascript is disabled or not through php

Posted on 2014-08-18
11
Medium Priority
?
194 Views
Last Modified: 2014-11-05
Please let me know any class or method to know that on client machine Javascript is disabled or not through PHP
0
Comment
Question by:Swatantra Bhargava
  • 5
  • 3
  • 2
10 Comments
 
LVL 43

Expert Comment

by:Rob
ID: 40267413
You can't.  PHP is a server side language and javascript is client side.  You use html (<noscript> tags) in your page to detect if javascript is available or not.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40267454
As a practical matter, what Rob says is correct, however you can make some more effort and get to an answer.  For example, you can make an AJAX request on page load and the AJAX script can start the PHP session.  Then the browser can redirect with a meta-refresh tag.  If the redirect target script finds that the session is present, you know with moderate certainty that JavaScript was working on the browser, since it was able to make the AJAX request.  And you can be reasonably sure that the browser is accepting and returning cookies since the PHP session uses cookies most of the time.

But now to the order of events... This article explains the stateless nature of the HTTP client/server relationship, and you need to understand this if you want to build web applications.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/A_11271-Understanding-Client-Server-Protocols-and-Web-Applications.html

The client makes a request, then the server makes a response.  The server response contains all of the HTML, CSS, JavaScript, and data in the response.  And once this is sent the server disconnects and goes to sleep.  Nothing else happens until the next request.  So upon the first client request there is no data sent to tell the server if the client runs JavaScript.

As a practical matter all browsers today accept and return cookies and run JavaScript.  You cannot use Facebook, Twitter, Google or Amazon without cookies and JavaScript.  But that aside, your server URL may be blocked by default, and it's worth knowing about this.
0
 
LVL 43

Expert Comment

by:Rob
ID: 40267466
Thanks for the clarification Ray.  

A question like this deserves an answer like you've given.  While we all know it can't be done, you've managed to capture that well in words.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Author Comment

by:Swatantra Bhargava
ID: 40271890
I have resolved this by making a class in PHP and called where I needed (top / first site page). I just set the session in that class for normal calling and compare session on the page where I required.

Thank you for your help.
0
 
LVL 43

Expert Comment

by:Rob
ID: 40271946
How does that determine if JavaScript is enabled?
0
 
LVL 7

Author Comment

by:Swatantra Bhargava
ID: 40278650
I've requested that this question be closed as follows:

Accepted answer: 0 points for Swatantra Bhargav's comment #a40271890

for the following reason:

The problem which I am facing, is resolved by my on which I have consumed at least 20 hours. I did this for Crawlers because they crawl site contents in the JS disabled mode.

Its very challenging work for me and finally I got success.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40278651
Instead of closing the question without any useful information, please post the solution you found.  Then you can accept your own answer as the solution, and by doing so you will contribute to the E-E knowledge base.  Thanks!
0
 
LVL 7

Accepted Solution

by:
Swatantra Bhargava earned 0 total points
ID: 40412554
Sorry Guys... I am bit late to submit the code which I implemented.

Here in below what I have implemented:

Put below code in head
<script type="text/javascript">
document.cookie = 'hasJS=0';
</script>

Put below code top of your index.php file:
require_once("class.jscheck.php");
$js = new jsCheck;
$js->checkJsByForm($_POST["javascriptField"]);
if($js->isJsActivated() === 1)
{
# After the call to checkJsByForm() is done we use the function isJsActivated to see if JS is activated or not.
# The function returns true if activated and false otherwise.
$_SESSION['isJSEnabled'] = 1;
$_COOKIE['hasJS'] = 1;
}
class.jscheck.php
0
 
LVL 7

Author Comment

by:Swatantra Bhargava
ID: 40416905
Ray Please find below code of class.jscheck.php

<?php
class jsCheck {
	public function __construct() {
		if(!isset($_SESSION)) {
			session_start();
		}
		if(!isset($_SESSION['javascriptSes'])) {
			$_SESSION['javascriptSes'] = 0;
		}
		if(!isset($_SESSION['loaded'])) {
			$_SESSION['loaded'] = 1;
		}
		unset($_SESSION['javascriptSes']);
		unset($_SESSION['isJSEnabled']);
		unset($_COOKIE['hasJS']);
		$_SESSION['javascriptSes'] = 0;
		$_SESSION['loaded'] = 1;
		$_COOKIE['hasJS'] = 0;
	}

	public $js = 0;
	public function checkJsByForm($javascriptField) {
		if(isset($javascriptField) && $javascriptField) {
			unset($_SESSION['javascriptSes']);
			$_SESSION['javascriptSes'] = $javascriptField;		
		}
		if($_SESSION['loaded'] < 2 && $_SESSION['javascriptSes'] != 1) {
			unset($_POST["javascriptField"]);
			$_SESSION['loaded']++;
			if(!isset($_POST["javascriptField"]))
			{
				define("SITE_URL", "yoursiteurl");
?>
	<form name="javascriptForm" id="javascriptForm" method="post" action="<?=SITE_URL.substr($_SERVER["REQUEST_URI"],1,strlen($_SERVER["REQUEST_URI"])-1);?>" style="display:none;">
		<input name="javascriptField" type="text" value="1" />
	</form>
    <script type="text/javascript">
		document.javascriptForm.submit();
	</script>
<?
			}
			unset($_SESSION['javascriptSes']);
			unset($_SESSION['loaded']);
		}
		$this->js = $javascriptField;
	}

	public function isJsActivated() {
		if($this->js == 1) {
			$_COOKIE['hasJS'] = 1;
			$_SESSION['isJSEnabled'] = 1;
			return 1;
		}else {
			return 0;
		}
	}
}
?>

Open in new window

0
 
LVL 7

Author Closing Comment

by:Swatantra Bhargava
ID: 40423549
I resolved the issue as mentioned as above.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month14 days, 17 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question