Jump to content
JIHAD

Tell PHP that JS is not enabled.

Recommended Posts

Nu stiu daca s-a mai discutat despre asta dar aici este exemplul meu. Asa cum stiti PHP este server-side si JS este client-side. PHP nu are cum sa stie ce este pe PC-ul vostru decat daca-i spuneti voi. Dar daca JS este dezactivat din browser, cum ii spuneti?

In fine, am pus cap la cap un exemplu, pe care-l voi folosi in productie. Exemplul consta in doi pasi, unu, pagina de login, unde userul se autentifica si tot aici aflam si daca JS este activat. Al doilea pas consta in verificarea raspunsului dat din prima pagina, cea de login. Daca raspunsul exista, inseamna ca JS este activat si putem afisa pagini generate dinamic cu suport JS, altfel afisam pagini exclusiv PHP.

Pagina de login are url index.php?page=login din care trimitem un ajax request. Daca JS este activ, atunci requestul va functiona si stocam in sesiune o variabila JS.

Pagina de continut are url index.php?page=content. Aici verificam daca variabila din sesiune exista. Daca exista inseamna ca ajax-ul a functionat corect, deci afisam raspunsul corespunzator.

Sper ca va este de folos.


<?php
session_start ();
if (isset ( $_GET ['page'] )) {
if ($_GET ['page'] == "login") {
echo <<<EOT
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<meta name="description" content="">
<meta name="author" content="fgdfgsdfgsd">
<link rel="icon" href="images/logo.ico">

<title>dfgsdfgsdf</title>
<script src="external/jquery/jquery.js"></script>
</head>

<body>
<script>
$.ajax({
type: "POST",
dataType: "html",
url: "index.php",
async: true,
data: {
JS: 'true',
},
success: function(response) {
},
error: function() {
}
});
</script>
</body>
EOT;
} else if ($_GET ['page'] == "content") {
if (isset ( $_SESSION ['JS'] )) {
echo "JS enabled";
} else {
echo "JS not enabled";
}
}
}

if (isset ( $_POST ['JS'] )) {
$_SESSION ['JS'] = "true";
}
?>

  • Upvote 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...