Using Simple Ajax and OpenSSL
The example and jobs database I explain in this section is not really a login application, but it demonstrates exactly how Ajax and OpenSSL work together in PHP. This application contains two pages:
- Uses a HTML form to select a user and implements the Ajax mechanism
- Makes the connection to the jobs database and reads from the users table (see Figure 5) all the information about the selected user
To encrypt the password, I use the OpenSSL library. To use this library you should activate this extension from php.ini (
;extension=php_openssl.dll
) and then download the DLL files required by this extension (libeay32.dll and ssleay32.dll), which should be in the PHP
and Windows/System32
folders. PHP 5.x and 6.x have the file in the extras/openssl
directory.This code creates a key file called private.pem that uses 1024 bits.
$ openssl genrsa -out private.pem 1024
Theprivate.pem file actually has both private and public keys, so you should extract the public one from it.
$ openssl rsa -in private.pem -out public.pem -outform PEM -pubout
The 1.html HTML page (see Listing 2) implements the Ajax mechanism and reveals an HTML form that interacts with the getuser.php PHP script to read from the jobs database and output the chosen value from the select component.
The getuser.php script (see Listing 3) outputs all the details of the user selected in the HTML form from the users table.
Conclusion
In this article you learned two approaches for writing a PHP login application: one using the aSSL library and the other using simple Ajax and the OpenSSL library.
About the Author
Octavia Andreea Anghel is a senior PHP developer currently working as a primary trainer for programming teams that participate at national and international software-development contests. She consults on developing educational projects at a national level. She is a coauthor of the book “XML Technologies: XML in Java” (Albastra, ISBN 978-973-650-210-1), for which she wrote the XML portions. In addition to PHP and XML, she’s interested in software architecture, web services, UML, and high-performance unit tests. to e-mail her.