Book Image

AJAX and PHP: Building Responsive Web Applications

Book Image

AJAX and PHP: Building Responsive Web Applications

Overview of this book

Assuming a basic knowledge of PHP, XML, JavaScript and MySQL, this book will help you understand how the heart of AJAX beats and how the constituent technologies work together. After teaching the foundations, the book will walk you through numerous real-world case studies covering tasks you'll be likely to need for your own applications: Server-enabled form-validation page Online chat collaboration tool Customized type-ahead text entry solution Real-time charting using SVG Database-enabled, editable and customizable data grid RSS aggregator application A server-managed sortable list with drag&drop support using the script.aculo.us JavaScript toolkit The appendices guide you through installing your working environment, using powerful tools that enable debugging, improving, and profiling your code, working with XSLT and XPath. From the Author, Cristian Darie AJAX and PHP: Building Responsive Web Applications is mainly a book for beginners, but when designing its contents we tried to find the ideal blend of topics that would help both novice and experienced web developers make a big step forward. One customer was very kind to let us know, through a review, that we succeeded: "The theory behind all the technologies used is very clearly explained, without boring you with details about obvious things. Right from the first chapter you start learning by examples. The examples can be easily adapted to many web projects and they cover stuff that is both useful and fun." Here are a few examples of such "useful and fun" things that you can find in this book: details on using proxy scripts to work around the security measures in modern browsers client-side and server-side code that doesn't break when fed with special characters (such as <, ", etc) code that works efficiently with Internet Explorer 5, 6 and 7, Firefox, Opera, Safari, and others a very quick introduction to SVG, the new rebel kid of the web (and of the house) client-server communication based on message queues that guarantee that your messages aren't lost on the way, and arrive in the intended order at the destination server-side state management techniques that use query string parameters and database records to keep track of your client's activity simple yet effective error-handling structures that combine JavaScript code and PHP code to report when something bad happens on the client or on the server a live errata page that is updated as soon as anyone reports a suggestion or a correction a friendly AJAX tutorial and many case studies that teach you how to use JavaScript, PHP, MySQL and XML together in order to achieve wonderful results The book's authors and the publisher are listening to your feedback, and appreciate when you invest some time to let them know what you think. The first result of this collaboration is an updated version of the AJAX Chat case study that uses (and teaches) JSON instead of XML. Find this new chapter in the code download or on my website. Thanks for reading such a long message. Have fun!" Cristian Darie.
Table of Contents (17 chapters)
AJAX and PHP
Credits
About the Authors
About the Reviewers
Preface
Index

Preparing Your *nix Playground


Almost all the UNIX and Linux distributions include Apache, PHP, and MySQL; however, you should check the versions of these programs. It would be good to have MySQL 4.1 or newer, and it’s very important to have at least PHP 5. The code in this book will not work with older versions of PHP.

Installing Apache

To install Apache on your Unix-based server, follow these simple steps:

  1. First, download the latest Apache Unix Source code for your system from http://httpd.apache.org/download.cgi and decompress it with a command such as:

    tar -zxvf httpd-2.0.55.tar.gz
  2. To compile and install the Apache Web Server on your system, go to the folder containing the sources and execute the following commands, while logged in as root:

    ./configure --prefix=/usr/local/apache2 --enable-so --enable-ssl --with-ssl --enable-auth-digest
    
    make
    
    make install

Installing MySQL

The official website of MySQL is http://www.mysql.com. At the time of this writing the latest stable version is MySQL 5.0, and you can download it from http://dev.mysql.com/downloads/mysql/5.0.html. However, it’s good to know that we made our SQL queries compliant with the SQL 92 standard, so you should be able to reuse them with other database systems with minimum of translation effort. Chapter 2 of the MySQL 5 manual covers installation procedures for all supported platforms, and you can read it here: http://dev.mysql.com/doc/refman/5.0/en/installing.html.

If your Linux distribution supports RPMs, you’ll need to download the RPMs for Server, Client programs, and Libraries and header files. Install MySQL as explained in the manual at http://dev.mysql.com/doc/refman/5.0/en/linux-rpm.html. If your platform doesn’t support RPMs, install MySQL as explained at http://dev.mysql.com/doc/refman/5.0/en/installing-binary.html.

After installing MySQL, you should change the MySQL administrator’s password (the root@localhost user), which is blank by default. Read more about MySQL passwords at http://dev.mysql.com/doc/mysql/en/Passwords.html. One way to change root’s password is to execute:

mysqladmin -u root password ‘your_new_password.’

Alternatively, you can access MySQL through a console program or by using a database administration tool such as phpMyAdmin, and execute this command:

SET PASSWORD FOR root@localhost=PASSWORD(‘your_new_password’);

You can now test your MySQL server by executing the following command in your console:

#mysql -u root -p

Installing PHP

Every time you want to get a new PHP library working on Linux, you need to recompile the PHP module. That’s why it’s recommended to make a good compilation, with all the needed libraries, from the start.

  1. Go to http://www.php.net/downloads.php and get the complete source code archive of PHP 5.x and extract the contents into a directory. At the time of writing, the latest PHP version was 5.1.2.

  2. Go to the folder where you extracted the PHP source and execute the following commands:

    ./configure --with-config-file-path=/etc --with-mysql=/usr/include/mysql 
    --with-apxs2=/usr/local/apache2/bin/apxs --with-zlib --with-gd --with-xsl
    
    make
    
    make install

    Note

    If you are compiling PHP for XAMPP, you need to use the following configure command instead:

    ./configure --with-config-file-path=/opt/lampp/etc --with-mysql=/opt/lampp --with-apxs2=/opt/lampp/bin/apxs --with-zlib --with-gd
    

    After executing make and make install, you need to copy the newly created php_src/libs/libphp5.so file to /opt/lampp/modules/libphp5.so.

  3. Copy php.ini-recommended to /etc/php.ini by executing the following command:

    cp php.ini-recommended /etc/php.ini.
  4. Open the Apache configuration file (httpd.conf), find the DirectoryIndex entry, and make sure you have index.php at the end of the line:

    DirectoryIndex index.html index.html.var index.php
  5. Restart your Apache Web Server using the following command:

    /usr/local/apache2/bin/apachectl restart
  6. Create a folder called ajax under the htdocs folder (by default /usr/local/apache2/htdocs/).

  7. To make sure your PHP installation works, create a file named test.php in the ajax folder you’ve just created, with the following contents in it:

    <?php
    phpinfo();
    ?>
  8. Finally, point your web browser to http://localhost/test.php, to ensure PHP was correctly installed under Apache (you should get a page similar to Figure A.3).