动态CSS中的PHP变量
我正在使用PHP/MySQL使用动态css(style.PHP)设计web应用程序的样式 MySQL值由URL确定:动态CSS中的PHP变量,php,mysql,css,variables,dynamic,Php,Mysql,Css,Variables,Dynamic,我正在使用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
$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文件之外分配的任何其他变量
有人知道如何让这些$_服务器(和其他)变量工作吗
这是完整的代码
$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;
我相信问题不是你所描述的。只要通过http访问
style.php
,就会设置$\u服务器
变量
但是,您描述的代码中存在一些语法错误:
if($url == "this") $style = "blue";
else( $style = "red"; ) // Incorrect syntax
正确的写作方法是:
if ($url == "this") { // $url will _never_ be "this"
$style = "blue";
} else {
$style = "red";
}
编辑:在评估MySQL结果时,会出现一些古怪的代码:
$row_rsTheme = mysql_fetch_assoc($rsTheme);
$totalRows_rsTheme = mysql_num_rows($rsTheme);
if($totalRows_rsTheme == 1){ // sucessful match, but ONLY if there's only one row
...
}
您应将其替换为:
if($row_rsTheme = mysql_fetch_assoc($rsTheme)){ // sucessful match
...
}
这样,即使有多个结果,也会成功。您可以检查URI是否匹配某些字符
if (strpos($_SERVER['REQUEST_URI'], 'this') !== FALSE ){
$style = "blue";
} else {
$style = "red";
}
如果您正在使用的文件实际上是另一个文件中的一个包含项,则这一点特别有用。您多次提到
$\u服务器是空的,但我怀疑您没有真正测试它:
print_r($_SERVER);
无论如何,您的style.php
脚本假定存在某些全局变量(即$database
和$connection
)。如果你真的发布了完整的脚本,你永远不会定义它们
你还提到:
在style.php文件之外指定的任何其他变量都将被忽略
当然。PHP就是这样工作的:每个脚本都是独立的。谢天谢地,style.php
不会从同一服务器上运行的任何其他随机脚本中选取变量
我的建议是:
启用完全错误报告。很明显,您没有看到通知,也可能没有看到警告和错误
分别测试脚本。加载<代码>http://example.com/include/version-3/css/style.php
在浏览器中查看生成的代码,而不是依赖HTML中显示的样式
style.php是如何调用的?style.php的用途是什么?如果($url==“this”){$style=“blue”}否则{$style=“red”}我无法理解您的问题是什么,但我可以向您保证
header()
函数不会在$\u服务器
数组中写入任何内容。发现问题:style.php正在通过http://,因此,在设置服务器变量时,我在稍后的阶段调用$url变量,删除$token。我在style.php?page=$currentPage中添加了一个登录页参数;这解决了我的问题。抱歉搞混了