>  기사  >  백엔드 개발  >  PHP는 쿠키를 통해 웹페이지 스타일 스킨을 전환합니다.

PHP는 쿠키를 통해 웹페이지 스타일 스킨을 전환합니다.

*文
*文원래의
2017-12-26 17:56:471166검색

PHP에서 쿠키를 통해 웹페이지 스타일을 전환하는 방법은 무엇입니까? 본 글은 쿠키를 통해 스킨을 변경하기 위해 PHP를 사용하는 방법을 주로 소개하며, 쿠키를 사용하여 사용자 선택 정보를 저장하여 스킨 변경 효과를 얻는 것이 모든 분들께 도움이 되기를 바랍니다.

상세 내용은 다음과 같습니다.

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

관련 권장 사항:

PH P 고급 튜토리얼: PHP 쿠키

php 쿠키 작업 클래스(소스 코드 포함)

PHP 쿠키는 종종 사용자를 식별하는 데 사용됩니다

위 내용은 PHP는 쿠키를 통해 웹페이지 스타일 스킨을 전환합니다.의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.