The ...">

Can i load javascript from php file?

Can I do something like this?

<script src="/js/custom-user.php" type="text/javascript"></script>

The reason for this is because I want the .php file to die () when the user is not logged in, so that other visitors (not authenticated) cannot see what javascript looks like. Is it possible / safe to do this?

+5
source share
6 answers

Of course, as long as the php file refers to the corresponding header content-typeat boot time.

+3
source

, . -, , , <script>, . , , , js, , .

js PHP, :

header("Content-type: text/javascript"); 

// either readFile or custom stuff here.
echo "alert('i canz have data!')";
// or, if you're less silly
readFile('/path/to/super-secret.js');

, - CSS PHP (, ), get. :

rel="stylesheet" type="text/css" href="css.php?v=#FF0000">

#FF0000 , -. , imagemagick . , , .

+8

, , .

custom-user.php Content-Type:

header('Content-Type: text/javascript');

javascript:

readfile('script.js');
+2

, ... :

<?php
if ($loggedIn) { echo '<script src="/js/custom-user.js" type="text/javascript"></script>'; }
?>

. , javascript, HEAD SCRIPT

+1

, .

, JavaScript minifiers -. (, http://code.google.com/p/minify/)

0

, , , - modphp, php/javascript script.

0

All Articles