Saturday, December 31, 2011

Protect CSS File using PHP

Leave a Comment
is it possible? of course..some of you might saw this before.
But for some readers who didn't i will show you how.
here's the code.


ingredients:
2 php pages (index.php,css.php)

***** index.php ****** 

<?php
session_start();
header("Cache-Control: no-cache, must-revalidate"); 
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past 
$_SESSION['css']="key"; /* this is the session value, you can change the word "key"
to any word you wish.*/
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="/css.php">
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>

******* css.php *******
<?php
session_start();
header("Cache-Control: no-cache, must-revalidate"); 
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past 
$key=$_SESSION['css'];
if($key!='key') // if the "$key" is not equal to session value,the word "Protected Page" will appear
{

die('Protected Page');

} // else, the css code will extract 

session_destroy();  // destroy the curent session 

?>

body
{
color:white;
background:black;
}

// you can add more css codes here
****************************************************

when someone tries to access css.php the word "protected page" will appear instead of css code.like this one:






but if the visitor access the index.php it will look like this:




you can also use this experimental trick to other file such as xml.

This is not the best solution, but always try to do an  experiment. perhaps you can make a better solution(s) than this article. 


Post by:Pinoyphp

0 comments:

Post a Comment