首頁  >  文章  >  後端開發  >  php基於cookie實現換膚的方法

php基於cookie實現換膚的方法

墨辰丷
墨辰丷原創
2018-06-08 17:05:231322瀏覽

這篇文章主要介紹php基於cookie實現換膚的方法,有興趣的朋友參考下,希望對大家有幫助。

本文實例講述了php實作透過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>
<html>
<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 實作的將圖片轉換為ASCII碼

透過php實作統計在線人數的方法

php程式中取得漢字拼音的首字母的方法

以上是php基於cookie實現換膚的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn