首页  >  文章  >  后端开发  >  php通过cookie切换网页风格皮肤

php通过cookie切换网页风格皮肤

*文
*文原创
2017-12-26 17:56:471167浏览

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[&#39;style&#39;]){ 
    case 1: 
      $value = &#39;style1.css&#39;; 
    break; 
    case 2: 
      $value = &#39;style2.css&#39;; 
    break; 
    case 3: 
      $value = &#39;style3.css&#39;; 
    break; 
    default: 
      $value = &#39;style.css&#39;; 
    break; 
  } 
  // If the user views this page, without using 
  // style=... then set cookie to the default 
  if(!isset($_GET[&#39;style&#39;])){ 
    $value = &#39;style.css&#39;; 
  } 
  // If the new value doesn&#39;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[&#39;style&#39;])){ 
    header("Location: ".$_SERVER[&#39;HTTP_REFERER&#39;]); 
    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切换网页风格皮肤的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn