PHP/Cookie Session/ COOKIE — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 10:37, 26 мая 2010

Checking for Cookie Support from PHP

 
<?php
    if(!isset($_GET["testcookie"])) {
        setcookie("testcookie", "test value");
        header("Location:  {$_SERVER["PHP_SELF"]}?testcookie=1");
        exit;
    } else {
        if(isset($_COOKIE["testcookie"])) {
            setcookie("testcookie");
            echo "You have cookies enabled";
        } else {
            echo "You do not support cookies!";
        }
    }
?>



Get Cookie Data

 
<?php
  $user  = $_COOKIE["firstname"];
  $color = $_COOKIE["fontcolor"];
?>
<html>
 <head>
  
  <title>Get Cookie Data</title>
  
  <style type = "text/css">
   body { color: <?php echo( $color ); ?> }
 
  </style>
 </head>
 <body>
  <h1>Hello <?php echo( $user ); ?>! </h1>
 </body>
</html>



Printing a cookie value

 
<?
print "Hello, " . $_COOKIE["userid"];
?>



Reading a cookie value

 
<?php
if (isset($_COOKIE["flavor"])) {
    print "You ate a {$_COOKIE["flavor"]} cookie.";
}
?>



Reading all cookie values

 
<?php
foreach ($_COOKIE as $cookie_name => $cookie_value) {
    print "$cookie_name = $cookie_value <br/>";
}
?>



Setting and Printing a Cookie Value

 
<?php
 setcookie( "vegetable", "artichoke", time()+3600, "/","wbex.ru", 0 );
 ?>
 <html>
 <head>
 <title>Setting and Printing a Cookie Value</title>
 </head>
 <body>
 <?php
 if ( isset( $_COOKIE["vegetable"] ) ) {
     print "<p>Hello again, your chosen vegetable is ";
     print "{$_COOKIE["vegetable"]}</p>";
 } else {
     print "<p>Hello you. This may be your first visit</p>";
 }
 ?>
 </body>
 </html>



Still Logged In with cookie

 
<?php
  $auth = $_COOKIE["auth"];
  header( "Cache-Control:no-cache" ); 
  if( ! $auth == "ok" ) 
  {
    header("Location:login.php" );
    exit();
  }
?>

<html>
 <head>
  <title>Still Logged In</title>
 </head>
 <body>
  You are still logged in ...
 </body>
</html>