php如何通过cookie切换网页风格?这篇文章主要介绍了php实现通过cookie换肤的方法,通过cookie存储用户选择信息实现换肤效果,希望对大家有所帮助。
具体如下:
savestylesheet.php页面如下:
<?php
function stylesheet($currentcookie){
// get current style sheet
$currentcookie = $_cookie["stylesheet"];
// get new cookie file name
switch($_get['style']){
case 1:
$value = 'style1.css';
break;
case 2:
$value = 'style2.css';
break;
case 3:
$value = 'style3.css';
break;
default:
$value = 'style.css';
break;
}
// if the user views this page, without using
// style=... then set cookie to the default
if(!isset($_get['style'])){
$value = 'style.css';
}
// if the new value doesn't equal the old value allow cookie change
if(isset($value)||$currentcookie!=$value||isset($currentcookie)){
setcookie("stylesheet", $value, time()+600000); /* expires in 10,000 hours*/
return $_cookie["stylesheet"];
}else{
return $_cookie["stylesheet"];
}
if(isset($_get['style'])){
header("location: ".$_server['http_referer']);
exit;
}
}
?>
index.php页面如下:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en"
"http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>my test page</title>
<?php
include("savestylesheet.php");
if(isset($_cookie["stylesheet"])){
?>
<link rel="stylesheet" type="text/css" href="stylesheets/ <?php echo stylesheet($_cookie["stylesheet"]); ?> " />
<?php
}else{
?>
<link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
<?php
}
?>
</head>
<body>
<a href="savestylesheet.php?style=1">style sheet 1</a><br />
<a href="savestylesheet.php?style=2">style sheet 2</a><br />
<a href="savestylesheet.php?style=3">style sheet 3</a><br />
<a href="savestylesheet.php">default style sheet</a>
</body>
</html>
相关推荐:
php高级教程:php cookies
php cookies操作类(附源码)
php cookies 常用于识别用户
以上就是php通过cookie切换网页风格皮肤的详细内容。