PHP and MySQL Web Development - P89 ppsx

5 158 0
PHP and MySQL Web Development - P89 ppsx

Đang tải... (xem toàn văn)

Thông tin tài liệu

412 Chapter 19 Generating Images You can use a similar approach to draw line graphs, and even pie charts, if you are good at mathematics. Other Image Functions In addition to the image functions we have used in this chapter, there are functions to let you draw curved lines (ImageArc()) and polygons (ImagePolygon()), as well as varia- tions on the ones we have used here.Always begin by sketching what you want to draw, and then you can hit the manual for any extra functions you might need. Further Reading A lot of reading material is available online. If you’re having trouble with the image functions, it sometimes helps to look at the source documentation for gd because the PHP functions are wrappers for this library.The gd documentation is available at http://www.boutell.com/gd/ There are also some excellent tutorials on particular types of graph applications, particu- larly at Zend and Devshed: http://www.zend.com http://devshed.com The bar chart application in this chapter was inspired by the dynamic bar graph script written by Steve Maranda, available from Devshed. Next In the next chapter, we’ll tackle PHP’s handy session control functionality, new in PHP 4. 24 525x ch19 1/24/03 2:57 PM Page 412 20 Using Session Control in PHP THIS CHAPTER WILL DISCUSS THE SESSION control functionality in PHP 4. We will cover n What session control is n Cookies n Setting up a session n Session variables n Sessions and authentication What Session Control Is You might have heard it said that “HTTP is a stateless protocol.”What this means is that the protocol has no built-in way of maintaining state between two transactions.When a user requests one page, followed by another, HTTP does not provide a way for us to tell that both requests came from the same user. The idea of session control is to be able to track a user during a single session on a Web site. If we can do this, we can easily support logging in a user and showing content according to her authorization level or personal preferences.We can track the user’s behavior.We can implement shopping carts. In earlier versions of PHP, session control was supported via PHPLib, the PHP Base Library, which is still a useful toolkit.You can read about it at http://phplib.sourceforge.net/ As of version 4, PHP includes native session control functions.They are conceptually similar to PHPLib, but PHPLib offers some extra functionality. If you find that the native functions do not quite meet your needs, you might want to take a look at it. 25 525x ch20 1/24/03 2:57 PM Page 413 414 Chapter 20 Using Session Control in PHP Basic Session Functionality Sessions in PHP are driven by a unique session ID, a cryptographically random number. This session ID is generated by PHP and stored on the client side for the lifetime of a session. It can be either stored on a user’s computer in a cookie, or passed along through URLs. The session ID acts as a key that allows you to register particular variables as so-called session variables.The contents of these variables are stored at the server.The session ID is the only information visible at the client side. If, at the time of a particular connection to your site, the session ID is visible either through a cookie or the URL, you can access the session variables stored on the server for that session. By default, the session variables are stored in flat files on the server. (You can change this to use a database if you are willing to write your own function—more on this in the section “Configuring Session Control.”) You have probably used Web sites that store a session ID in the URL. If there’s a string of random looking data in your URL, it is likely to be some form of session con- trol. Cookies are a different solution to the problem of preserving state across a number of transactions while still having a clean looking URL. What Is a Cookie? A cookie is a small piece of information that scripts can store on a client-side machine. You can set a cookie on a user’s machine by sending an HTTP header containing data in the following format: Set-Cookie: NAME=VALUE; [expires=DATE;] [path=PATH;] [domain=DOMAIN_NAME;] [secure] This will create a cookie called NAME with the value VALUE.The other parameters are all optional.The expires field sets a date beyond which the cookie is no longer relevant. (Note that if no expiry date is set, the cookie is effectively permanent unless manually deleted by you or the user.) Together, the path and domain can be used to specify the URL or URLs for which the cookie is relevant.The secure keyword means that the cookie will not be sent over a plain HTTP connection. When a browser connects to an URL, it first searches the cookies stored locally. If any of them are relevant to the URL being connected to, they will be transmitted back to the server. Setting Cookies from PHP You can manually set cookies in PHP using the setcookie() function. It has the fol- lowing prototype: int setcookie (string name [, string value [, int expire [, string path [, string domain [, int secure]]]]]) 25 525x ch20 1/24/03 2:57 PM Page 414 415 Basic Session Functionality The parameters correspond exactly to the ones in the Set-Cookie header mentioned previously. If you set a cookie as setcookie ('mycookie', 'value'); when the user visits the next page in your site (or reloads the current page), you will have access to the cookie via either $_COOKIE['mycookie'] or $HTTP_COOKIE_VARS["mycookie"]. (Or, if you have register_globals turned on, directly as $mycookie.) You can delete a cookie by calling setcookie() again with the same cookie name and an expiry time in the past.You can also set a cookie manually via the Header() function and the cookie syntax given previously. One tip is that cookie headers must be sent before any other headers, or they will not work. (This is a cookie limitation rather than a PHP limitation.) Using Cookies with Sessions Cookies have some associated problems: Some browsers do not accept cookies, and some users might have disabled cookies in their browsers.This is one of the reasons PHP ses- sions use a dual cookie/URL method. (We’ll discuss more about this in a minute.) When you are using PHP sessions, you will not have to manually set cookies.The ses- sion functions will take care of this for you. You can use the function session_get_cookie_params() to see the contents of the cookie set by session control. It returns an associative array containing the elements lifetime, path,and domain. You can also use session_set_cookie_params($lifetime, $path, $domain [, $secure]); to set the session cookie parameters. If you want to read more about cookies, you can consult the cookie specification on Netscape’s site: http://home.netscape.com/newsref/std/cookie_spec.html (You can probably ignore the fact that this document calls itself a “preliminary specifica- tion”—it’s been that way since 1995.) Storing the Session ID PHP will use cookies by default with sessions. If possible, a cookie will be set to store the session ID. The other method it can use is adding the session ID to the URL.You can set this to happen automatically if you compile PHP with the enable-trans-sid option.This is the default from PHP 4.2 onward. 25 525x ch20 1/24/03 2:57 PM Page 415 416 Chapter 20 Using Session Control in PHP Alternatively, you can manually embed the session ID in links so that it is passed along.The session ID is stored in the constant SID.To pass it along manually, you add it to the end of a link similar to a GET parameter: <a href="link.php?<?=SID?>"> It is generally easier to compile with enable-trans-sid, where possible. Implementing Simple Sessions The basic steps of using sessions are n Starting a session n Registering session variables n Using session variables n Deregistering variables and destroying the session Note that these steps don’t necessarily all happen in the same script, and some of them will happen in multiple scripts. Let’s talk about each of these steps in turn. Starting a Session Before you can use session functionality, you need to actually begin a session.There are three ways you can do this. The first, and simplest, is to begin a script with a call to the session_start() func- tion: session_start(); This function checks to see whether there is already a current session ID. If not, it will create one. If one already exists, it essentially loads the registered session variables so that you can use them. It’s a good idea to call session_start() at the start of all your scripts that use ses- sions. Second, a session will be started when you try to register a session variable using ses- sion_register() (see the next section). The third way you can begin a session is to set PHP to start one automatically when someone comes to your site.You can do this with the session.auto_start option in your php.ini file—we’ll look at this when we discuss configuration. Registering Session Variables Registering session variables has recently changed in PHP. Session variables are stored in the superglobal array $_SESSION as of PHP 4.1, and also in the older $HTTP_SESSION_VARS. In order to create a session variable you simply set an element in one of these arrays, as follows: 25 525x ch20 1/24/03 2:57 PM Page 416 . versions of PHP, session control was supported via PHPLib, the PHP Base Library, which is still a useful toolkit.You can read about it at http://phplib.sourceforge.net/ As of version 4, PHP includes. Session Control in PHP Basic Session Functionality Sessions in PHP are driven by a unique session ID, a cryptographically random number. This session ID is generated by PHP and stored on the client. automatically if you compile PHP with the enable-trans-sid option.This is the default from PHP 4.2 onward. 25 525x ch20 1/24/03 2:57 PM Page 415 416 Chapter 20 Using Session Control in PHP Alternatively,

Ngày đăng: 07/07/2014, 03:20

Từ khóa liên quan

Mục lục

  • PHP and MySQL Web Development

  • Copyright

  • Table of Contents

  • Introduction

  • Part I: Using PHP

    • Chapter 1: PHP Crash Course

    • Chapter 2: Storing and Retrieving Data

    • Chapter 3: Using Arrays

    • Chapter 4: String Manipulation and Regular Expressions

    • Chapter 5: Reusing Code and Writing Functions

    • Chapter 6: Object-Oriented PHP

    • Part II: Using MySQL

      • Chapter 7: Designing Your Web Database

      • Chapter 8: Creating Your Web Database

      • Chapter 9: Working with Your MySQL Database

      • Chapter 10: Accessing Your MySQL Database from the Web with PHP

      • Chapter 11: Advanced MySQL

      • Part III: E-commerce and Security

        • Chapter 12: Running an E-commerce Site

        • Chapter 13: E-commerce Security Issues

        • Chapter 14: Implementing Authentication with PHP and MySQL

        • Chapter 15: Implementing Secure Transactions with PHP and MySQL

        • Part IV: Advanced PHP Techniques

          • Chapter 16: Interacting with the File System and the Server

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan