Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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;
share|improve this question
    
How is style.php invoked? –  hank Aug 12 '13 at 8:36
    
what is purpose of style.php? –  VIVEK-MDU Aug 12 '13 at 8:36
    
if($url == "this"){ $style = "blue";} else { $style = "red";} –  Vector Aug 12 '13 at 8:38
    
<link rel="stylesheet" href="/include/version-3/css/style.php" type="text/css" media="screen, projection"/> –  user715105 Aug 12 '13 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. –  Álvaro G. Vicario Aug 12 '13 at 8:40

3 Answers 3

up vote 1 down vote accepted

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.

share|improve this answer
    
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.#] –  user715105 Aug 12 '13 at 9:35

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.

share|improve this answer

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.

share|improve this answer
    
yes, apologies, the syntax is wrong, this is corrected. –  user715105 Aug 12 '13 at 8:43
    
But still no cigar? Can you show us the whole php code? –  Nicklas Nygren Aug 12 '13 at 8:44
    
original post updated –  user715105 Aug 12 '13 at 8:53
    
sweet, thanks for the info –  user715105 Aug 12 '13 at 9:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.