4

I am looking for a solution in PHP that will allow me to reference a PHP file that outputs javascript as a script includes in the header of my html page. The reason i'm looking to do this is because I have some dynamically generated javascript which I want the browser to cache.

I have done this before in ASP.NET by using a .ashx handler, but i'm not sure how to do it in PHP.

Here's a more detailed breakdown of what i'm trying to achieve.

<!DOCTYPE html>
<html lang="en">
        <script type="text/javascript" src="javascriptHandler.php"></script>
<head>
</head>

In the above example I want the javascriptHandler.php to respond with a javascript file once it's requested. The javascriptHandler.php needs to check the incoming request headers and determine whether the file already exists on the client and return the appropriate response.

I'm looking for any clean solution that will do what is described above or any links that will point me in the right direction. If there's a better way to include and cache dynamically generated javascript please post it here.

6
  • Why don't you just add some php code to that file? E.g. <?php if( /* condition /* ) { ?> <script> //JAVASCRIPT </script> <?php } ?> Commented Mar 2, 2015 at 8:31
  • I need the browser to cache the javascript. Currently the javascript is being referenced inline, but that is exactly what i'm trying to avoid. Commented Mar 2, 2015 at 8:32
  • Also if the javascript files gets requested after the page is already rendered, you'll need AJAX to do that. Commented Mar 2, 2015 at 8:32
  • The idea is that the javascriptHandler.php is requested exactly the same way a normal .js file is requested by the browser. Like i've said in my question, i've done this before with asp.net, but i could not find a PHP solution for this. Commented Mar 2, 2015 at 8:47
  • 2
    Your Code-Snippet is correct for your solution. Make sure you send the right Headers in your javascriptHandler.php (for cache-control and mime-type). Commented Mar 2, 2015 at 9:24

2 Answers 2

12

Put JavaScript Headers in PHP file

Keep your HTML page the same. Call it as a .php file:

<script type="text/javascript" src="javascriptHandler.php"></script>

In your javascriptHandler.php file, add this to the very top of the file:

<?php
header("Content-Type: application/javascript");
header("Cache-Control: max-age=604800, public");
?>

Then you can put your regular javascript below. All together it will look like:

<?php
header("Content-Type: application/javascript");
header("Cache-Control: max-age=604800, public");
?>

function bob(){
    alert('hello')
}
bob();

and your browser will treat your javascriptHandler.php file like a JavaScript resource. Normal caching rules will apply.

Sign up to request clarification or add additional context in comments.

Comments

5

URL Rewrites on the web server:

This will rewrite the requested .js filename to read your .php file instead.

This is known as an 'Internal' Rewrite so the web-browser will not know the file is actually .php. It will believe it is the .js file it requested.

Apache

If you are using an Apache webserver, you can make an .htaccess file in the same directory as your javascriptHandler.php file.

Inside .htaccess, put the following:

RewriteEngine On    # Turn on the rewriting engine
RewriteRule    javascriptHandler.js$  javascriptHandler.php [PT]

Nginx

You can do this with an Nginx web-server also, but you have to do it inside of your /etc/nginx/sites-available/yoursite config file for your server.

I am not going into how to do rewrites on Nginx because it is much more complicated than Apache. But it is basically the following:

server{rewrite javascriptHandler.js$  javascriptHandler.php last;}

Just to Mention it

After these changes, you will call the file in your HTML page as you would normally in order to cache it:

<script type="text/javascript" src="javascriptHandler.js"></script>

On your webserver, you will save the file as .php. This will allow you to use PHP to build dynamic JavaScript within the file.

/path/to/javascriptHandler.php

2 Comments

Thanks, gave you an upvote as i can see this as a potential solution. I'm trying to achieve this without having to modify the .htaccess, but i'll give this a shot if it does not work out.
@Jako Basson, I appreciate it. There's also the second answer above/below this one that lets you avoid messing with .htaccess

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.