PHP/Cookie Session/Session Variables

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

Accessing Session Variables

   <source lang="html4strict">

<?php session_start(); ?> <html> <head> <title>Accessing Session Elements</title> </head> <body>

A Content Page

<?php if ( is_array( $_SESSION["products"] ) ) {

print "Your cart:
    \n"; foreach ( $_SESSION["products"] as $p ) { print "
  1. $p
  2. ";
     }
    
    print "
";

} ?> <a href="indec.php">Back to product choice page</a>

</body> </html>

 </source>
   
  


Adding Session Data

   <source lang="html4strict">

<?

   $_SESSION["var"] = $val;
   $_SESSION["FirstName"] = "Jim";

?>

 </source>
   
  


A session variable can be destroyed with a call to session_unregister().

   <source lang="html4strict">

Its syntax is: boolean session_unregister (string varname) <? session_start(); session_register("username"); // ...use the variable $username as needed, then destroy it. session_unregister("username"); session_destroy(); ?>

 </source>
   
  


Checking Session Data

   <source lang="html4strict">

<?

   session_start( );
   if (isset($_SESSION["FirstName"])) {
           /// your code here
   }

?>

 </source>
   
  


Reading Session Data

   <source lang="html4strict">

<?

   $_SESSION["foo"] = "bar";
   print $_SESSION["foo"];

?>

 </source>
   
  


Registering an Array Variable with a Session

   <source lang="html4strict">

<?php session_start(); if ( empty( $_SESSION["products"] ) ) {

 $_SESSION["products"]=array();

} if ( is_array( $_REQUEST["form_products"] ) ) {

 $_SESSION["products"] = array_unique(
   array_merge( $_SESSION["products"],
         $_REQUEST["form_products"] )
 );

} ?> <html> <head> <title>Registering an Array Element with a Session</title> </head> <body>

<form action="<?php print $_SERVER["PHP_SELF"]?>" method="post">

<select name="form_products[]" multiple="multiple" size="3"> <option>A</option> <option>B</option> <option>C</option> <option>D</option> <option>E</option> </select>

<input type="submit" value="choose" />

</form> <a href="index.php">A content page</a>

</body> </html>

 </source>
   
  


Registering Variables with a Session

   <source lang="html4strict">

<?php session_start(); ?> <html> <head> <title>Registering Variables with a Session</title> </head> <body>

<?php $_SESSION["product1"] = "A"; $_SESSION["product2"] = "B"; print "The products have been registered"; ?>

</body> </html>

 </source>
   
  


Storing Complex Data Types in Sessions

   <source lang="html4strict">

<?php

 session_start();
 
 class myclass {
   protected $myvalue;
   
   public function setmyvalue ($newvalue){
     $this->myvalue = $newvalue;
   }
   
   public function getmyvalue (){
     return $this->myvalue;
   }
 }
 
 $_SESSION["myclass_value"] = new myclass ();
 
 function outputsessions (){
   $_SESSION["myclass_value"]->setmyvalue ("Hello World");
   echo $_SESSION["myclass_value"]->getmyvalue ();
 }
 outputsessions();

?>

 </source>
   
  


Storing Complex Data Types to session

   <source lang="html4strict">

<?

   $myarr["0"] = "Sunday";
   $myarr["1"] = "Monday";
   $myarr["2"] = "Tuesday";
   $myarr["3"] = "Wednesday";
   $myarr["4"] = "Thursday";
   $myarr["5"] = "Friday";
   $myarr["6"] = "Saturday";
   $_SESSION["myarr"] = $myarr;

?>

 </source>
   
  


Storing Simple Data Types in Sessions

   <source lang="html4strict">

<?php

 session_start();
 
 (int) $_SESSION["integer_value"] = "115";
 (string) $_SESSION["string_value"] = "Hello World";
 (float) $_SESSION["float_value"] = "1.07";
 
 function outputsessions (){
   echo $_SESSION["integer_value"] . "
"; echo $_SESSION["string_value"] . "
"; echo $_SESSION["float_value"] . "
"; } outputsessions();

?>

 </source>