How to detect Javascript is disabled or not through php

Please let me know any class or method to know that on client machine Javascript is disabled or not through PHP
LVL 9
Swatantra BhargavaTechnical Specialist/DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobOwner (Aidellio)Commented:
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
Ray PaseurCommented:
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
RobOwner (Aidellio)Commented:
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Swatantra BhargavaTechnical Specialist/DeveloperAuthor Commented:
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
RobOwner (Aidellio)Commented:
How does that determine if JavaScript is enabled?
0
Swatantra BhargavaTechnical Specialist/DeveloperAuthor Commented:
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
Ray PaseurCommented:
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
Swatantra BhargavaTechnical Specialist/DeveloperAuthor Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Swatantra BhargavaTechnical Specialist/DeveloperAuthor Commented:
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
Swatantra BhargavaTechnical Specialist/DeveloperAuthor Commented:
I resolved the issue as mentioned as above.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.