Best practice for hosting javascript files for user authentication?

I have a web application where most of the functions are in the javascript file, and I'm going to introduce a version of the application in which registered users will have access to more functionality.

Again, the extra functionality is just the extra functionality in the javascript file.

I plan to do the following:
- link pro_script.js if the user is logged in,
or link to normal_script.js if the user is not logged in,
in the page header using user authentication with php.

I was wondering if this is the best way to approach this situation?

I have a problem that pro_script.js remains available in the javascripts folder, and it would be possible to write a script or plugin that loads pro_script.js instead of normal_script.js.

+3
source share
3 answers

You may have your own HTML code to call my_script.phpinstead of my_script.js. This PHP file simply displays your JS depending on the state if the user is registered or not.

+7
source

You can hide pro_script.js behind a PHP script - it will check the user account, and if the user is "premium", then it displays the contents of pro_script.js, otherwise - an empty string. Remember to set the correct headers (content type and caching)

+2
source

@Adnan, .

my_script.php :

<?php 
session_start();
header("Content-type: application/x-javascript";);

if (!empty($_SESSION['PRO_USER'])) {
    echo file_get_contents("js/pro_script.js");
} else {
    echo file_get_contents("js/normal_script.js");
}

exit;
?>
+2
source

Source: https://habr.com/ru/post/1790726/


All Articles