...

View Full Version : Cannot figure out how to add definition...



Errica
03-17-2007, 02:28 PM
The following page (http://handycode.kahi.cz/my/?code=php:font-switcher), has a script I'd like to try.

Below is the way they have the code displayed. I cannot figure out how to test this. I tried including the definition but got the following error (Warning: session_start(): Cannot send session cache limiter - headers already sent). I have the distinct feeling its something obvious I'm overlooking. :)


Definition


<?php
/*
* (c) 2007, Peter Kahoun aka Kahi
* web: kahi.cz
* mail: kahi@kahi.cz
* BSD licence
*
* Version: 1.0 RC1
* Last change: 3.3.2007
**/

// SETTINGS

$sett_influenced_elements = 'body';

$sett_fonts = '
Arial;sans-serif
Times;serif
';

$sett_address = $_SERVER['SERVER_NAME'];

// CLASS

class FontSwitcher {

var $fonts;
var $active_font;
var $default_font;
var $unique_string;
var $influenced_elements;


// Parse settings // Create unique string // Find active font // Check switching
function FontSwitcher () {
session_start();

// Parse fonts from settings
global $sett_fonts;
$sett_fonts = explode ("\n", trim ($sett_fonts));
$row_count = count($sett_fonts);

for ($i = 0; $i < $row_count; $i++) {
$row = $sett_fonts[$i];
$row = explode (';', $row);

$fonts[trim ($row[0])] = trim ($row[1]);

if ($i == 0) {
$this -> default_font = array('name' => $row[0], 'family' => $row[1]);
}
}

$this -> fonts = $fonts;

// Create unique string and so

global $sett_address;
$this -> unique_string = 'font_switcher_' . substr( md5($sett_address), 0, 8 );

global $sett_influenced_elements;
$this -> influenced_elements = $sett_influenced_elements;

// Find active font

$font_session = @$_SESSION [$this -> unique_string];
$font_cookie = @$_COOKIE [$this -> unique_string];

if (isset ($font_sess) AND array_key_exists($font_sess, $fonts)) {
$active_font = $font_sess;

} elseif (isset ($font_cookie) AND array_key_exists($font_cookie, $fonts)) {
$active_font = $font_cookie;

} else {
$active_font = $this -> default_font['name'];
}

$this -> active_font = $active_font;

// Check switching
$this -> SwitchFont();
}


// Switch it easy
function SwitchFont () {
if (isset ($_GET['font_switcher'])) {
$new_font = $_GET['font_switcher'];

if ($new_font) {
setcookie($this -> unique_string, $new_font, time()+30*24*3600);
$_SESSION[$this -> unique_string] = $new_font;
Header ('Location: '.$_SERVER['HTTP_REFERER']);
}
}
}


// Echoes <style> element with right css definition
function EchoCss () {
$active_font = $this -> active_font;
$fonts = $this -> fonts;
if (array_key_exists ($active_font, $fonts)) {
$family = $fonts [$active_font];
$message = '';
} else {
$family = $this->default_font['family'];
$message = "/* '$active_font' family definition not found. Default font is: '". $this->default_font['name'] ."' */ \n";
}

echo "\n" . '<style type="text/css">' . "\n" . $message . $this -> influenced_elements . " {font-family: $family } \n</style>\n";

}

// Links that cause switching :)
function EchoLinks ($separator = ' ') {
$fonts = $this -> fonts;
$active_font = $this -> active_font;
$fonts_count = count($fonts);

$active_address = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$active_address_any_par = strpos($active_address, '?');
$active_address .= ($active_address_any_par) ? '&' : '?';

$link_list = array();

foreach ($fonts as $font => $family) {
if ($active_font != $font) {
$link_list[] = '<a href="'. $active_address . 'font_switcher=' . $font .'" rel="nofollow">' . $font . '</a>';
} else {
$link_list[] = $font;
}

}

echo implode ($separator, $link_list);
}

} // This is the end...

?>


Using:


<?php $fs = new FontSwitcher(); ?>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Font Switcher demonstration page</title>

<style type="text/css">
#all {width:400px; margin:2em auto; padding:2em;}
</style>

<?php $fs -> EchoCss (); ?>

</head>

<body>
<div id="all">

<h1>Font Switcher demonstration page</h1>

<p>Lorem ipsum dolor sit amet consectetuer nisl sit et dui tincidunt. In Vivamus Maecenas Ut eu habitant vel Curabitur Morbi at faucibus. Magnis magna sagittis non auctor pretium odio Ut nibh dis felis. Leo facilisi Nunc turpis enim tortor Nunc elit nibh Sed wisi. Justo felis rutrum massa metus lacus Curabitur Nulla orci vel nisl. Habitasse nisl cursus ante tincidunt congue aliquet Nulla Vivamus lacus interdum.</p>

<p><?php $fs -> EchoLinks (' / '); ?></p>

<p>&copy; 2007, <a href="http://kahi.cz">Kahi</a> </p>
</div>
</body>
</html>

mlseim
03-17-2007, 04:03 PM
I don't really have an answer to the problem with your script.

Their working example works good and it's pretty neat, but I've always
used "alternate CSS" style sheets to change the look of my pages.

http://www.alistapart.com/stories/alternate/

or: http://www.google.com/search?hl=en&safe=active&q=css+switcher

One main reason to use alternate CSS is the capability of creating
a nice "printer only" version of your page ... or "accessibility" version.

I just wanted to mention there was another method you could look at,
in case you never solve the session problem.

koyama
03-17-2007, 04:54 PM
I tried including the definition but got the following error (Warning: session_start(): Cannot send session cache limiter - headers already sent).
Check that there is no white space in the included file before and after the opening <?php and closing ?>.

Also, a common mistake is to save the included file in UTF-8 using Notepad. This won't work because it includes a BOM. Save as plain ASCII. This is the same as UTF-8 as long as no special characters are being used (which is the case for your class definition).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum