1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
|
<HTML
><HEAD
><TITLE
>Session handling functions</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.57"><LINK
REL="HOME"
TITLE="PHP Manual"
HREF="manual.html"><LINK
REL="UP"
TITLE="Function Reference"
HREF="funcref.html"><LINK
REL="PREVIOUS"
TITLE="shm_remove_var"
HREF="function.shm-remove-var.html"><LINK
REL="NEXT"
TITLE="session_start"
HREF="function.session-start.html"><META
NAME="HTTP_EQUIV"
CONTENT="text/html; charset=ISO-8859-1"></HEAD
><BODY
CLASS="reference"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>PHP Manual</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="function.shm-remove-var.html"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="function.session-start.html"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="reference"
><A
NAME="ref.session"
></A
><DIV
CLASS="TITLEPAGE"
><H1
CLASS="title"
>LX. Session handling functions</H1
><DIV
CLASS="PARTINTRO"
><A
NAME="AEN33776"
></A
><P
> Session support in PHP consists of a way to preserve certain data
across subsequent accesses. This enables you to build more
customized applications and increase the appeal of your web site.
</P
><P
> If you are familiar with the session management of PHPLIB, you
will notice that some concepts are similar to PHP's session
support.
</P
><P
> A visitor accessing your web site is assigned an unique id, the
so-called session id. This is either stored in a cookie on the
user side or is propagated in the URL.
</P
><P
> The session support allows you to register arbitrary numbers of
variables to be preserved across requests. When a visitor accesses
your site, PHP will check automatically (if session.auto_start is
set to 1) or on your request (explicitly through
<A
HREF="function.session-start.html"
><B
CLASS="function"
>session_start()</B
></A
> or implicitly through
<A
HREF="function.session-register.html"
><B
CLASS="function"
>session_register()</B
></A
>) whether a specific session
id has been sent with the request. If this is the case, the prior
saved environment is recreated.
</P
><P
> All registered variables are serialized after the request
finishes. Registered variables which are undefined are marked as
being not defined. On subsequent accesses, these are not defined
by the session module unless the user defines them later.
</P
><P
> The <A
HREF="configuration.html#ini.track-vars"
><TT
CLASS="literal"
>track_vars</TT
></A
> and
<A
HREF="configuration.html#ini.register-globals"
><TT
CLASS="literal"
>register_globals</TT
></A
>
configuration settings influence how the session variables get
stored and restored.
</P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Note: </B
>
As of PHP 4.0.3, <A
HREF="configuration.html#ini.track-vars"
><TT
CLASS="literal"
>track_vars</TT
></A
> is
always turned on.
</P
></BLOCKQUOTE
></DIV
><P
> If <A
HREF="configuration.html#ini.track-vars"
><TT
CLASS="literal"
>track_vars</TT
></A
> is
enabled and <A
HREF="configuration.html#ini.register-globals"
><TT
CLASS="literal"
>register_globals</TT
></A
>
is disabled, only members of the global associative array
$HTTP_SESSION_VARS can be registered as session variables. The
restored session variables will only be available in the array
$HTTP_SESSION_VARS.
<TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN33798"
></A
><P
><B
>Example 1.
Registering a variable with <A
HREF="configuration.html#ini.track-vars"
><TT
CLASS="literal"
>track_vars</TT
></A
>
enabled
</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> <?php
session_register("count");
$HTTP_SESSION_VARS["count"]++;
?>
</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
</P
><P
> If <A
HREF="configuration.html#ini.register-globals"
><TT
CLASS="literal"
>register_globals</TT
></A
>
is enabled, then all global variables can be registered as session
variables and the session variables will be restored to
corresponding global variables.
<TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN33806"
></A
><P
><B
>Example 2.
Registering a variable with <A
HREF="configuration.html#ini.register-globals"
><TT
CLASS="literal"
>register_globals</TT
></A
>
enabled
</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> <?php
session_register("count");
$count++;
?>
</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
</P
><P
> If both <A
HREF="configuration.html#ini.track-vars"
><TT
CLASS="literal"
>track_vars</TT
></A
> and
<A
HREF="configuration.html#ini.register-globals"
><TT
CLASS="literal"
>register_globals</TT
></A
>
are enabled, then the globals variables and the $HTTP_SESSION_VARS
entries will reference the same value.
</P
><P
> There are two methods to propagate a session id:
<P
></P
><UL
><LI
><P
> Cookies
</P
></LI
><LI
><P
> URL parameter
</P
></LI
></UL
>
</P
><P
> The session module supports both methods. Cookies are optimal, but
since they are not reliable (clients are not bound to accept
them), we cannot rely on them. The second method embeds the
session id directly into URLs.
</P
><P
> PHP is capable of doing this transparently when compiled with
<TT
CLASS="literal"
>--enable-trans-sid</TT
>. If you enable this option,
relative URIs will be changed to contain the session id
automatically. Alternatively, you can use the constant
<TT
CLASS="literal"
>SID</TT
> which is defined, if the client did not
send the appropriate cookie. <TT
CLASS="literal"
>SID</TT
> is either of
the form <TT
CLASS="literal"
>session_name=session_id</TT
> or is an empty
string.
</P
><P
> The following example demonstrates how to register a variable, and
how to link correctly to another page using SID.
<TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN33829"
></A
><P
><B
>Example 3. Counting the number of hits of a single user</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> <?php
session_register ("count");
$count++;
?>
Hello visitor, you have seen this page <? echo $count; ?> times.<p>
<php?
# the <?=SID?> is necessary to preserve the session id
# in the case that the user has disabled cookies
?>
To continue, <A HREF="nextpage.php?<?=SID?>">click here</A>
</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
</P
><P
> The <TT
CLASS="literal"
><?=SID?></TT
> is not necessary, if
<TT
CLASS="literal"
>--enable-trans-sid</TT
> was used to compile PHP.
</P
><P
> To implement database storage, or any other storage method, you
will need to use <A
HREF="function.session-set-save-handler.html"
><B
CLASS="function"
>session_set_save_handler()</B
></A
> to
create a set of user-level storage functions.
</P
><P
> The session management system supports a number of configuration
options which you can place in your php.ini file. We will give a
short overview.
<P
></P
><UL
><LI
><P
> <TT
CLASS="literal"
>session.save_handler</TT
> defines the name of the
handler which is used for storing and retrieving data
associated with a session. Defaults to
<TT
CLASS="literal"
>files</TT
>.
</P
></LI
><LI
><P
> <TT
CLASS="literal"
>session.save_path</TT
> defines the argument which
is passed to the save handler. If you choose the default files
handler, this is the path where the files are created.
Defaults to <TT
CLASS="literal"
>/tmp</TT
>.
</P
></LI
><LI
><P
> <TT
CLASS="literal"
>session.name</TT
> specifies the name of the
session which is used as cookie name. It should only contain
alphanumeric characters. Defaults to
<TT
CLASS="literal"
>PHPSESSID</TT
>.
</P
></LI
><LI
><P
> <TT
CLASS="literal"
>session.auto_start</TT
> specifies whether the
session module starts a session automatically on request
startup. Defaults to <TT
CLASS="literal"
>0</TT
> (disabled).
</P
></LI
><LI
><P
> <TT
CLASS="literal"
>session.cookie_lifetime</TT
> specifies the lifetime of
the cookie in seconds which is sent to the browser. The value 0
means "until the browser is closed." Defaults to
<TT
CLASS="literal"
>0</TT
>.
</P
></LI
><LI
><P
> <TT
CLASS="literal"
>session.serialize_handler</TT
> defines the name
of the handler which is used to serialize/deserialize
data. Currently, a PHP internal format (name
<TT
CLASS="literal"
>php</TT
>) and WDDX is supported (name
<TT
CLASS="literal"
>wddx</TT
>). WDDX is only available, if PHP is
compiled with <A
HREF="ref.wddx.html"
>WDDX
support</A
>. Defaults to <TT
CLASS="literal"
>php</TT
>.
</P
></LI
><LI
><P
> <TT
CLASS="literal"
>session.gc_probability</TT
> specifies the
probability that the gc (garbage collection) routine is started
on each request in percent. Defaults to <TT
CLASS="literal"
>1</TT
>.
</P
></LI
><LI
><P
> <TT
CLASS="literal"
>session.gc_maxlifetime</TT
> specifies the number
of seconds after which data will be seen as 'garbage' and
cleaned up.
</P
></LI
><LI
><P
> <TT
CLASS="literal"
>session.referer_check</TT
> contains the substring you
want to check each HTTP Referer for. If the Referer was sent by the
client and the substring was not found, the embedded session id will
be marked as invalid. Defaults to the empty string.
</P
></LI
><LI
><P
> <TT
CLASS="literal"
>session.entropy_file</TT
> gives a path to an
external resource (file) which will be used as an additional
entropy source in the session id creation process. Examples are
<TT
CLASS="literal"
>/dev/random</TT
> or
<TT
CLASS="literal"
>/dev/urandom</TT
> which are available on many
Unix systems.
</P
></LI
><LI
><P
> <TT
CLASS="literal"
>session.entropy_length</TT
> specifies the number
of bytes which will be read from the file specified
above. Defaults to <TT
CLASS="literal"
>0</TT
> (disabled).
</P
></LI
><LI
><P
> <TT
CLASS="literal"
>session.use_cookies</TT
> specifies whether the
module will use cookies to store the session id on the client
side. Defaults to <TT
CLASS="literal"
>1</TT
> (enabled).
</P
></LI
><LI
><P
> <TT
CLASS="literal"
>session.cookie_path</TT
> specifies path to set
in session_cookie. Defaults to <TT
CLASS="literal"
>/</TT
>.
</P
></LI
><LI
><P
> <TT
CLASS="literal"
>session.cookie_domain</TT
> specifies domain to
set in session_cookie. Default is none at all.
</P
></LI
><LI
><P
> <TT
CLASS="literal"
>session.cache_limiter</TT
> specifies cache control
method to use for session pages (nocache/private/public).
Defaults to <TT
CLASS="literal"
>nocache</TT
>.
</P
></LI
><LI
><P
> <TT
CLASS="literal"
>session.cache_expire</TT
> specifies time-to-live
for cached session pages in minutes, this has no effect for
nocache limiter. Defaults to <TT
CLASS="literal"
>180</TT
>.
</P
></LI
></UL
>
<DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Note: </B
>
Session handling was added in PHP 4.0.
</P
></BLOCKQUOTE
></DIV
>
</P
></DIV
><DIV
CLASS="TOC"
><DL
><DT
><B
>Table of Contents</B
></DT
><DT
><A
HREF="function.session-start.html"
>session_start</A
> — Initialize session data</DT
><DT
><A
HREF="function.session-destroy.html"
>session_destroy</A
> — Destroys all data registered to a session</DT
><DT
><A
HREF="function.session-name.html"
>session_name</A
> — Get and/or set the current session name</DT
><DT
><A
HREF="function.session-module-name.html"
>session_module_name</A
> — Get and/or set the current session module</DT
><DT
><A
HREF="function.session-save-path.html"
>session_save_path</A
> — Get and/or set the current session save path</DT
><DT
><A
HREF="function.session-id.html"
>session_id</A
> — Get and/or set the current session id</DT
><DT
><A
HREF="function.session-register.html"
>session_register</A
> —
Register one or more variables with the current session
</DT
><DT
><A
HREF="function.session-unregister.html"
>session_unregister</A
> —
Unregister a variable from the current session
</DT
><DT
><A
HREF="function.session-unset.html"
>session_unset</A
> —
Free all session variables
</DT
><DT
><A
HREF="function.session-is-registered.html"
>session_is_registered</A
> —
Find out if a variable is registered in a session
</DT
><DT
><A
HREF="function.session-get-cookie-params.html"
>session_get_cookie_params</A
> —
Get the session cookie parameters
</DT
><DT
><A
HREF="function.session-set-cookie-params.html"
>session_set_cookie_params</A
> —
Set the session cookie parameters
</DT
><DT
><A
HREF="function.session-decode.html"
>session_decode</A
> — Decodes session data from a string</DT
><DT
><A
HREF="function.session-encode.html"
>session_encode</A
> —
Encodes the current session data as a string
</DT
><DT
><A
HREF="function.session-set-save-handler.html"
>session_set_save_handler</A
> —
Sets user-level session storage functions
</DT
><DT
><A
HREF="function.session-cache-limiter.html"
>session_cache_limiter</A
> — Get and/or set the current cache limiter</DT
></DL
></DIV
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="function.shm-remove-var.html"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="manual.html"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="function.session-start.html"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>shm_remove_var</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="funcref.html"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>session_start</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
|