Page 1 of 1

Sessions And Css Changing CSS with Sessions and php Rate Topic: -----

#1 SpaceMan  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 10
  • View blog
  • Posts: 507
  • Joined: 20-February 03

Posted 21 December 2004 - 12:58 AM

I have lately got into CSS a bit, seems is much more powerful then most know about.
Off topic a bit, (I even went so far as to write a CSS maker/manager on-line with php. it does many things, like save as, auto backups, ftp upload, undo and many other things you do not see in on-line programs. )
This is a small example of how sessions can help you or your surfers to change a style easily.

assuming you have a directory style/ from page being loaded, with several different style-name.css in that directory.

first thing we need to do is start a session and make a default style for when the page loads.
this is set to load style.css as default.
the ( ! ) before a setting is, if it is empty do this function.
if they already chose one, then load it, else statement.
<?php 
session_start(); 

if ( ! session_is_registered("style") ) 
{ 
// default setting is style in session_register("style"); 
session_register("style"); 
$_SESSION['style'] = "style";  

$my_style = $_SESSION['style']; 
} 
else 
{ 
$my_style = $_SESSION['style']; 
} 

?>

Styles are loaded via a header tag, like....
my_style being the name we chose to use as to register it in sessions.
And .css as the file type.
if you are loading pages in different directory's use full URL to style/ directory.
<head> 
<link href="style/<?=$my_style;?>.css" rel="StyleSheet" type="text/css"> 
</head> 



so now what we need is links to change it.
on the links you can use page-name.php?style=style-name , i use php self a lot with php so it does not mater what page you add it to. but you may find a reason or need.

<a href="<?=$_SERVER[PHP_SELF]?>?style=style">My Style</a> <a href="<?=$_SERVER[PHP_SELF]?>?style=niftystyle">Nifty Style</a> 



Aha we missing a way to register a new style into sessions.
HM, were we gonna put it?
probably at the beginning of the php. but want it under the other functions.
i will often use a refresh for various reasons, in this case was needed to get the new style from the sessions, and i add a link if the browser does not refresh.
if($_GET[style]) {
session_start(); 
$_SESSION['style'] = $_GET['style']; 
print '<a href='.$_SERVER[PHP_SELF].'>Refresh</a>'; 
print "<html><head><META http-equiv=\"refresh\" content=\"0;URL=$_SERVER[PHP_SELF]\"></head><body>";
}


complete code with a test, or sample link to clcick on.
<?php 
session_start(); 

if ( ! session_is_registered("style") ) 
{ 
session_register("style"); 
$_SESSION['style'] = "style";  

$my_style = $_SESSION['style']; 
} 
else 
{ 
$my_style = $_SESSION['style']; 
} 
if($_GET[style]) {
session_start(); 
$_SESSION['style'] = $_GET['style']; 
print '<a href='.$_SERVER[PHP_SELF].'>Refresh</a>'; 
print "<html><head><META http-equiv=\"refresh\" content=\"0;URL=$_SERVER[PHP_SELF]\"></head><body>";
}
?>
<head> 
<link href="style/<?=$my_style;?>.css" rel="StyleSheet" type="text/css"> 
</head> 



<a href="<?=$_SERVER[PHP_SELF]?>?style=style">My Style</a> <a href="<?=$_SERVER[PHP_SELF]?>?style=niftystyle">Nifty Style</a> 

<br><br><a href="<?=$_SERVER[PHP_SELF]?>">My Links</a> 


Have fun
Dave

Is This A Good Question/Topic? 0
  • +

Page 1 of 1