Your First Web Server Application lab
Upcoming SlideShare
Loading in...5
×
 

Your First Web Server Application lab

on

  • 3,388 views

 

Statistics

Views

Total Views
3,388
Views on SlideShare
3,388
Embed Views
0

Actions

Likes
0
Downloads
101
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft Word

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Your First Web Server Application lab Your First Web Server Application lab Document Transcript

  • Your First Web Server Application lab 1.1 PHP 1.1.1 Overview PHP is just one of many popular Web server side open source scripting languages. It is widely used for Web application development. There are over half million server pages developed by PHP. It is still growing very fast. PHP is often embedded into HTML to enhance functions of HTML. PHP allows you to interact with a HTML page dynamically. PHP collect data from page where user provides and then process and utilize data to create a dynamic home page output. PHP can do all tasks that ASP and JSP can do plus it is free. Why is PHP so popular? The answer is following: • PHP is Open Source and free • PHP is much compact than Java and .NET in term of its system size • PHP is portable and runs on Linux, Windows, and Mac platforms • PHP is a server-side script language easy to be adopted • PHP has fast connections to popular databases compared with Perl • PHP is integrated with the Open Source MySQL database • PHP is an OO interpreter language • PHP Web pages work with all popular major browsers • PHP has integrated linking to XML PDF, SWF, and Java for dynamic creation and processing • PHP syntax is familiar to C, Perl, and Java PHP marries HTML well because PHP is designed to be used along with HTML. You can embed PHP into HTML document, or put HTML tags in PHP coding seamlessly. In addition, PHP gets alone with MySQL very well. These two make up the best combination for data-driven Web application cross-platform. PHP can also support a massive number of databases, including Informix, Oracle, Sybase, and ODBC drivers. 1.1.2 Practice lab for PHP Web application on Apache In this section, you will be given a hands-on lab on a “Hello user” Apache server and PHP Web project. A PHP based on HTTP server page is populated on an Apache Web server. If a Web client accesses this page and fill in his/her name as <name> then the PHP page will dynamically create a response page which responds a greeting statement “Hello <name>”. After installing Apache server, you can download and install PHP with http://www.php.net/downloads.php. The installation manual at http://www.php.net/install.windows may be helpful. The binary installer is very easy to use. However, if you want to use manual installation, you need to extract the php 5.x into C:php. You may choose a different location but do not leave any spaces in the path (like C:Program FilesPHP) as some Web servers will crash if you do so. In your php directory, there are two files: php.ini-dist and php.ini-recommended. Change one of them into php.ini (php.ini-recommended is preferred).
  • First, edit php.ini file as follows: doc_root = C:xampphtdocs extension_dir = "c:xamppphpext" Next, edit Apache configuration file httpd.conf. Add following lines into it, the PHP would work as a module: ### LoadModule php5_module "c:/xampp/php/php5apache2_2.dll" AddType application/x-httpd-php.php PHPIniDir "c:/xampp/php" ### The Web page of http://www.ricocheting.com/server/php.html is a good getting started tutorial. You can create a file named test.php in your apache default folder: C:xampphtdocs The php template is shown as . . . <?php phpinfo(); ?> . . . Save your configuration and start up the Apache server. Go to http://localhost/test.php, and you will see:
  • Edit the HTML template provided by PHP as follows: <html> <head> <title>hello user</title> </head> <body> <form action="test.php" method="post"> Name: <input type="text" name="name" /> <input type="submit" value="submit"/> </form> Hello! <?php echo $_POST["name"]; ?> </body> </html> This is a html file with embedded php tag <?php ?>.
  • This php tags just simply echo the input name on the page. If you type John in the name box as follows: You will see a PHP responded page saying Hello John as follows. 1.1.3 PHP with XAMPP This tutorial shows the same hello user example above implemented by html client and PHP server interaction on an Apache server. The XAMPP(LinuX, Apache, MySQL, PHP and Perl) is Apache distribution containing MySQL, PHP and Perl. XAMPP is very easy to install and to use, just download, extract and start. Here the XAMPP is used which comes with the pre compiled apache and PHP. You can download XAMPP for Windows in http://www.apachefriends.org/en/xampp- windows.html Step 1: First you need to extract the XAMPP and double click the setup file in order to start the server.
  • The install wizard of XAMPP win32. After the installation is complete, go to the XAMPP folder. You can use the XAMPP Control Panel to start/stop all servers and also install/uninstall services.
  • Step 2: You open a browser and type http://localhost/ You find a page similar to the figure below. This shows that the server is running. Inside the XAMPP folder, there is a folder named htdocs. Inside this folder you open a Notepad and type the following code.
  • 1. Create a phpindex.php file as follows. <html> <head> <title>hello user</title> </head> <body> <form action="testphp.php" method="post"> Name: <input type="text" name="username" /> <input type="submit" value="submit"/> </form> </body> </html> 2. Create a new file testphp.php and type this code. <?php $content = $_POST['username']; echo "Hello !" . $content . ".<br />"; echo "You have visited the Server Page" ?> Step 3:Output Now open the browser and type http://localhost/phpindex.php If you enter some name in the Text Field, the page response you automatically. You may have noticed that there is not any submit button because the response is posted instantly and asynchronously by Ajax.
  • 1.1.4 PHP with WAMP (The system is not installed with wampserver, instead it is installed with Xampp server- This is just for reading) This tutorial shows the same hello user example above implemented with html client and PHP server interaction on an Apache server. The WAMP (Windows, Apache, MySQL, and PHP) is very easy to install and to use, just download, extract and start. SQLitemanager, The Phpmyadmin comes along with WAMP server which gives a very good user interface to manage MySQL. You can download the latest WAMP Server in http://www.wampserver.com/en/ . Step 1: First you need to run the WAMP setup and extract it to your desired folder; for example: C:/wamp. To start the WAMP Server, first click start wampserver, then find the server near the taskbar to start the Apache and MySQL Server. Now Choose Start All Services.
  • Step 2: To check whether the server has started, open a browser and type http://localhost/ You find a page similar to the figure below. This shows that the server is running. Inside the WAMP folder, there is a folder named www. Inside this folder you open a Notepad and type the following code.
  • 1. Create a phpindex.php file as follows. <html> <head> <title>hello user</title> </head> <body> <form action="testphp.php" method="post"> Name: <input type="text" name="username" /> <input type="submit" value="submit"/> </form> </body> </html> 2. Create a new file testphp.php and type this code. <?php $content = $_POST['username']; echo "Hello !" . $content . ".<br />"; echo "You have visited the Server Page" ?> Step 3: Output Now open the browser and type http://localhost/phpindex.php or you could type it in http://localhost and browse through the projects and select phpindex.php 1.2 NetBeans and Tomcat 1.2.1 Overview of NetBeans The NetBeans IDE is a free, open-source Integrated Development Environment (IDE) for Java software developers. It covers all Java Standard Edition (Java SE), Java Enterprise Edition(Java EE, was called J2EE before), Java Micro Edition( was called J2ME) all in one IDE environment. This IDE runs on almost all platforms including Windows, Linux, Solaris, and the Mac OS. It is easy to install and very convenient to develop any Java based cross-platform desktop, enterprise, Web and mobile applications. It is also a very useful component-based software development (CBSD) tool to create, reuse, and deploy Java jar, war, and ear components. NetBeans provides
  • • Swing GUI Builder , Integrated CVS Version Control, NetBeans Debugger with JUnit, Developer Collaboration for project sharing in real time, and Ant-based Project System and Customizable Environment tools; • Source Code Editor for Java SE and JSP Development , XML Editor for DTD, Schema, and CSS, plus C/C++ Development; • Web server Application Development tools with JSP, Servlets, JavaServer Faces(JSF), and Struts (Java Enterprise Edition), Sun Application Server, Weblogic, and JBoss ; • RJB developments with ear-jar deployment; • Visual Web Application Development for AJAX, CSS and JSF, Web Service Development (SOA) with Java Enterprise Edition for WSDL and web services implementation development and deployment; BEPL design, development, and testing tools; • Web Client Platform Development; • Mobile Application Development (Java Micro Edition) including Connected Limited Device Configuration (CLDC), CDC, and Mobile Information Device Profile (MIDP); Java ME Development Cycle Tools; • UML modeling tools with code generators • Embedded Tomcat server Just like Eclipse, The NetBeans is a popular IDE widely used by Java developers. 1.3 NetBeans PHP Plugin In addition to being an excellent Java development platform, the NetBeans IDE has a PHP plugin that can aid with developing PHP Web applications by providing syntax coloring, code completion, and debugging. It’s fairly simple to install and get setup. The first thing you need in place is the Apache Web server and PHP, which have already been covered in this Appendix. If you haven’t set them up yet, then go back and follow the instructions. The other important piece of software you will want in place before installing the PHP plugin is the PHP debugger, available for download at http://www.xdebug.org/docs/install. Download XDebug and place it anywhere on your hard drive, then add the following to the php.ini file, but replace “c:/xdebug/php_xdebug-2.0.2-5.2.5.dll” with the location of your copy of the XDebug dll. zend_extension_ts="c:/xdebug/php_xdebug-2.0.2-5.2.5.dll" xdebug.remote_enable=1 Now, to install and setup the PHP plugin, follow these steps. 1. Check if the PHP plugin is already installed: choose Tools > Plugins and switch to the Installed tab.
  • 2. If PHP is not on the list of installed plugins, switch to the Available Plugins tab, select the PHP option in the list, and click the Install button. 3. In the NetBeans IDE main screen, switch to the Services tab, which shows a tree of available databases and web server profiles. 4. Position the cursor on the Web Servers node, and from the context menu choose Add Web Server. The Add New Web Server Record dialog box opens. 5. In the Connection Name text field, enter the name of the web server profile and from the Server Type drop-down list, choose one of the connection types: a. Local Web Server with file access. This option assumes that you have a web server installed on your local computer. Every time you run your PHP project, the IDE copies your source files to a specified directory under the web server document root. If you set up the Apache Web server locally per the instructions earlier in this Appendix, then this is the option you should choose. b. Remote Web Server with FTP access. This option allows you to deploy your PHP files to a remote web server via FTP.
  • 6. Click Next and choose the Manual Configuration option. 7. In the Apache config file Location text box, specify the location of the httpd.conf file. 8. From the Choose Host Name drop-down list, select the relevant virtual host. In a development environment, typically, only one choice is available. 9. To specify the server manually, from the Server Location dropdown list, choose the relevant server. 10. Click Next. 11. In the Base Directory text field, enter the subfolder of the Document Root where your PHP files will be copied. To deploy your web applications to the web server root, leave this field empty. 12. Click Finish. Now you can create new PHP projects and deploy your project to the Apache Web server to test. 1. In the Projects window, choose File > New Project. 2. The New Project wizard opens. 3. In the Categories list, select PHP. 4. In the Projects area, choose PHP Project to create an empty project and click Next. To use pre-existing source files, choose PHP Project with Existing Sources.
  • 5. At the Name and Location page, enter the name of the project in the Project Name field. NetBeans will automatically create a new folder for your project. 6. In the Project Location text field, specify the location for the new project folder or accept the defaults. Note: The contents of the fields change while you type in the Project Location text field. 7. Select the Set as Main Project checkbox if you have more than one PHP project in your projects tree and want the current project to run first. 8. To create an index file, select the Create Index File checkbox and specify the field name in the text field. 9. Click Next. The Web Server Configuration dialog box opens. 10. From the Configured Web Servers drop-down list, choose the name of the appropriate web server profile. You can view the following settings for the selected web server profile: 1. HTTP Path. This is a host name or IP address of the HTTP server and the port number the server listens to. 2. File access settings for the selected profile, which are the directories where the web server looks for files (Document Path if you are using a local server or FTP Path if you are using a remote server). 11. In the Context Path text fields, enter the name of the subfolder in the Document Root where you want your PHP files to be deployed. For more information on the PHP plugin see the Web site at http://php.netbeans.org/.
  • 1.4 MySQL 1.4.1 Overview The MySQL is the most popular open source relational SQL database system in the world. The MySQL database server provides a multi-threaded, multi-user, and robust SQL information management environment for mission-critical, heavy-load production systems. MySQL can be used as back-end for any information management systems and accessible with almost all programming languages or script languages. A MySQL database can be hosted on any Web servers and is often coupled with PHP for Web application so that the information on MySQL database can be accessed from anywhere in the world with any Web clients. PHP can collect data and then send data to MySQL database, and MySQL can in turn store the information. PHP can send request to database, get response from database. PHP and MySQL combine together to make Web application easier and more powerful .in production of interactive and dynamic web pages on the fly. HTML can create useful and well formatted Web pages. For example, Web clients can use PHP to browse a on-line shopping catalog stored in a MySQL database, create a shopping cart for clients to dynamically add or remove items to or from shopping cart. The MySQL can keep track of customer information, transaction information, shipping, and handling information. 1.4.2 Practice lab for MySQL, PHP, and Apache Let’s practice AMP (Apache, MySQL, and PHP) in this lab. Download MySQL Community Server from http://dev.mysql.com/downloads/index.html, the current release version is MySQL5.x.
  • Follow the instructions and choose a proper version for your OS, in my case: Windows Essentials (x86). Run the .msi file to install MySQL,
  • After complete the wizard, you can launch the configuration wizard automatically. Here is my MySQL configuration; you may configure it differently as you need.
  • Continue on the MySQL Server Instance Configuration wizard.
  • Continue on the configuration with password settings.
  • Now you completed MySQL server configurations and MySQL should run on your machine as a service. Next, Go to Start->All Programs->MySQL->MySQL Server 5.0->MySQL Server Command Line Client ,and then type the password you set up to login MySQL. In order to let PHP work with MySQL you need to have PHP installed as demonstrated in the previous tutorial. In the configuration file c:phpphp.ini, uncomment following two lines: extension=php_mysql.dll extension=php_mysqli.dll Copy the file libmysql.dll from c:/xampp/php/ into C:/WINDOWS/system32. Make sure you have php_mysql.dll and php_mysqli.dll in your c:/xampp/php/ext Create a test.php file in apache root folder /htdocs, and add the following code in the file: . . . <?php $con = mysql_connect('localhost','root','admin'); if (!$con) { die('Could not connect: ' . mysql_error()); } else
  • { echo"connected"; } mysql_close($con); ?> . . . After startup your Apache server and visit http://localhost/test.php you should see a single line of “connected” in your browser. This indicates that you have linked Apache server, php and MySQL successfully. 1.4.3 A simple MySQL application In this section, you will create a toy on-line MYSQL application with PHP which will verify any names requested on line against the names stored in a MySQL database. First, login MySQL (Start->All Programs->MySQL->MySQL Server 5.0->MySQL Command Line Client) Next, Execute the following SQL commands to create a MySQL table with a single name data field(string type) and insert few data records. mysql> create database my_db; mysql> use my_db; mysql> create table nameTB (name varchar(20)); mysql> insert into nameTB (name) values ('John'); mysql> insert into nameTB (name) values ('Smith'); mysql> insert into nameTB (name) values ('Mike'); Enter the following code to your test.php . . . <?php $con = mysql_connect('localhost','root','admin'); if (!$con) { die('Could not connect: ' . mysql_error()); exit(); } mysql_select_db("my_db", $con); $name = $_POST["name"]; if($name) {
  • $query = "SELECT * FROM nameTB WHERE name = '" . $name . "';"; $result = mysql_query($query); if(mysql_fetch_array($result)) { echo "Welcome back, " . $name; } else { echo "Sorry, you are not registered"; } } mysql_close($con); ?> <form action="test.php" method="post"> Name: <input type="text" name="name" /> <input type="submit" value="submit"/> </form> . . . Now, if your typed name matches any name in the table (John, Smith, Mike), it will be recognized. Otherwise, your request will be refused.
  • An unregistered user will be rejected as follows.