Skip to content

Instantly share code, notes, and snippets.

@mikedamage
Created December 30, 2011 17:49
Show Gist options
  • Save mikedamage/1540777 to your computer and use it in GitHub Desktop.
Save mikedamage/1540777 to your computer and use it in GitHub Desktop.
Combiner usage example
<!DOCTYPE html>
<html>
<head>
<title>Combiner Usage Example</title>
<!-- Stylesheet usage -->
<link rel="stylesheet" type="text/css" href="/combiner.php?files[]=css/blueprint.css&amp;files[]=css/screen.css" />
</head>
<body>
<h1></h1>
<!-- JavaScript usage -->
<script type="text/javascript" src="/combiner.php?files[]=js/jquery-latest.js&amp;files[]=js/main.js"></script>
<!-- This script requires the combo above it in order to run -->
<script type="text/javascript">
$(document).ready(function() {
$("h1").text("hello world");
});
</script>
</body>
</html>
@mwilc0x
Copy link

mwilc0x commented Dec 31, 2011

https://github.com/mjw56/cacheScript/blob/master/combine.php

So I cleaned up the script a little bit. I added checks to make sure that only one table is created, and that now duplicate rows of the same data are added. Also, it only fetches one row with mysql_fetch_assoc. I am still not quite sure how to have the script return as original JS/CSS inside a script tag. Right now, the script is just returning a blob of text. And also how to know when we want to store the data and when we want to use it?

So yeah, when you run the script it won't show anything on the page. It will just be blank as I commented out the echo statements that were for my own debugging.

@mwilc0x
Copy link

mwilc0x commented Dec 31, 2011

*inserted checks so that duplicate rows are not added

@mwilc0x
Copy link

mwilc0x commented Dec 31, 2011

So looking at your example above, it seems that the scripts will be cached the first time that this page is loaded. Then, after the initial load, each successive load after that it should search to find those particular files in the DB and return that text.

@mwilc0x
Copy link

mwilc0x commented Dec 31, 2011

the js/css files will be cached*

@mikedamage
Copy link
Author

The script returns JS/CSS inside the script tag by echoing the content as its output. That means if you visit the script in a browser, it should spit out JavaScript or CSS depending on what you put in ?files[]=. So it shouldn't echo any HTML, but it should echo the contents of the fileData column. return only works to push a value out of a function, but it doesn't automatically return it to the script's standard output unless you echo it.

Great job with the IF NOT EXISTS clause! I was hoping you'd figure out how to do that in SQL instead of using PHP. The objective in any database backed script is to let the database do most of the work. That's sort of a preparation for true MVC, where your PHP layer is there to grab content from the database and handle returning it as output, but the database does most of the heavy lifting (filtering, sorting, JOINing, etc.).

Anyway, just change the final line of your script to echo $fileText; and you'll have a testable script! One good next step would be to take a look at lines 96 and 114 and figure out how someone could use SQL injection to pwn your database.

@mwilc0x
Copy link

mwilc0x commented Dec 31, 2011

Ok, great. I did the changes and pushed backed to GH. I will have to explore the security vulnerabilities then. I know that SQL injection is a major problem and is very easy so I wouldn't want to expose my data like that. I will have to do some reading on that as well. Anyways, I def took a lot away from this especially when I had to hunt for bugs when I kept breaking the code.

@mwilc0x
Copy link

mwilc0x commented Dec 31, 2011

Thanks for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment