我正在使用 PHP/MySQL 使用动态 css (style.php) 来设置 Web 应用程序的样式。
MySQL 值由 URL 确定:
$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
if($url == "this") $style = "blue";
else( $style = "red"; )
我似乎遇到的问题是 style.php 使用:
header('Content-type: text/css');
这会导致 $url 等于:“http://”,同时忽略 style.php 文件之外分配的任何其他变量。
有谁知道如何让这些 $_SERVER (和其他)变量工作?
这是完整的代码
$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; // current URL
$key = true;
while($key){
mysql_select_db($database, $connection);
$query_rsTheme = "
SELECT s.name, s.url, t.name as theme, t.colour, t.hover
FROM db_site as s
INNER JOIN db_theme.theme as t ON t.name = s.theme
WHERE s.url = '$url'";
$rsTheme = mysql_query($query_rsTheme, $connection) or die(mysql_error());
$row_rsTheme = mysql_fetch_assoc($rsTheme);
$totalRows_rsTheme = mysql_num_rows($rsTheme);
if($totalRows_rsTheme == 1){ // sucessful match
$key = false;
$site = $row_rsTheme['name'];
$theme = $row_rsTheme['theme'];
$default_state = $row_rsTheme['colour'];
$hover_state = $row_rsTheme['hover'];
}
$tokens = explode('/', $url);
$remove = $tokens[sizeof($tokens)-2]."/";
$url = substr($url, 0, strpos($url, $remove));
}
header('Content-type: text/css');
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
$stylesheet = 'style.css';
$content = preg_replace('/\$([\w]+)/e','$0',@file_get_contents($stylesheet));
echo $content;