Jump to content

css style switcher with php


benjahnee

Recommended Posts

Hello guys

 

I have tried to create a css style switcher using php.

The code shows no errors but when i click a link to change css style on the page, it doesnt direct to the correct place.

coukd somebdy take a look at my code and tell me how to change it, i am very new to this so if you could change it for me and explain i would be very grateful.

 

thanks

 

<code>

 

 

<?php session_start(); ?>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
 
 <body>
 
 
</head>
 
<?php
if(isset($_GET['css'])){
switch ($_GET['css']) {
 
case 'red':
$stylesheet = '<link rel="stylesheet" type="text/css" href="alternate.css">';
$_SESSION['switchcss']=$stylesheet;
break;
 
case 'yellow':
$stylesheet = '<link rel="stylesheet" type="text/css" href="css/main.css">';
$_SESSION['switchcss']=$stylesheet;
break;
 
case 'black':
$stylesheet = '<link rel="stylesheet" type="text/css" href="css/alternate2.css">';
$_SESSION['switchcss']=$stylesheet;
break;
 
// Our default stylesheet
default:
$stylesheet = '<link rel="stylesheet" type="text/css" href="css/main.css">';
$_SESSION['switchcss']=$stylesheet;
}
}
?>
 
<?php echo ($_SESSION['switchcss'])? $_SESSION['switchcss']: '<link href="css/main.css" type="text/css" rel="stylesheet">';?>
 
<a href="css/alternate.css<?php echo $_SERVER['PHP_SELF'].'?css=red'; ?>">[red]</a> 
 | <a href="css/main.css<?php echo $_SERVER['PHP_SELF']. '?css=default'; ?>">[blue]</a>
 | <a href="css/main.css<?php echo $_SERVER['PHP_SELF']. '?css=yellow';?>">[yellow]</a>
| <a href="css/alternate2.css<?php echo $_SERVER['PHP_SELF']. '?css=black';?>">[black]</a>
 
 
 
 
 
 
</body>
 </html>
 
<code>
Link to comment
https://forums.phpfreaks.com/topic/275597-css-style-switcher-with-php/
Share on other sites

Let's break down the code. In the href attribute, you're telling the link to go to the CSS file ("css/alternate.css"). But then you also tack on the actual page address with PHP_SELF. So if your page is called about.php and it's in the root directory, the href attribute would look something like this:

 

 

"css/alternate.css/about.php?css=red"

 

Does that look correct?

 

 

 

First off, I would recommend staying away from PHP_SELF in this case for security reasons. More information can be found here:

http://www.cyberscorpion.com/2012-03/why-php_self-should-be-avoided-when-creating-website-links/

 

You could use the page name, for example. Also, the path to the CSS file shouldn't be included since that's handled by your GET variable ("css"). If your page is named about.php, you would do something like this:

 

 

<a href="about.php?css=red">[red]</a>

 

Note that you'll need to change the other links too.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.