23
CHAPTER 1 EXPLORING THE PHP ENVIRONMENT EXPLORING THE ENVIRONMENT

CHAPTER 1 EXPLORING THE PHP ENVIRONMENT - …homepage.smc.edu/seno_vicky/cs85/unit1/unit1.pdf ·  · 2015-09-13PHP is embedded code within in XHTML code ... unusual because the user

Embed Size (px)

Citation preview

Page 1: CHAPTER 1 EXPLORING THE PHP ENVIRONMENT - …homepage.smc.edu/seno_vicky/cs85/unit1/unit1.pdf ·  · 2015-09-13PHP is embedded code within in XHTML code ... unusual because the user

CHAPTER 1 EXPLORING THE PHP ENVIRONMENTEXPLORING THE ENVIRONMENT

Page 2: CHAPTER 1 EXPLORING THE PHP ENVIRONMENT - …homepage.smc.edu/seno_vicky/cs85/unit1/unit1.pdf ·  · 2015-09-13PHP is embedded code within in XHTML code ... unusual because the user

CHAPTER OBJECTIVE

What is PHP?

Download and install Apache Web Server

Download and install PHP

Download and install MySQL Database

Ensure PHP is on your system

Run a basic diagnostic check of your PHP installation

Add PHP code to a web page

Page 3: CHAPTER 1 EXPLORING THE PHP ENVIRONMENT - …homepage.smc.edu/seno_vicky/cs85/unit1/unit1.pdf ·  · 2015-09-13PHP is embedded code within in XHTML code ... unusual because the user

WHAT CAN PHP DO?

PHP can dynamic generate web-page content

PHP can take form data and process it

PHP can connect and interact with databases

PHP can process user supplied data

PHP can generate and send e-mails

PHP can read and edit files

PHP can process Text

PHP can setup Network connections

And more…

Page 4: CHAPTER 1 EXPLORING THE PHP ENVIRONMENT - …homepage.smc.edu/seno_vicky/cs85/unit1/unit1.pdf ·  · 2015-09-13PHP is embedded code within in XHTML code ... unusual because the user

WHAT IS PHP?

PHP is a scripting language commonly used on web servers.

Stands for “PHP: Hypertext Preprocessor”

It is an Open source scripting language

PHP is embedded code within in XHTML code

Runs on multiple operating systems/web servers (Apache/IIS/Ngix)

The PHP environment is a series of programs and library files. These programs are

unusual because the user never runs them directly. Instead, a user requests a PHP

program from a web server and the server calls upon PHP to process the instructions

in the file. PHP then returns HTML code, which the user sees in the browser.

Page 5: CHAPTER 1 EXPLORING THE PHP ENVIRONMENT - …homepage.smc.edu/seno_vicky/cs85/unit1/unit1.pdf ·  · 2015-09-13PHP is embedded code within in XHTML code ... unusual because the user

FUNDAMENTALS

PHP is embedded within xhtml pages within the tags: <?php … ?>

The short version of these tags can also be used: <? … ?>

Each line of PHP is terminated with a semi-colon.

Example:

<?php

print ‘Hello world!’ ;

?>

Each line of PHP

ends with a ;

(semi-colon)

PHP code must

be between

<?php … php?>

Page 6: CHAPTER 1 EXPLORING THE PHP ENVIRONMENT - …homepage.smc.edu/seno_vicky/cs85/unit1/unit1.pdf ·  · 2015-09-13PHP is embedded code within in XHTML code ... unusual because the user

BASIC PHP SCRIPT

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

<title>Basic PHP Page</title>

</head>

<body>

<p>This is standard HTML.</p>

<?php

**PHP code entered here**

?>

</body>

</html>

PHP code goes between the PHP tags.

Anything outside of the tags is immediately

sent to the browser as HTML.

Must use the .php extension.

Page 7: CHAPTER 1 EXPLORING THE PHP ENVIRONMENT - …homepage.smc.edu/seno_vicky/cs85/unit1/unit1.pdf ·  · 2015-09-13PHP is embedded code within in XHTML code ... unusual because the user

HELLO WORLD!

<html>

<head>

<title>PHP Test</title>

</head>

<body>

<?php echo ‘<p>Hello World!</p>’; ?>

</body>

</html>

If XAMPP is Installed:

• Using Aptana, create a PHP file called hello.php

• Save hello.php to C:\xampp\htdocs

• With a browser, navigate to the file by going to http://localhost/hello.php

You should see: Hello World!

If that worked, replace echo `<p>Hello World!</p>`; with phpinfo(); and run

again…

You should now see a page with lots of information about the PHP

installation.This will become useful later!

Course website has instructions on installing XAMPP and APTANA

Page 8: CHAPTER 1 EXPLORING THE PHP ENVIRONMENT - …homepage.smc.edu/seno_vicky/cs85/unit1/unit1.pdf ·  · 2015-09-13PHP is embedded code within in XHTML code ... unusual because the user

INSTALLING YOUR OWN DEVELOPMENT

ENVIRONMENTINSTALL XAMPP: PHP PREPROCESSPR, APACHE WEB SERVER, MYSQL DATASBASE

INSTALL APTANA: HTML AND PHP EDITOR

Page 9: CHAPTER 1 EXPLORING THE PHP ENVIRONMENT - …homepage.smc.edu/seno_vicky/cs85/unit1/unit1.pdf ·  · 2015-09-13PHP is embedded code within in XHTML code ... unusual because the user

INSTALLING YOUR OWN DEVELOPMENT ENVIRONMENT

You can test your programs without exposing them to the entire world.

It’s easier to configure development environments to work with local servers than to

work with remote ones.

You don’t have to be connected to the web server while you work on your PHP code.

Unit 1 Assignment Part 2, is to setup your home system for PHP/Apache/MySQL

development. Before uploading your assignments to the class web server, you will be

coding and testing your code on your personal computer Apache/PHP/MySQL

environment. It also makes script development and testing easier and faster.

Follow the XAMPP and Aptana installation and configuration instructions provided

on course website.

Page 10: CHAPTER 1 EXPLORING THE PHP ENVIRONMENT - …homepage.smc.edu/seno_vicky/cs85/unit1/unit1.pdf ·  · 2015-09-13PHP is embedded code within in XHTML code ... unusual because the user

PHP/HTML EDITOR

Aptana—An open-source program based on the famous Eclipse Java editor.

It comes with a very solid PHP plugin with syntax coloring, syntax completion, and the

ability to preview your programs directly in the editor.

Has the ability to Run PHP scripts directly in the Aptana console or open up a browser

to XAMPP server locally running on your personal computer.

Page 11: CHAPTER 1 EXPLORING THE PHP ENVIRONMENT - …homepage.smc.edu/seno_vicky/cs85/unit1/unit1.pdf ·  · 2015-09-13PHP is embedded code within in XHTML code ... unusual because the user

RUNNING PHP SCRIPTS ON YOUR LOCAL WEBSERVER

If you use your browser to open an HTML or PHP file from the local file system, say by double-clicking the file in Windows Explorer, a webserver is not involved, so any PHP/server-side code will not be processed. If a form in the HTML file calls a local PHP file, again there is no server involved, so the PHP code will not be executed since it is server-side code which will run only on a webserver.

To run PHP code (the examples provided or your own code) on your local webserver, you need to do the following so your server serves the documents to the client/browser:

1. Make sure your server is running.

2. Save files to c:\xampp\htdocs\foldername

3. In your browser's address box, enter: localhost/foldername

4. The server should display the folders and files in its document folder.

5. Browse to the folder where your PHP scripts are located.

6. Click your HTML or PHP file. Your local webserver will serve the page.

7. If your HTML file has a form and its action attribute has a relative path to a PHP script, submitting the form sends a request to your local webserver to run that script. The response will be the output of the script.

Page 12: CHAPTER 1 EXPLORING THE PHP ENVIRONMENT - …homepage.smc.edu/seno_vicky/cs85/unit1/unit1.pdf ·  · 2015-09-13PHP is embedded code within in XHTML code ... unusual because the user

USING AN EXISTING SERVER

This class has a Web Server running on Amazon Web Services.

The Web Server has Apache Web Server, PHP and MySQL installed.

Each CS 85 student will have their own web space and MySQL databases to

complete required assignment.

Instructions on how to publish your assignments on the CS 85 Web Server will be

on the class website. Username and passwords will be emailed to your SMC

Account.

Page 13: CHAPTER 1 EXPLORING THE PHP ENVIRONMENT - …homepage.smc.edu/seno_vicky/cs85/unit1/unit1.pdf ·  · 2015-09-13PHP is embedded code within in XHTML code ... unusual because the user

ADDING PHP TO YOUR PAGES

PHP allows you to do things not possible with just HTML and CSS

Write a simple program, e.g., hello.php, upload it to the server and see if it works

Check with your Web server administrator to see if PHP is supported

Need three things to run PHP/MySQL Web applications:

Web server (e.g., Apache or MS IIS)

PHP interpreter

MySQL Database Management System (DBMS)

Page 14: CHAPTER 1 EXPLORING THE PHP ENVIRONMENT - …homepage.smc.edu/seno_vicky/cs85/unit1/unit1.pdf ·  · 2015-09-13PHP is embedded code within in XHTML code ... unusual because the user

EXAMINING THE RESULTS

Order of events Between Web Server and End User:

User requests hello.php via browser

The file is run through the PHP interpreter on the web server which executes the

code in the PHP code regions

Each code region is replaced with its output (if any)

The preprocessed page is sent to the browser

Remember, PHP stands for PHP Hypertext Preprocessor

Page 15: CHAPTER 1 EXPLORING THE PHP ENVIRONMENT - …homepage.smc.edu/seno_vicky/cs85/unit1/unit1.pdf ·  · 2015-09-13PHP is embedded code within in XHTML code ... unusual because the user

CREATING THE "TIP OF THE DAY" PROGRAM

tip.php

<body>

<h1>Tip of the day</h1>

<?php

print "<h3>Here's your tip:</h3>";

?>

<div style = "border-color:green; border-style:groove; border-width:2px">

<?php readfile("tips.txt");

?>

</div>

</body>

tip.txt

Stop and smell the roses.

Page 16: CHAPTER 1 EXPLORING THE PHP ENVIRONMENT - …homepage.smc.edu/seno_vicky/cs85/unit1/unit1.pdf ·  · 2015-09-13PHP is embedded code within in XHTML code ... unusual because the user

SUMMARY

hello.php

Print “Hello World!”

phpInfo()

We have covered the hello.php and tip.php examples, Web servers with PHP support, and embedding PHP into

an HTML document

Page 17: CHAPTER 1 EXPLORING THE PHP ENVIRONMENT - …homepage.smc.edu/seno_vicky/cs85/unit1/unit1.pdf ·  · 2015-09-13PHP is embedded code within in XHTML code ... unusual because the user

CODE EXAMPLES FOR THIS CHAPTER

The examples for this chapter are available on the ciswebs Web server (see ciswebs Accounts for more info).

Please read these important notes about running the code examples: Code Examples

You can run the script by clicking the .php link.

Filenames with Src in them display the syntax-highlighted PHP source code. For example, helloSrc.php shows

the source code for hello.php.

ph01WithMods.zip is a ZIP folder of the examples, both original and modified versions.

Chapter 1 examples

Page 18: CHAPTER 1 EXPLORING THE PHP ENVIRONMENT - …homepage.smc.edu/seno_vicky/cs85/unit1/unit1.pdf ·  · 2015-09-13PHP is embedded code within in XHTML code ... unusual because the user

INTRODUCING THE "TIP OF THE DAY" PROGRAM

tip.php

tips.txt

handcode the HTML, at first

example of a content management system

Page 19: CHAPTER 1 EXPLORING THE PHP ENVIRONMENT - …homepage.smc.edu/seno_vicky/cs85/unit1/unit1.pdf ·  · 2015-09-13PHP is embedded code within in XHTML code ... unusual because the user

ACTIVITIES

Read the class syllabus

Review HTML/CSS scripting: http://www.w3.org/Style/Examples/011/firstcss.en.html

Read Chapter 1 of Harris: PHP 6/MySQL Programming for the Absolute Beginner

Read Chapter 1 PowerPoint Notes

Assignment 1: Local Setup and HelloWorld.php :

Setup local Apache/PHP/MySQL servers & Aptana editor web development environment

Create helloworld.php

Page 20: CHAPTER 1 EXPLORING THE PHP ENVIRONMENT - …homepage.smc.edu/seno_vicky/cs85/unit1/unit1.pdf ·  · 2015-09-13PHP is embedded code within in XHTML code ... unusual because the user

DON’T GET DROPPED

CS85: UNIT ASSIGNMENTS - UNIT 1: Assignment

Due: Sunday September 27, 2015 11:59 pm

YOU WILL BE DROPPED IF UNIT 1 ASSIGNMENT (PART 1 & 2) IS COMPLETED BY DUE DATE

Page 21: CHAPTER 1 EXPLORING THE PHP ENVIRONMENT - …homepage.smc.edu/seno_vicky/cs85/unit1/unit1.pdf ·  · 2015-09-13PHP is embedded code within in XHTML code ... unusual because the user

UNIT 1 ASSIGNMENT : DUE: SUNDAY SEPTEMBER 27, 2015 11:59 PM

Unit 1 Assignment Part 1:

Review HTML and CSS: Material Provided

Complete the step by step example provided in the HTML\CSS Review document.

Upload HTML\CSS Webpage to your unit1-1 folder on the class webserver. Instructions to upload the HTML\CSS webpage to web server can be found here

http://homepage.smc.edu/seno_vicky/cs85/upload.html

Required Reading

Read Chapter 1: Exploring the PHP Environment.

Review Chapter 1 Slides

Be sure to run and study the PHP source code for all the examples in the chapter after you setup your PHP/MySQL development environment on your personal

computer.

Unit 1 Assignment Part 2: Set up your home system for PHP/MySQL development

Install XAMPP: Windows Apache MySQL PHP Perl. Instructions provided.

Install and Configure Aptana PHP IDE (Integrated development environment). Instructions provided.

Write a PHP script named hello.php that print (echo) out “Hello World!”.

Test it on your XAMPP/Aptana Install. Instructions provided.

Upload to the class Web Server. Instructions to publish assignments to web server can be found here http://homepage.smc.edu/seno_vicky/cs85/upload.html

Note: You just need to do the tasks listed for the assignment. There is no requirement in this assignment to email me a link. I will be able to run and view your hello.php script

using my browser, SFTP/SCP client, etc.

Page 22: CHAPTER 1 EXPLORING THE PHP ENVIRONMENT - …homepage.smc.edu/seno_vicky/cs85/unit1/unit1.pdf ·  · 2015-09-13PHP is embedded code within in XHTML code ... unusual because the user

RESOURCES

PHP Manual

www.php.net

HTML/CSS Review

http://www.w3.org/Style/Examples/011/firstcss.en.html

Page 23: CHAPTER 1 EXPLORING THE PHP ENVIRONMENT - …homepage.smc.edu/seno_vicky/cs85/unit1/unit1.pdf ·  · 2015-09-13PHP is embedded code within in XHTML code ... unusual because the user

VIDEOS

Install XAMPP

https://www.youtube.com/watch?v=msF-XcJk3Bc&index=6&list=PLlRFEj9H3Oj5F-GFxG-rKzORVAu3jestu

Install Aptana

https://www.youtube.com/watch?v=U5ghHLRv6uc

Hello World!

Link

Filezilla

Upload to Linux Server