PHP Sessions w/ CSS File

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
w0lf42
Forum Newbie
Posts: 3
Joined: Sun Aug 17, 2008 3:28 pm

PHP Sessions w/ CSS File

Post 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
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Re: PHP Sessions w/ CSS File

Post by neophyte »

Sure, but when do you call session_start()? Gotta be called before accessing _SESSION super global.
w0lf42
Forum Newbie
Posts: 3
Joined: Sun Aug 17, 2008 3:28 pm

Re: PHP Sessions w/ CSS File

Post 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.
Post Reply