php实现通过cookie换肤的方法

yipeiwu_com6年前PHP代码库

本文实例讲述了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基于递归实现的约瑟夫环算法示例

本文实例讲述了PHP基于递归实现的约瑟夫环算法。分享给大家供大家参考,具体如下: 约瑟夫环问题: 39 个犹太人与Josephus及他的朋友躲到一个洞中,39个犹太人决定宁愿死也不要被敌...

PHP SESSION的增加、删除、修改、查看操作

SESSION与COOKIE的不同之处首先是,cookie的文件是保存在客户端的,而session是保存在服务器的,相比而言,为了提高一定的安全性,session更具有优势。 因为ses...

PHP读取word文档的方法分析【基于COM组件】

本文实例讲述了PHP读取word文档的方法。分享给大家供大家参考,具体如下: php开发 过程中可能会word文档的读取问题,这里可以利用com组件来完成此项操作 一、先开启php.in...

PHP iconv 函数转gb2312的bug解决方法

iconv( "UTF-8", "gb2312//IGNORE" , $FormValues['a']) ignore的意思是忽略转换时的错误,发现iconv在转换字符"—"到gb231...

CentOS系统中PHP安装扩展的方式汇总

CentOS系统中PHP安装扩展的方式汇总

CentOS 下,PHP有多种方式来安装扩展, 主要有 包管理式 的 yum 安装 、 pecl 安装 , 以及 源码编译安装 。 包管理式 的安装卸载尤为方便,而 源码编译式 的安装则...