0

I'm using a PHP/MySQL to style a web app using dynamic css (style.php).

The MySQL values are determined from the URL:

$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

if($url == "this") $style = "blue";
else( $style = "red"; )

The problem I seem to be having is that the style.php uses:

header('Content-type: text/css');

This causes $url to be equal to: "http://", also any other variables assigned outside of the style.php file are ignored.

Does anyone know how to get these $_SERVER (and other) variables to work?

Here is the full code

$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;
11
  • How is style.php invoked? Commented Aug 12, 2013 at 8:36
  • what is purpose of style.php? Commented Aug 12, 2013 at 8:36
  • if($url == "this"){ $style = "blue";} else { $style = "red";} Commented Aug 12, 2013 at 8:38
  • <link rel="stylesheet" href="/include/version-3/css/style.php" type="text/css" media="screen, projection"/> Commented Aug 12, 2013 at 8:38
  • I can't understand what your problem is but I can assure you that the header() function does not write anything in the $_SERVER array. Commented Aug 12, 2013 at 8:40

3 Answers 3

1

You mention several times that $_SERVER is empty but I suspect you don't really test it:

print_r($_SERVER);

Whatever, your style.php script assumes that certain global variables exist (namely $database and $connection). If you've really posted the complete script, you never define them.

You also mention:

any other variables assigned outside of the style.php file are ignored.

Of course. That's how PHP works: each script is independent. Thankfully, style.php will not pick variables from any other random script that runs on the same server.

My advice is:

  1. Enable full error reporting. It's clear that you aren't seeing notices and possibly warnings and errors.

  2. Test the script separately. Load http://example.com/include/version-3/css/style.php in your browser and see the generated code, rather than possibly relying on styles showing up in your HTML.

Sign up to request clarification or add additional context in comments.

1 Comment

problem found: style.php is being accessed through http://, so the server variables were being set, i was calling the $url variable at a later stage where the $tokens were being removed. I added a landing page parameter to style.php?page=$currentPage; which solves my problem. sorry for the confusion.#]
0

You could check if the URI matches certain characters

if (strpos($_SERVER['REQUEST_URI'], 'this') !== FALSE ){
    $style = "blue";
} else {
    $style = "red";
}

This is particularly helpful if the file you are using is in fact an include into another file.

Comments

0

I believe the problem is not what you're describing. So long as style.php is accessed via http, then the $_SERVER variables will be set.

There is, however, some syntax error in the code you described:

if($url == "this") $style = "blue";
else( $style = "red"; )  // Incorrect syntax

The right way to write that would be:

if ($url == "this") { // $url will _never_ be "this"
    $style = "blue";
} else {
    $style = "red";
}

Edit: There's some funky code when evaluating the MySQL results:

$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
    ...
}

You should replace it with:

if($row_rsTheme = mysql_fetch_assoc($rsTheme)){ // sucessful match
    ...
}

That way, there will be success even if there's more than one result.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.