Professional Documents
Culture Documents
Beginning Php4
Beginning Php4
Beginning Php4
When a browser calls an ASP document, the ASP Server reads the .asp document and
1. 2. 3.
Substitutes appropriate files for the (server-side) include statements Runs the ASP code (Visual Basic Script see the Tutorial and Language Reference, ) Returns the resulting HTML code to the browser
ASP code enclosed in: <% VBScript code %> Everything outside is HTML The result of the combined HTML and ASP code must be a standard HTML document, e.g.:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Final//EN"> <html> <head> <title>Miracle Drug Study</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <meta name="Description" content=""><meta name="Keywords" content=""> <link rel=STYLESHEET type="text/css" href=""> </head> <body> </body> </html>
Open connection:
SQL = "SELECT FirstName, LastName, DOB, Gender FROM Patients WHERE Gender = '" & Gender & "' ORDER BY FirstName DESC"
do while NOT Patients.eof Name = Patients(0) & " " & Patients(1) Patients.MoveNext loop
Add
to or edit table:
set RS = Server.CreateObject("ADODB.Recordset") RS.Open table name", conn, , adLockOptimistic, adCmdTable (where adLockOptimistic = 3, adCmdTable = 2)
Add to or edit table (continued): Create new record, Edit, & Update:
RS.AddNew RS(Dosage) = 200 RS.Update do while NOT RS.eof if RS(ID) = 7 then RS(Dosage) = 200 RS.Update else RS.MoveNext end if loop
Queries:
Patients.Close
Record Sets:
RS.Close
set RS = nothing
The Connection:
conn.close
ASP Security
Apart
from various Internet Information Services (IIS Windows Web service) security holes (for viruses and worms), security is quite good. Use https:// if you want to protect content over the internet provides Secure Socket Layer (SSL) security
ASP Resources
ASP Introduction
Microsofts
ColdFusion
Easy-to-learn Server-Side Scripting Language: CFML, or Cold Fusion Markup Language, is embedded in HTML code CF code is enclosed in or by CF tags:
Documents must end in .cfm ColdFusion is Case Insensitive Example (code, copy of database)
All
#variables# are enclosed in # signs HTML output which includes of CF variables must be surrounded by CF output tags; e.g.:
<CFQUERY Name="Patients" dbtype="dynamic" connectstring="#DBdriver# #DBfile#"> SELECT ID, FirstName, LastName FROM Patients ORDER BY FirstName </CFQUERY>
<CFset Dbdriver = "Driver={MICROSOFT ACCESS DRIVER (*.mdb)}; UID=admin; PWD=; dbq="> <CFset Dbfile = "f:\web\database\rescomp\study.mdb">
<SELECT name="PatientID">
All variables in the form object (e.g. Form.var1) that match attributes in the table are inserted into the table automatically
<CFquery name="Treatment" dbtype="dynamic" connectstring="#DBdriver# #DBfile#"> INSERT into Treatment VALUES (#PatientID#, #EventID#, Now(), #Dosage(mg)#, #Severity#, #Time#) </CFquery>
Other
Allaire/Macromedias
WebMonkeys
Originally designed to be a better general purpose tool than a Unix shell, it has grown and spread to be supported from Windows to Macintosh to VMS.
Powerful
file itself must end in .cgi or .pl First line must specify the location of the Perl engine (The DBI module will not work for #!/usr/local/bin /perl[5] see below):
#!/uva/bin/perl -w
First
printed line must be the following if you want its response to go to a browser:
print "Content-type: text/html\n\n";
my $hostname = lebec.tc.edu"; my $username = gordie"; # "my" defines a local variable my $password = mine122"; my $database = $username . "_study"; # = dld5s_study my $data_source = "DBI:mysql:$database:$hostname";
Connect
to the database:
my $SQL = "SELECT FirstName, LastName, DOB, Gender FROM Patients WHERE Gender = '$Gender ORDER BY FirstName DESC"; my $sth = $dbh->prepare($SQL) or die "Unable to prepare $SQL: dbh->errstr\n"; $sth->execute or die "Unable to execute query: $dbh->errstr\n";
Clean
up
$sth->finish; $dbh->disconnect;
Perl Resources
Perl Programming Introduction Perl Programming for the Web Overview, Built-in functions, Data types, Regular expressions, Modules: DBI(1), DBI(2), CGI
Perl Documentation
WebMonkeys Tutorial, etc. MySQL and PERL for the Web by DuBois (New Riders) Learning Perl by Schwartz & Christiansen (OReilly) Programming Perl by Wall, Orwant, & Christiansen (OReilly) Programming the Perl DBI: Database Programming with Perl by Descartes, Bunce, & Mui (Editor) (OReilly)
PHP
Example Add
embedding scripting language (see the PHP online manual When a browser calls a PHP document, the Server reads the PHP document and
Runs the PHP code Returns the resulting HTML code to the browser
Example
(code)
must end in .php or .phtml PHP code enclosed in <?php PHP code ?> or <? PHP code ?> Everything outside is HTML Output is (generally) to a browser requiring standard HTML
mysql_connect($hostName, $userName, $password) or die("Unable to connect to host $hostName"); mysql_select_db($dbName) or die("Unable to select database $dbName");
$SQL = "SELECT FirstName, LastName, DOB, Gender FROM Patients WHERE Gender = '$Gender ORDER BY FirstName DESC"; $Patients = mysql_query($SQL); $SQL = "INSERT INTO Patients (FirstName, LastName) VALUES('$firstName', '$lastName')"; $Patients = mysql_query($SQL);
mysql_close();
PHP/MySQL Security
The
same problems as PHP occur with Perl if you run it as a Perl or CGI script.
See the passwords link
PHP Resources
PHP
Documentation PHPs Tutorial WebMonkeys Tutorial PHP and MySQL Web Development by Welling & Thomson (SAMS) Beginning PHP4 by Blan, Choi, et. al (Wrox)
(Other) Books
MySQL by DuBois (New Riders) MySQL and PERL for the Web by DuBois (New Riders) MySQL & mSQL byYarger, Reese, & King PHP and MySQL Web Development by Welling & Thomson (SAMS) Beginning PHP4 by Blan, Choi, et. al (Wrox) Learning Perl by Schwartz & Christiansen (OReilly) Programming Perl by Wall, Orwant, & Christiansen (OReilly) Programming the Perl DBI: Database Programming with Perl by Descartes, Bunce, & Mui (Editor) (OReilly) SQL-99 Complete, Really by Gulutzan & Pelzer (R&D Books)
PHP
PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. PHP is a project of the Apache Software Foundation. PHP stands for PHP: Hypertext Preprocessor.
#!/usr/local/bin/php <html> <body> <?php echo Hello I am a PHP script; ?> </body> </html>
First file
Save
the file with a PHP extension in your public_html folder. Give the webserver execute permission (chmod 755 hello.php is the easiest way to do this) Point a webserver at the file Lets look at the first line of the file #!/usr/local/bin/php
PHP Forms
Forms are a useful part of any internet site, and Im sure youve used them in the past. Forms are part of HTML, however you can pass their values to a PHP script. Here is a simple HTML form which you can put in any HTML file:
<form action="action.php" method="POST"> Your name: <input type="text" name="name"> Your age: <input type="text" name="age"> <input type="submit"> </form>
This isnt specific to PHP, the only thing to notice is how we pass the values to action.php
Data Types
There are eight data types in PHP (which isnt a lot), Im going to cover three, the others are not used a lot and would be considered advanced in the contexts of PHP anyways. Data types in PHP, unlike, are flexible or not strict, and are refrenced the same way, so inter-chaging is simple.
Data Types
$bool=TRUE;//aboolean $str="foo";//astring $int=12;//aninteger echogettype($bool);//printsout"boolean" echogettype($str);//printsout"string" //Ifthisisaninteger,incrementitbyfour if(is_int($int)){$int+=4;} /*If$boolisastring,printitout(doesnot printoutanything)*/ if(is_string($bool)){ echo"String:$bool"; }
Manulipating Data Types These types can be added, subtracted, concatinated like they can be in C or Java
$a = 5; $b = 4; Echo ($a + $b); // prints 9! $string = touch; $end = down; Echo $string.$end; //prints touchdown!
Control Structures
If:
$hey = TRUE; If($hey) { printf(Hello\n); } else { printf(Never gona see this!\n); }
While
$a = 0; While($a < 100) { printf(hello $a\n); a++; } Prints all the numbers from 0 to 99.
Global Variables
Weve already seen the use of some global or Super variables in $_POST and $_SERVER. Theres a good few of them which can be useful to make your site more personal. To get a list of them, create this PHP file.
#!/usr/local/bin/php <?php phpinfo(); ?>
Resources This helpdesk tutorial like the others is just geared to get you started at PHP coding. PHPs website is the best resource for consultation, www.php.net it has a simple to follow tutorial (you might find striking resemblances with tonites!), as well as a manual page for every function in PHP. I recommend you go to the site for more detailed descriptions of what we did tonite.