欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php实现通过cookie换肤的方法

程序员文章站 2022-06-03 15:54:52
本文实例讲述了php实现通过cookie换肤的方法。分享给大家供大家参考。具体如下: savestylesheet.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程序设计有所帮助。