Koobi
01-19-2005, 07:26 AM
Not that I really mind anyone viewing any CSS layouts I create but with all the ripping that's going around these days, I feel like I have to hide some things. So, I ended up writing this little PHP snippet that I would like to share with you guys...I'm sure this has done before but I can't find it on the web so I wrote my own.
How it works
What this snippet does is, it checks if either:
* the referrer is actually the X/HTML file that calls the CSS
* the user agent of what is accessing the CSS is an allowed one.
If the agent that is attempting to view the CSS is an allowed one, your CSS will display as usual, otherwise, in the place of a CSS file, an HTML-type page renders and you can display the message of your choice there and the use of X/HTML tags are permitted.
What is a user agent?
A user agent, to put it simply, is an identity value that browser types have. This identity value may vary among versions of browsers of one type (eg, Mozilla Firefox 1.0 has a different user agent from that of Mozilla Firefox 0.9 although they are similar but the user agent for MSIE greatly differs from the Mozilla user agents).
Instructions:
* The CSS file must have a .php extension so that it is parsed as PHP (or you can configure your server to parse CSS as PHP)
* The following code must be at the top of your CSS file:
<?php
/********************************************************\
* Please leave these comments intact.
*
* @author Housni Yakoob <baneATkoobi-studioDOTcom>
* @url http://www.koobi-studio.com/
* @date 2005-January-19
* @version 1.0.0
\********************************************************/
/************************************\
* Begin editing from here onwards. *
\************************************/
/**
* Allowed user agents.
* Every type of browser has a special ID called a user agent
* and all browsers that have the user agents within the array
* below can access the CSS.
*
* The user agent entered below is that of the CSS validator.
* see: http://jigsaw.w3.org/css-validator/
**/
$allowAgent = array('Jigsaw/2.2.3 W3C_CSS_Validator_JFouffa/2.0');
/**
* The absolute URI of the X/HTML that can access this CSS.
**/
$callingFile = 'http://mySite.com/myFolder/mySubFolder/';
/**
* The message to display when unauthorzed users attempt to
* view your CSS.
* You can use X/HTML tags here since the MIME type of the
* file is HTML (text/html)
**/
$errorMsg = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Error</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css" media="all">
<!--
body {font: bold 16px sans-serif; color: #F00; background: #fff; text-align: center;}
-->
</style>
</head>
<body>
You are unwanted here.
</body>
</html>
';
/***********************************\
* Stop editing from here onwards. *
\***********************************/
$prePend = reset(explode('/', end(explode('http://', $_SERVER['HTTP_REFERER']))));
$refUri = (substr($prePend, 0, 4) === 'www.') ? substr($prePend, 4) : $prePend;
if( ($_SERVER['HTTP_REFERER'] === $callingFile) || (in_array($_SERVER['HTTP_USER_AGENT'], $allowAgent)) )
{
header('Content-type: text/css');
}
else
{
header('Content-type: text/html');
die($errorMsg);
}
?>
Now, make sure that code is right at the top of your CSS. Not even a line break, or a blank space or a single character can be output to the browser. The PHP tag (<?php) must be the first character of the first line on the first column of your CSS or you will recieve an error telling you that headers have already been sent.
* Within your X/HTML document, simply link to the CSS file (with the PHP extension) as you would any other.
Example (assuming the name of the CSS file is style.css.php):
<style type="text/css" media="all">@import "style.css.php";</style>
Extra:
As you can see, my variable $allowAgent holds something called the User agent. That is the user agent of the CSS validator most of us are familiar with.
I have added that so that the validator also sees the CSS, otherwise the CSS will be hidden from the validator as well. If there are other validator's user agents that you would like to add to that list so they they can validate your CSS, simply enter their user agents. If you are unsure how to get the user agent of an agent, contact me via PM or by posting here.
Keep in mind that if someone really wants to view your CSS, they can. There are many ways...this might just keep the not so bright ones guessing :D
Hope this helps :)
Sorry for the long post :D
How it works
What this snippet does is, it checks if either:
* the referrer is actually the X/HTML file that calls the CSS
* the user agent of what is accessing the CSS is an allowed one.
If the agent that is attempting to view the CSS is an allowed one, your CSS will display as usual, otherwise, in the place of a CSS file, an HTML-type page renders and you can display the message of your choice there and the use of X/HTML tags are permitted.
What is a user agent?
A user agent, to put it simply, is an identity value that browser types have. This identity value may vary among versions of browsers of one type (eg, Mozilla Firefox 1.0 has a different user agent from that of Mozilla Firefox 0.9 although they are similar but the user agent for MSIE greatly differs from the Mozilla user agents).
Instructions:
* The CSS file must have a .php extension so that it is parsed as PHP (or you can configure your server to parse CSS as PHP)
* The following code must be at the top of your CSS file:
<?php
/********************************************************\
* Please leave these comments intact.
*
* @author Housni Yakoob <baneATkoobi-studioDOTcom>
* @url http://www.koobi-studio.com/
* @date 2005-January-19
* @version 1.0.0
\********************************************************/
/************************************\
* Begin editing from here onwards. *
\************************************/
/**
* Allowed user agents.
* Every type of browser has a special ID called a user agent
* and all browsers that have the user agents within the array
* below can access the CSS.
*
* The user agent entered below is that of the CSS validator.
* see: http://jigsaw.w3.org/css-validator/
**/
$allowAgent = array('Jigsaw/2.2.3 W3C_CSS_Validator_JFouffa/2.0');
/**
* The absolute URI of the X/HTML that can access this CSS.
**/
$callingFile = 'http://mySite.com/myFolder/mySubFolder/';
/**
* The message to display when unauthorzed users attempt to
* view your CSS.
* You can use X/HTML tags here since the MIME type of the
* file is HTML (text/html)
**/
$errorMsg = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Error</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css" media="all">
<!--
body {font: bold 16px sans-serif; color: #F00; background: #fff; text-align: center;}
-->
</style>
</head>
<body>
You are unwanted here.
</body>
</html>
';
/***********************************\
* Stop editing from here onwards. *
\***********************************/
$prePend = reset(explode('/', end(explode('http://', $_SERVER['HTTP_REFERER']))));
$refUri = (substr($prePend, 0, 4) === 'www.') ? substr($prePend, 4) : $prePend;
if( ($_SERVER['HTTP_REFERER'] === $callingFile) || (in_array($_SERVER['HTTP_USER_AGENT'], $allowAgent)) )
{
header('Content-type: text/css');
}
else
{
header('Content-type: text/html');
die($errorMsg);
}
?>
Now, make sure that code is right at the top of your CSS. Not even a line break, or a blank space or a single character can be output to the browser. The PHP tag (<?php) must be the first character of the first line on the first column of your CSS or you will recieve an error telling you that headers have already been sent.
* Within your X/HTML document, simply link to the CSS file (with the PHP extension) as you would any other.
Example (assuming the name of the CSS file is style.css.php):
<style type="text/css" media="all">@import "style.css.php";</style>
Extra:
As you can see, my variable $allowAgent holds something called the User agent. That is the user agent of the CSS validator most of us are familiar with.
I have added that so that the validator also sees the CSS, otherwise the CSS will be hidden from the validator as well. If there are other validator's user agents that you would like to add to that list so they they can validate your CSS, simply enter their user agents. If you are unsure how to get the user agent of an agent, contact me via PM or by posting here.
Keep in mind that if someone really wants to view your CSS, they can. There are many ways...this might just keep the not so bright ones guessing :D
Hope this helps :)
Sorry for the long post :D