PHP/Cookie Session/Session Variables — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 10:37, 26 мая 2010
Содержание
- 1 Accessing Session Variables
- 2 Adding Session Data
- 3 A session variable can be destroyed with a call to session_unregister().
- 4 Checking Session Data
- 5 Reading Session Data
- 6 Registering an Array Variable with a Session
- 7 Registering Variables with a Session
- 8 Storing Complex Data Types in Sessions
- 9 Storing Complex Data Types to session
- 10 Storing Simple Data Types in Sessions
Accessing Session Variables
<?php
session_start();
?>
<html>
<head>
<title>Accessing Session Elements</title>
</head>
<body>
<div>
<h1>A Content Page</h1>
<?php
if ( is_array( $_SESSION["products"] ) ) {
print "<b>Your cart:</b><ol>\n";
foreach ( $_SESSION["products"] as $p ) {
print "<li>$p</li>";
}
print "</ol>";
}
?>
<a href="indec.php">Back to product choice page</a>
</div>
</body>
</html>
Adding Session Data
<?
$_SESSION["var"] = $val;
$_SESSION["FirstName"] = "Jim";
?>
A session variable can be destroyed with a call to session_unregister().
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();
?>
Checking Session Data
<?
session_start( );
if (isset($_SESSION["FirstName"])) {
/// your code here
}
?>
Reading Session Data
<?
$_SESSION["foo"] = "bar";
print $_SESSION["foo"];
?>
Registering an Array Variable with a Session
<?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>
<div>
<form action="<?php print $_SERVER["PHP_SELF"]?>" method="post">
<p>
<select name="form_products[]" multiple="multiple" size="3">
<option>A</option>
<option>B</option>
<option>C</option>
<option>D</option>
<option>E</option>
</select>
</p>
<p>
<input type="submit" value="choose" />
</p>
</form>
<a href="index.php">A content page</a>
</div>
</body>
</html>
Registering Variables with a Session
<?php
session_start();
?>
<html>
<head>
<title>Registering Variables with a Session</title>
</head>
<body>
<div>
<?php
$_SESSION["product1"] = "A";
$_SESSION["product2"] = "B";
print "The products have been registered";
?>
</div>
</body>
</html>
Storing Complex Data Types in Sessions
<?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();
?>
Storing Complex Data Types to session
<?
$myarr["0"] = "Sunday";
$myarr["1"] = "Monday";
$myarr["2"] = "Tuesday";
$myarr["3"] = "Wednesday";
$myarr["4"] = "Thursday";
$myarr["5"] = "Friday";
$myarr["6"] = "Saturday";
$_SESSION["myarr"] = $myarr;
?>
Storing Simple Data Types in Sessions
<?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"] . "<br />";
echo $_SESSION["string_value"] . "<br />";
echo $_SESSION["float_value"] . "<br />";
}
outputsessions();
?>