php实现通过cookie换肤的方法

作者:不吃皮蛋 时间:2023-11-23 17:57:07 

本文实例讲述了php实现通过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,cookie,换肤
0
投稿

猜你喜欢

  • 大大改善用户体验的10个小细节

    2008-09-17 13:27:00
  • python游戏实战项目之俄罗斯方块的魅力

    2021-12-07 19:38:23
  • webpack的 rquire.context用法实现工程自动化的方法

    2024-05-11 09:05:29
  • 用Python写飞机大战游戏之pygame入门(4):获取鼠标的位置及运动

    2023-09-28 11:01:21
  • python机器学习之决策树分类详解

    2023-04-28 14:31:05
  • mysql按照自定义(指定顺序)排序的方法实例

    2024-01-14 12:33:46
  • Python判断Abundant Number的方法

    2023-10-30 02:25:30
  • python实现图像全景拼接

    2023-05-08 13:29:24
  • Go语言实现枚举的示例代码

    2024-04-26 17:27:47
  • 从trim原型函数看js正则表达式的性能

    2008-12-11 13:55:00
  • python装饰器初探(推荐)

    2023-01-19 14:40:27
  • Vue自定义指令中无法获取this的问题及解决

    2024-05-29 22:46:46
  • 使用Fabric自动化部署Django项目的实现

    2022-09-05 22:00:33
  • pytorch常用数据类型所占字节数对照表一览

    2021-07-25 15:26:29
  • python列表[list]和元组(tuple)详情

    2022-05-23 01:14:31
  • 开发Web应用程序的结构化过程

    2009-06-01 10:52:00
  • JavaScript简单计算人的年龄示例

    2024-05-03 15:04:39
  • Git 标签使用详解

    2023-09-06 14:09:32
  • Python使用pandas导入xlsx格式的excel文件内容操作代码

    2022-03-12 04:29:57
  • 用python实现简单EXCEL数据统计的实例

    2021-12-30 09:59:01
  • asp之家 网络编程 m.aspxhome.com