tailieunhanh - PHP and MySQL Web Development - P93

PHP and MySQL Web Development - P93: PHP and MySQL Web Development teaches the reader to develop dynamic, secure, commercial Web sites. Using the same accessible, popular teaching style of the first edition, this best-selling book has been updated to reflect the rapidly changing landscape of MySQL and PHP. | 432 Chapter 21 Other Useful Features the session control functions serialize session variables in order to store them between HTTP requests. However you might still want to store a PHP array or object in a file or database. If you do there are two functions you need to know how to use serialize and unse-rialize . You can call the serialize function as follows serial_object serialize my_object If you want to know what the serialization actually does take a look at what is returned from serialize. It turns the contents of an object or array into a string. For example we can look at the output of running serialize on a simple employee object defined and instantiated thus class employee var name var employee_id this_emp new employee this_emp- name Fred this_emp- employee_id 5324 If we serialize this and echo it to the browser the output is O 8 employee 2 s 4 name s 4 Fred s 11 employee_id i 5324 It is fairly easy to see the relationship between the original object data and the serialized data. As the serialized data is just text you can write it to a database or whatever you like. Be aware that you should addslashes to any data before writing it to a database as per can see the need for this by noting the quotes in the previous serialized string. To get the object back call unserialize new_object unserialize serial_object Obviously if you called addslashes before putting the object into a database you will need to call stripslashes before unserializing the string. Another point to note when serializing classes or using them as session variables PHP needs to know the structure of a class before it can reinstantiate the you will need to include the class definition file before calling session_start or unserialize . Getting Information About the PHP Environment 433 Getting Information About the PHP Environment A number of functions can be used to find out information about how PHP is configured. Finding Out What Extensions Are Loaded You can .

TỪ KHÓA LIÊN QUAN