首頁 > 後端開發 > php教程 > php透過cookie切換網頁風格皮膚

php透過cookie切換網頁風格皮膚

*文
發布: 2023-03-18 15:14:01
原創
1377 人瀏覽過

php如何透過cookie切換網頁風格?這篇文章主要介紹了php實現透過cookie換膚的方法,透過cookie儲存使用者選擇資訊來實現換膚效果,希望對大家有幫助。

具體如下:

saveStyleSheet.php頁面如下:


#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

<?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頁面如下:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

<!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 Cookies

php Cookies操作類別(附原始碼)

#PHP Cookies 常用來辨識使用者

以上是php透過cookie切換網頁風格皮膚的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板