© sameer verma 2001 the scripting triangle sameer verma [ san francisco state university all icons...

22
© Sameer Verma 2001 The Scripting Triangle Sameer Verma [http://verma.sfsu.edu/] San Francisco State University All icons or logos are properties of respective

Post on 22-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

© Sameer Verma 2001

The Scripting Triangle

Sameer Verma [http://verma.sfsu.edu/]

San Francisco State University

All icons or logos are properties of respective owners

© Sameer Verma 2001

Script

A text file with commands.A sequence of commands.Not compiled, but interpreted.Purpose: Rapid light-weight development.

© Sameer Verma 2001

The Code Cycle

Edit Link

CompileRun

1 2

34

© Sameer Verma 2001

The Script Cycle

Edit Link

CompileRun

Most user activity takes place here

1 2

22

© Sameer Verma 2001

“Scripting” Engines

Good old PerlActive Server Pages (VB Script)Java Server Pages (Java)PHP:Hypertext Preprocessor (PHP)Others

© Sameer Verma 2001

PHP

A scripting language invented by Rasmus LerdorfPHP is free: freedom to download and use.PHP is cross-platform.PHP is scalable and reliable.Apache is the most widely used web server in the world (66.49%).PHP is the most widely used module for Apache web server (38.59%).

http://www.securityspace.com/s_survey/data/index.html

© Sameer Verma 2001

PHP and platforms

PHP works on Windows 95, Windows 98, Windows NT Workstation, Windows NT Server, Windows 2000 Professional, all Windows 2000 Server lines and Windows XP.PHP works with all Linux distributions, and variants of Unix.MacOS X

© Sameer Verma 2001

PHP and Web servers

PHP works with: Apache Microsoft IIS Netscape Server Microsoft PWS Xitami and a handful others.

© Sameer Verma 2001

PHP and Databases

MySQLmSQLAccessMS-SQLOracleDB2LDAP*Paradox

PostGreSQLVelocisIMAP*SybaseODBCdBasefileProInformix

* LDAP is a Directory Service and IMAP is an email protocol. Both are databases, but used in non-database applications.

© Sameer Verma 2001

PHP + MySQL + Apache

Free + Free + Free = Value AddedPHP works on Windows, Linux, UnixMySQL works on Windows, Linux, UnixApache works on Windows, Linux, UnixFree = Freedom to use and modify.Free does not necessarily mean $0.00Opensource or GPL ≠ Freeware

E-commerce web sites.Database powered portals.Development environment = Production environment.

Develop on a Win 98 Laptop.

Host on a Unix or Linux based ISP.

How: Where:

© Sameer Verma 2001

CGI

Web server(e.g. Apache)

PHP, ASP, etc.

BrowserCGI

x[ ]-?

PHP and Apache run as two programs

Server-Side Client-Side

© Sameer Verma 2001

Module or Server API

Web server(e.g. Apache)

PHP, ASP, etc.

Browser

x[ ]-?

PHP and Apache run as one program

Server-Side Client-Side

© Sameer Verma 2001

PHP files

PHP files work like HTML.Need no special cgi-bin type directories.Can be embedded inside HTML documents.Can also be written as stand-alone “class” type files for data-processing only.

© Sameer Verma 2001

PHP files & more

Adding a database to the mix increases the portability of your application.MySQL works really well with PHP.Store user preferences, login/password, content, inventory, etc. in the database.Use the database as a cache for web data.

© Sameer Verma 2001

Applications

E-commerce sites.Portals.Forums/discussions groups.Web-based E-mail programs.Calendars.Personal Information Managers.WAP, XML

© Sameer Verma 2001

Who uses PHP?

Dialpad.com, Sourceforge.net, Six.deAudi, Subaru, Honda, VolvoApache, Be, Fujitsu, NASA, US Army, W3C, Freshmeat, MP3.com, Lycos, SF Giants, Oakland Raiders, NY Yankees, Ericsson, Sprint Canada, Lufthansa, Swatch.BICS 565, BICS 814, ISYS814

© Sameer Verma 2001

PHP SampleCreate a text file in notepad and type the following green text as-is.Name this file sample.php

<?PHPecho (‘This is a test page. This line is printed via an echo statement.’);echo(‘<BR>’);$remote_ip=getenv('REMOTE_ADDR');print ("You are accessing the page from $remote_ip");?>

Note: If you view this on your off-line computer, make sure you open the file in the browser as http://127.0.0.1/sample.php or http://localhost/sample.php

not

file:///C|/Program Files/Apache Group/Apache/htdocs/sample.php

If you look at the file as a file:/// it will simply show the source of the php file. To make sure php actually gets processed, ensure that the php file is accessed via a web server. A properly written and processed php file will never leave the server. Only the result of your php code will leave the server. See the source of your page in the browser. Do you see any php? Only HTML. Your php file along with its intellectual copyright is quite intact.

© Sameer Verma 2001

PHP SampleYou should see something like this:

Correct

Wrong

If you see the php source code as-is, then either check your web server, or check to see how you are accessing the page. It should NOT be a file:/// type URL in the location bar.

© Sameer Verma 2001

PHP Sample + MoreHere is a modification on the sample.php file with more stuff.

<?PHPecho (‘This is a test page. This line is printed via an echo statement.’);echo(‘<BR>’);$remote_ip=getenv(‘remote_addr’);$remote_host=gethostbyaddr($remote_ip);print ("You are accessing the page from numeric IP address $remote_ip");print ("You are accessing the page from DNS entry $remote_host");?>

Here we simply re-use the $remote_ip variable to generate another variable namely, $remote_host, which is simply the host name assigned to this IP address.

This type of “cascading variable set” is quite common in scripts.

© Sameer Verma 2001

PHP Sample + MoreHere is a script to obtain data from a MySQL database.<?PHP$connection = mysql_connect(“localhost”,“userid”,“password”) or die ("Unable to connect to MySQL server.");$db = mysql_select_db(“database”, $connection) or die ("Unable to select database.");$sql = "SELECT fname, lname, email, nick FROM student WHERE uid = '$uid'";$sql_result = mysql_query($sql,$connection) or die ("Couldn't get student information");echo(‘<html><body><table border=1>’);while ($row = mysql_fetch_row($sql_result)){print ("<tr bgcolor=\"#DDDDDD\"><td colspan=2><H3>First Name: $row[0], Last Name: $row[1] </H3></td></tr><tr bgcolor=\"#D3DCE3\"><td><b>E-mail:</b></td><td> $row[2]</td></tr>");}echo (‘</table></body></html>’);mysql_close($connection);?>•We connect to a MySQL database using login ”userid” and password ”password” and store it in $connection•Then we select the database ”database” and store it in $db•Next we specify an SQL statement and store it in $sql•We run the query ($sql) and store its result in $sql_result•Finally, we use the resulting row(s) to populate a simple HTML table using the values in the query result ($sql_result)•Closing the MySQL connection is not really required, but is a nice practice.

© Sameer Verma 2001

Get Started

This information should get you started.Search for more information on the web. Many PHP projects are available as opensource/free for use.Follow up the links to see more examples of real world applications.

© Sameer Verma 2001

Links

http://www.php.nethttp://www.mysql.comhttp://www.apache.org/http://www.phpbuilder.com/http://www.phpwizard.net/http://www.weberdev.com/http://www.phpclub.net/http://px.sklar.com/http://tecfa.unige.ch/guides/php/http://www.nusphere.com/

http://www.phorum.org/http://www.thickbook.com/http://www.hawhaw.de/http://www.phpnuke.org/http://www.midgard-project.org/http://www.phpworld.com/http://www.zend.com/http://www.gimpster.com/php/http://www.edomex.net/phpopenmonitor/http://www.hotscripts.com/PHP/