hercioneto

Exemplo Cookie

Aug 7th, 2023 (edited)
882
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.82 KB | None | 0 0
  1. ----- arquivo definido.php
  2.  
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6.     <title>Meu Site com Tema</title>
  7.     <?php
  8.     // Verifica se o cookie de tema foi definido
  9.     if (isset($_COOKIE['theme'])) {
  10.         $theme = $_COOKIE['theme'];
  11.         // Carrega um arquivo CSS com base no tema escolhido
  12.         echo '<link rel="stylesheet" href="' . $theme . '_theme.css">';
  13.     }
  14.     ?>
  15. </head>
  16. <body>
  17.     <h1>Bem-vindo ao Meu Site</h1>
  18.     <p>Este é um exemplo de site que usa cookies para lembrar o tema de cor escolhido.</p>
  19.     <p><a href="preferences.php">Configurar Preferências</a></p>
  20. </body>
  21. </html>
  22.  
  23.  
  24. ----- arquivo  preferences.php
  25.  
  26. <!DOCTYPE html>
  27. <html>
  28. <head>
  29.     <title>Preferências de Tema</title>
  30. </head>
  31. <body>
  32.     <h1>Preferências de Tema</h1>
  33.     <form action="set_cookie.php" method="post">
  34.         <label for="theme">Escolha um tema de cor:</label>
  35.         <select name="theme" id="theme">
  36.             <option value="light">Claro</option>
  37.             <option value="dark">Escuro</option>
  38.         </select>
  39.         <br>
  40.         <input type="submit" value="Salvar Preferência">
  41.     </form>
  42. </body>
  43. </html>
  44.  
  45.  
  46. --- arquivo set_cookie.php
  47.  
  48. <?php
  49. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  50.     if (isset($_POST['theme'])) {
  51.         $theme = $_POST['theme'];
  52.         // Define um cookie com o tema escolhido que expira em 30 dias
  53.         setcookie('theme', $theme, time()+3600, '/');
  54.     }
  55. }
  56. header('Location: definido.php'); // Redireciona de volta para a página inicial
  57. ?>
  58.  
  59. ---
  60.  
  61. ------ arquivo dark_theme.css
  62.  
  63. body {
  64.     background-color: #333;
  65.     color: #fff;
  66. }
  67.  
  68. h1 {
  69.     color: #eee;
  70. }
  71.  
  72. a {
  73.     color: #66b3ff;
  74. }
  75.  
  76. ---------- arquivo light_theme.css
  77.  
  78.  
  79. body {
  80.     background-color: #f5f5f5;
  81.     color: #333;
  82. }
  83.  
  84. h1 {
  85.     color: #555;
  86. }
  87.  
  88. a {
  89.     color: #007bff;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment