php实现通过cookie换肤的方法

yipeiwu_com4年前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 使用二进制保存用户状态的实例

前言 用户状态保存是一个很常见的需求,一般用来保存用户状态的方式是在数据库表中创建多个字段来存储相应的用户状态,比如要保存用户是否绑定了手机号和QQ,则需要2个字段(mobile,qq)...

php 从数据库提取二进制图片的处理代码

image.php文件 复制代码 代码如下: <?php $conn=@mysql_connect("localhost","root","123") or die("服务器连接错...

360通用php防护代码(使用操作详解)

360发布通用php防护代码,其实最初是协助phpcms来防护安全用的,现在看来可以加入到任何有漏洞的网站里面,拿phpcmsv9问题,解决方案如下,其他网站以此类推! 1.将360_s...

php密码生成类实例

本文实例讲述了php实现的密码生成类及其应用方法,分享给大家供大家参考。具体分析如下: 一、php密码生成类功能: 1.可设定密码长度。 2.可设定要生成的密码个数,批量生成。 3.可以...

24条货真价实的PHP代码优化技巧

 PHP代码优化24条真经,希望对大家开发php项目有所帮助,具体内容如下  1.echo比print快。  2.使用echo的多重参数代替字符串连接。...