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中有很多函数可以移动数组指针。下面将给大家介绍几个。 将指针移动到下一个数组位置next() next()函...

PHP随机数函数rand()与mt_rand()的讲解

PHP中rand()与mt_rand()都是用于产生一个指定范围内单独随机数的函数既然他们都是用于产生一个随机数,那么他们有什么区别呢?rand() 函数默认使用 libc 随机数发生器,...

总结PHP中数值计算的注意事项

一:四舍五入 1.round — 对浮点数进行四舍五入 float round ( float $val [, int $precision ] ) 2:floor — 舍去法取...

php模拟post上传图片实现代码

php模拟post上传图片实现代码

本文实例为大家分享了php模拟post上传图片的具体代码,供大家参考,具体内容如下 服务器和客户端都是php语言 但是客户端不是网页,不在浏览器上运行,而是在命令行运行 现在要做的是在客...

php隐藏IP地址后两位显示为星号的方法

本文实例讲述了php隐藏IP地址后两位显示为星号的方法。分享给大家供大家参考。具体实现方法如下: 我们在很多的公共网站中都会有碰到显示用户的IP时后面几个IP段显示为星号了,这样很好的保...