Page 227 - Open Soource Technologies 304.indd
P. 227

Unit 9: Web Techniques



            $bg = $colors[$bg_name];                                                              Notes
            $fg = $colors[$fg_name]; ?>
            The example given below shows once the session is started, the $bg and $fg variables are created,
            and all the script has to do is use them.
                          Using preferences from sessions

            <?php session_start( ) ?>
            <html>
            <head>

            <title>Front Door</title>
            </head>
            <body bgcolor=”<? = $bg?>” text=”<? = $fg?>”>

            <h1>Welcome to the Store</h1>
            We have many fine products for you to view. Please feel free to browse the aisles and stop
            an assistant at any time. But remember, you break it you bought it!<p> would you like to <a
            href=”prefs.html”>change your preferences?</a> </body> </html>
            Alternatives to Cookies

            By default, the session ID is passed from page to page in the PHPSESSID cookie. However,
            PHP’s session system supports two alternatives: form fields and URLs. Passing the session ID
            via hidden fields is extremely awkward, as it forces you to make every link between pages be
            a form’s submit button.
            The URL system for passing around the session ID, however, is very elegant. PHP can rewrite
            your HTML files, adding the session ID to every relative link. For this to work, though, PHP
            must be configured with the -enable-trans-id option when compiled. There is a performance
            penalty for this, as PHP must parse and rewrite every page. Busy sites may wish to stick with
            cookies, as they do not incur the slowdown caused by page rewriting.
            Custom Storage

            By default, PHP stores session information in files in your server’s temporary directory. Each
            session’s variables are stored in a separate file. Every variable is serialized into the file in a
            proprietary format. You can change all of these things in the php.ini file.
            You can change the location of the session files by setting the session.save_path value in php.ini.
            If you are on a shared server with your own installation of PHP, set the directory to somewhere
            in your own directory tree, so other users on the same machine cannot access your session files.
            PHP can store session information in one of two formats in the current session store—either
            PHP’s built-in format, or WDDX (http://www.openwddx.org/). You can change the format
            by setting the session.serialize_handler value in your php.ini file to either php for the default
            behaviour, or wddx for WDDX format.
            You can write your own functions for reading and writing the registered variables. In this, we will
            develop an example that stores session data in a database, which lets you share sessions between
            multiple sites. It is easy to install your custom session store. First, set session.save_handler to user
            in your php.ini file. Next, write functions for opening a new session, closing a session, reading
            session information, writing session information, destroying a session, and cleaning up after a
            session. Then register them with the session_set_save_handler( ) function:




                                             LOVELY PROFESSIONAL UNIVERSITY                                   221
   222   223   224   225   226   227   228   229   230   231   232