PHP/Cookie Session/setcookie

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

Assigning cookie names according to array index value

   <source lang="html4strict">

<? setcookie("php[uid]", "1111111111111111", time()+3600); setcookie("php[color]", "black", time()+3600); setcookie("php[preference]", "english", time()+3600); if (isset ($php)) {

  while (list ($name, $value) = each ($php)) {
     echo "$name = $value
\n"; }

} ?>

 </source>
   
  


Cookie created

   <source lang="html4strict">

<?php

 echo( "Cookie created? : " . setcookie("cookie_name", "cookie_data" ) ); 

?> <html>

<head>
 <title>Cookie</title>
</head>
<body>
</body>

</html>

 </source>
   
  


Deleting a Cookie Using setcookie()

   <source lang="html4strict">

<?php

   setcookie("mycookie", NULL, NULL, "/", ".demo.org");

?>

 </source>
   
  


Deleting Cookies

   <source lang="html4strict">

<?php setcookie ( "cookie_user", "test", time () + 60 * 60 * 24 * 30 ); setcookie ( "cookie_pass", md5 ( "test" ), time () + 60 * 60 * 24 * 30 ); function logout() {

 setcookie ( "cookie_user", "", time () + 60 * 60 * 24 * 30 );
 setcookie ( "cookie_pass", "", time () + 60 * 60 * 24 * 30 );

} logout (); echo $_COOKIE ["cookie_user"] . "
"; echo "You have successfully logged out."; ?>

 </source>
   
  


PHP 5 setcookie() Parameters

   <source lang="html4strict">

Parameter Description name The name to set the cookie variable to and hence the name to access it with value The value of the current cookie expire When a cookie will expire (in the form of a Unix timestamp) path The directory where the cookie will be available for use domain The domain at which the cookie will be available secure Whether a cookie can be read on a non-SSL enable script <?php

 $GLOBALS["username"] = "test";
 $GLOBALS["password"] = "test";
 
 function validatelogin ($username, $password){
   if (strcmp ($username, $GLOBALS["username"]) == 0 && strcmp ($password, $GLOBALS["password"]) == 0){
     setcookie ("cookie_user", $username, time()+60*60*24*30);
     setcookie ("cookie_pass", md5 ($password), time()+60*60*24*30);
     return true;
   } else {
     return false;
   }
 }
 if (validatelogin ("test","test")){
   echo "Successfully logged in.";
 } else {
   echo "Sorry, invalid login.";
 }

?>

 </source>
   
  


Reading Cookies

   <source lang="html4strict">

<?php

 $GLOBALS["username"] = "test";
 $GLOBALS["password"] = "test";
 
 setcookie ("cookie_user", "test", time()+60*60*24*30);
 setcookie ("cookie_pass", md5 ("test"), time()+60*60*24*30);
 
 function validatelogin (){
   if (strcmp ($_COOKIE["cookie_user"], $GLOBALS["username"]) == 0 && strcmp ($_COOKIE["cookie_pass"], md5 ($GLOBALS["password"])) == 0){
     return true;
   } else {
     return false;
   }
 }
 
 if (validatelogin ()){
   echo "Successfully logged in.";
 } else {
   echo "Sorry, invalid login.";
 }

?>

 </source>
   
  


Setting a cookie with a domain restriction

   <source lang="html4strict">

<?php setcookie("flavor","chocolate","","",".example.ru"); setcookie("flavor","chocolate","","","j.example.ru"); ?>

 </source>
   
  


Setting a cookie with a path restriction

   <source lang="html4strict">

<?php setcookie("flavor","chocolate","","/products/"); ?>

 </source>
   
  


Setting an expiring cookie

   <source lang="html4strict">

<?php setcookie("flavor","chocolate",1259841600); ?>

 </source>
   
  


Setting cookie expiration

   <source lang="html4strict">

// The cookie expires one hour from now setcookie("short-userid","AAA",time() + 60*60);

 </source>
   
  


Setting the cookie domain

   <source lang="html4strict">

setcookie("short-userid","AAA",0,"/",".example.ru");

 </source>
   
  


Setting the cookie path

   <source lang="html4strict">

setcookie("short-userid","AAA",0,"/");

 </source>
   
  


Setting the cookie path to a specific directory

   <source lang="html4strict">

setcookie("short-userid","AAA",0,"/~alice/");

 </source>
   
  


Starting a page with setcookie()

   <source lang="html4strict">

<?php setcookie("userid","AAA"); ?> <html><head><title>Page with cookies</title><head> <body> This page sets a cookie properly, because the PHP block with setcookie() in it comes before all of the HTML. </body></html>

 </source>
   
  


The cookie expires one day from now

   <source lang="html4strict">

setcookie("longer-userid","AAA",time() + 60*60*24);

 </source>