Page 1 of 1

PHP Sessions w/ CSS File

Posted: Sun Aug 17, 2008 3:40 pm
by w0lf42
I'm using PHP Sessions to remember the browser name and version and it's working.

Code: Select all

include($_SERVER['DOCUMENT_ROOT'] . '/include/browser.php');
$uad = new User_Agent_Detector(false);
session_start();
$_SESSION['operatingSystem'] = $uad->get_os();
$_SESSION['browser'] = $uad->get_browser();
However, my style sheet (a PHP file) doesn't know what the session variables are.

Call to my Style Sheet:
<link rel="stylesheet" type="text/css" href="/include/css.php?theme=default" title="Default" media="screen" />

This is what I'm currently using in my CSS file (css.php):

Code: Select all

header("Content-type: text/css");
include($_SERVER['DOCUMENT_ROOT'] . '/include/browser.php');
$uad = new User_Agent_Detector(false);
$browser = $uad->get_browser();
preg_match('/(?<name>[\w ]+) (?<digit>\d+)/', $browser, $matches);
include $_SERVER['DOCUMENT_ROOT'] . '/styles/themes/' . $_GET['theme'] . '/reset.css';
include $_SERVER['DOCUMENT_ROOT'] . '/styles/themes/' . $_GET['theme'] . '/structure.css';
include $_SERVER['DOCUMENT_ROOT'] . '/styles/themes/' . $_GET['theme'] . '/presentation.css';
if (trim($matches['name']) == 'Internet Explorer') {
    // load IE specific sytle sheet
    include $_SERVER['DOCUMENT_ROOT'] . '/styles/themes/' . $_GET['theme'] . '/ieFix.css';
    if ($matches['digit'] == 6) {
        // load IE6 specific style sheet
        include $_SERVER['DOCUMENT_ROOT'] . '/styles/themes/' . $_GET['theme'] . '/ieFix6.css';
    }
}
I was hoping to do something similar to:

Code: Select all

header("Content-type: text/css");
preg_match('/(?<name>[\w ]+) (?<digit>\d+)/', $_SESSION['browser'], $matches);
include $_SERVER['DOCUMENT_ROOT'] . '/styles/themes/' . $_GET['theme'] . '/reset.css';
include $_SERVER['DOCUMENT_ROOT'] . '/styles/themes/' . $_GET['theme'] . '/structure.css';
include $_SERVER['DOCUMENT_ROOT'] . '/styles/themes/' . $_GET['theme'] . '/presentation.css';
if (trim($matches['name']) == 'Internet Explorer') {
    // load IE specific sytle sheet
    include $_SERVER['DOCUMENT_ROOT'] . '/styles/themes/' . $_GET['theme'] . '/ieFix.css';
    if ($matches['digit'] == 6) {
        // load IE6 specific style sheet
        include $_SERVER['DOCUMENT_ROOT'] . '/styles/themes/' . $_GET['theme'] . '/ieFix6.css';
    }
}
Is there any way for the CSS file to use the session variables?

THX

Re: PHP Sessions w/ CSS File

Posted: Sun Aug 17, 2008 3:53 pm
by neophyte
Sure, but when do you call session_start()? Gotta be called before accessing _SESSION super global.

Re: PHP Sessions w/ CSS File

Posted: Sun Aug 17, 2008 5:58 pm
by w0lf42
Thanks - It works! I assumed since I called the session start in my index.php file, that I didn't need to do it in my css.php file.