A-A+

php实现通过cookie换肤的方法

2021年06月12日 我爱编程 暂无评论

这篇文章主要介绍了php实现通过cookie换肤的方法,通过cookie存储用户选择信息实现换肤效果,具有一定参考借鉴价值,需要的朋友可以参考下。

本文实例讲述了php实现通过cookie换肤的方法,分享给大家供大家参考,具体如下:

saveStyleSheet.php页面如下:

  1. <?php
  2. function styleSheet($currentCookie){
  3.   // Get Current Style Sheet 
  4.   $currentCookie = $_COOKIE["StyleSheet"];
  5.   // Get New cookie file name 
  6.   switch($_GET['style']){
  7.     case 1:
  8.       $value = 'style1.css';
  9.     break;
  10.     case 2:
  11.       $value = 'style2.css';
  12.     break;
  13.     case 3:
  14.       $value = 'style3.css';
  15.     break;
  16.     default:
  17.       $value = 'style.css';
  18.     break;
  19.   }
  20.   // If the user views this page, without using 
  21.   // style=... then set cookie to the default 
  22.   if(!isset($_GET['style'])){
  23.     $value = 'style.css';
  24.   }
  25.   // If the new value doesn't equal the old value allow cookie change 
  26.   if(isset($value)||$currentCookie!=$value||isset($currentCookie)){
  27.     setcookie("StyleSheet"$value, time()+600000); /* expires in 10,000 hours*/
  28.     return $_COOKIE["StyleSheet"];
  29.   }else{
  30.     return $_COOKIE["StyleSheet"];
  31.   }
  32.   if(isset($_GET['style'])){
  33.     header("Location: ".$_SERVER['HTTP_REFERER']);
  34.     exit;
  35.   }
  36. }
  37. ?>

index.php页面如下:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  6. <title>My Test Page</title>
  7. <?php
  8. include("saveStyleSheet.php");
  9. if(isset($_COOKIE["StyleSheet"])){
  10. ?>
  11. <link rel="stylesheet" type="text/css" href="stylesheets/ <?php echo styleSheet($_COOKIE["StyleSheet"]); ?> " />
  12. <?php
  13. }else{
  14. ?>
  15. <link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
  16. <?php
  17. }
  18. ?>
  19. </head>
  20. <body>
  21. <a href="saveStyleSheet.php?style=1">Style Sheet 1</a><br />
  22. <a href="saveStyleSheet.php?style=2">Style Sheet 2</a><br />
  23. <a href="saveStyleSheet.php?style=3">Style Sheet 3</a><br />
  24. <a href="saveStyleSheet.php">Default Style Sheet</a>
  25. </body>
  26. </html>

希望本文所述对大家的php程序设计有所帮助。

标签:

给我留言

Copyright © 四季博客 保留所有权利.   Theme  Ality

用户登录