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

Unit 9: Web Techniques



            The page created by above example contains a link to another page, shown in below given   Notes
            example, that uses the colour preferences by accessing the $_COOKIE array.
                          Using the colour preferences with cookies
            <html>
            <head>

            <title>Front Door</title>
            </head>
            <?

            php $bg = $_COOKIE[‘bg’];
            $fg = $_COOKIE[‘fg’];
            ?>

            <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>

            There are plenty of caveats about the use of cookies. Not all clients support or accept cookies,
            and even if the client does support cookies, the user may have turned them off. Furthermore,
            the cookie specification says that no cookie can exceed 4 KB in size, only 20 cookies are allowed
            per domain, and a total of 300 cookies can be stored on the client side. Some browsers may have
            higher limits, but you cannot rely on that. Finally, you have no control over when browsers
            actually expire cookies—if they are at capacity and need to add a new cookie, they may discard
            a cookie that has not yet expired. You should also be careful of setting cookies to expire quickly.
            Expiration times rely on the client’s clock being as accurate as yours. Many people do not have
            their system clocks set accurately, so you cannot rely on rapid expirations.
            Despite these limitations, cookies are very useful for retaining information through repeated
            visits by a browser.

                          Do not use whitespace or semicolons in a cookie name otherwise it will be
                          invalid.




                        Develop a PHP program to set the preferences with cookies.
            9.6.2 Sessions

            PHP has built-in support for sessions, handling all the cookie manipulation for you to provide
            persistent variables that are accessible from different pages and across multiple visits to the
            site. Sessions allow you to easily create multipage forms (such as shopping carts), save user
            authentication information from page to page, and store persistent user preferences on a site.
            Each first-time visitor is issued a unique session ID. By default, the session ID is stored in a
            cookie called PHPSESSID.
            Every session has a data store associated with it. You can register variables to be loaded from
            the data store when each page starts and saved back to the data store when the page ends.



                                             LOVELY PROFESSIONAL UNIVERSITY                                   219
   220   221   222   223   224   225   226   227   228   229   230