november 2003bent thomsen - fit 6-21 it – som værktøj bent thomsen institut for datalogi aalborg...

24
November 2003 Bent Thomsen - FIT 6-2 1 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

Upload: clement-bailey

Post on 16-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: November 2003Bent Thomsen - FIT 6-21 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

November 2003 Bent Thomsen - FIT 6-2 1

IT – som værktøj

Bent Thomsen

Institut for Datalogi

Aalborg Universitet

Page 2: November 2003Bent Thomsen - FIT 6-21 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

November 2003 Bent Thomsen - FIT 6-2 2

Input

• So far we have only talked about output

• PHP can input from:– HTTP request

• Encoded in link• Form elements• Cookies

– Global Variables– Files– Databases

Page 3: November 2003Bent Thomsen - FIT 6-21 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

November 2003 Bent Thomsen - FIT 6-2 3

HTML Forms• HTML forms are just about the only way for PHP to collect

information from users.• Text box example:<Form action=“that.php” method=“Post”><input type=“text” name=”variable1”><input type=“text” name=”variable2”><input type=“submit” value=“click here”></Form>

• The above creates 2 text boxes and anything that is type into the 1st text box will be assigned to variable1; anything that is enter into the 2nd textbox is assigned to variable2. These variables are then sent to the PHP script “that.php” for processing.

Page 4: November 2003Bent Thomsen - FIT 6-21 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

November 2003 Bent Thomsen - FIT 6-2 4

In “that.php”

<?PHPPrint(“$variable1, $variable2 <br>”);

?>

• “that.php” will output the information that is associated with the 2 text boxes.

Page 5: November 2003Bent Thomsen - FIT 6-21 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

November 2003 Bent Thomsen - FIT 6-2 5

Other Form Elements• Radio Buttons

What is your favourite pet?<br><input type=“radio” name=“favourite_pet” value=“termite”>Termite<br>

<input type=“radio” name=“favourite_pet” value=“cockroach”>Cockroach

• CheckboxesWhat magazine are you currently subscribed to?<br>

<input type=“checkbox” name=“t_w” value=“true”>Termite World<br>

<input type=“checkbox” name=“t_a” value=“true”>Cockroach’s Day<br>

• What is/are the variable(s) and the assigned value(s)?

Page 6: November 2003Bent Thomsen - FIT 6-21 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

November 2003 Bent Thomsen - FIT 6-2 6

Continue …• List box example:

<select name="hob[]" size="2" multiple><option>left</option><option>right</option><option>top</option><option>bottom</option></Select>

• Note the variable hob[]. In this case, an array will be sent to a PHP script. Values can be accessed through array indexing.

Page 7: November 2003Bent Thomsen - FIT 6-21 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

November 2003 Bent Thomsen - FIT 6-2 7

Cookies

<?php

if (!$myname) {     print "What is your name? ";     print "<FORM ACTION=\"$PHP_SELF\" METHOD=\"GET\">\n";     print "<INPUT NAME=\"myname\" SIZE=20>\n";     print "</FORM>";     exit; }

setcookie("myname", $myname);

?>

Page 8: November 2003Bent Thomsen - FIT 6-21 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

November 2003 Bent Thomsen - FIT 6-2 8

Form Handling again

• A simple form:

<form action="simple_form.php" method="POST">

Your name: <input type=“text” name=“name”><br/>

You age: <input type=“text” name=“age”><br/>

<input type=“submit”/>

</form>

• The form handling code:

Hi <?php echo $name; ?>.

You are <?php echo $age; ?> years old.

Page 9: November 2003Bent Thomsen - FIT 6-21 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

November 2003 Bent Thomsen - FIT 6-2 9

Form Handling

• Global Variables, $HTTP_GET_VARS, $_GET,

register_globals configuration

Hi <?php echo $HTTP_GET_VARS[‘name’]; ?>.

You are <?php echo $_GET[‘age’]; ?> years old.

Page 10: November 2003Bent Thomsen - FIT 6-21 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

November 2003 Bent Thomsen - FIT 6-2 10

More Global VariablesVariable Name Description

$DOCUMENT_ROOT Your Web server's base directory with user-visible files.

$REQUEST_METHOD The HTTP method used to access this page, for example GET or POST.

$REQUEST_URI Full local part of the request URL, including parameters.

$HTTP_GET_VARS An associative array with the GET parameters passed to PHP, if any.

$HTTP_POST_VARS An associative array with the POST parameters passed to PHP, if any.

$HTTP_COOKIE _VARS An associative array with the cookies passed by the browser, if any.

$SCRIPT_FILENAME File name of the top-level page being executed.

$SCRIPT_NAME Local URI part of the page being executed.

$SERVER_ADMIN Server administrator's email address.

$SERVER_NAME Domain name for the server.

$SERVER_PORT TCP port number the server runs on.

$SERVER_PROTOCOL Protocol used to access the page, for example "HTTP/1.1".

Page 11: November 2003Bent Thomsen - FIT 6-21 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

November 2003 Bent Thomsen - FIT 6-2 11

File Access

• Local File Access

– fopen, fread, fwrite, fclose, fputs, freads, feof, much more…

• Remote File Access

– Uses the same functions as local file access

– Uses URL’s to retrieve files, FTP and HTTP supported.

<?php readfile(‘http://www.ActiveState.com/’); ?>

– Can write files to FTP is username and password is sent

• ftp://username:[email protected]/path/filename

Page 12: November 2003Bent Thomsen - FIT 6-21 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

November 2003 Bent Thomsen - FIT 6-2 12

Example<?php $visitors = 0; // Initialize the visitors to zero $fr = fopen('counter.txt', 'r');if(!$fr){ $visitors = 1; // Our first visitor $fr = fopen('counter.txt','w'); if(!$fr) {echo "Could not create the counter file!"; exit;} fputs($fr, $visitors); fclose($fr);} else { $visitors = fgets($fr,4096); $visitors++; echo "You are visitor number: $visitors"; fclose($fr); $fr = fopen('counter.txt','w'); if(!$fr) {echo "Could not re-create the counter file!";exit;} fputs($fr, $visitors); fclose($fr); }?>

Page 13: November 2003Bent Thomsen - FIT 6-21 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

November 2003 Bent Thomsen - FIT 6-2 13

Authentication

<? function authenticate() { global $PHP_AUTH_USER; global $PHP_AUTH_PW; if(!($PHP_AUTH_USER == “user" && $PHP_AUTH_PW == “password“)) { Header(‘WWW-Authenticate: basic realm=“My Website“’); Header(‘HTTP/1.0 401 Unauthorized’); echo(‘Please enter a username and password to proceed.’); return false; } return true;} if (!authenticate()) exit;echo “You have authenticated properly!”;?>

Page 14: November 2003Bent Thomsen - FIT 6-21 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

November 2003 Bent Thomsen - FIT 6-2 14

PHP and SQL Databases

• Wide range of SQL database supported

– MySQL, PostgreSQL, MS-SQL, Oracle, Sybase, ODBC,

DBM, Informix…

– Native interfaces (MySQL, etc), and abstracted interfaces

(ODBC, dba, PEAR)

– Persistent connections supported

Page 15: November 2003Bent Thomsen - FIT 6-21 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

November 2003 Bent Thomsen - FIT 6-2 15

MySQL

<?php$conn = mysql_pconnect(“localhost”, “username”, “password);mysql_select_db(“mydatabase”, $conn);$res = mysql_query($conn, “SELECT * FROM resources”);while (($rs = mysql_fetch_array($res))) {

echo(“column1: “.$rs[0].” column2: “.$rs[1].” …<br>\n”);}mysql_close();?>

Page 16: November 2003Bent Thomsen - FIT 6-21 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

November 2003 Bent Thomsen - FIT 6-2 16

PostgreSQL<?// database access parameters -- alter this as per your configuration$host = "localhost"; $user = "postgres"; $pass = "postgres"; $db = "test";

// open a connection to the database server$connection = pg_connect ("host=$host dbname=$db user=$user password=$pass");

if (!$connection){

die("Could not open connection to database server");}

// generate and execute a query$query = "SELECT name, address FROM addressbook ORDER BY name"; $result = pg_query($connection, $query) or die("Error in query: $query. " .pg_last_error($connection));

// get the number of rows in the resultset// this is PG-specific$rows = pg_num_rows($result);

Page 17: November 2003Bent Thomsen - FIT 6-21 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

November 2003 Bent Thomsen - FIT 6-2 17

// if records presentif ($rows > 0){

// iterate through resultsetfor ($i=0; $i<$rows; $i++) {

$row = pg_fetch_row($result, $i);?>

<li><font size="-1"><b><? echo $row[0]; ?></b></font><br><font size="-1"><? echo $row[1]; ?></font><p>

<?}

}// if no records present display messageelse{?>

<font size="-1">No data available.</font><?}// close database connectionpg_close($connection);?>

Page 18: November 2003Bent Thomsen - FIT 6-21 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

November 2003 Bent Thomsen - FIT 6-2 18

ODBC<? // connect to a DSN "mydb" with a user and password "marin" $connect = odbc_connect("mydb", "marin", "marin");

// query the users table for name and surname$query = "SELECT name, surname FROM users";// perform the query$result = odbc_exec($connect, $query);

// fetch the data from the databasewhile(odbc_fetch_row($result)){ $name = odbc_result($result, 1); $surname = odbc_result($result, 2); print("$name $surname\n");}

// close the connectionodbc_close($connect);?>

Page 19: November 2003Bent Thomsen - FIT 6-21 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

November 2003 Bent Thomsen - FIT 6-2 19

Putting it all togetherWeb-Client

Web-Server

DBMS

DatabaseOutput

SQL commands

PHPScript

HTML-Form (+JavaScript)

Reply

WWW

SubmitData

Call PHPinterpreter

Response Response

LAN

Web-Browser

DatabaseServer

Page 20: November 2003Bent Thomsen - FIT 6-21 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

November 2003 Bent Thomsen - FIT 6-2 20

Going Mobile with WAP

• WAP: Wireless Application Protocol– Facilitates communication between a wireless

device and a gateway, which in turn allows communication with Internet- or intranet-based resources

• WML: Wireless Markup Language– Derivative of XML used to create pages for

wireless devices

• WAP application can be built using PHP

Page 21: November 2003Bent Thomsen - FIT 6-21 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

November 2003 Bent Thomsen - FIT 6-2 21

Compelling WAP applications

• Brief data that users want available while mobile– Flight, directions, and traffic information– Movie listings– News– Weather– Reading email– Controlling “things” – house, industrial plants, …

• Key today: application must provide high value with a minimum of typing

• Eventually: location-based services

Page 22: November 2003Bent Thomsen - FIT 6-21 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

November 2003 Bent Thomsen - FIT 6-2 22

A Basic Card<?xml version=“1.0”?><!DOCTYPE wml PUBLIC “-//WAPFORUM//DTD WML 1.1//EN”

“http://www.wapforum.org/DTD/wml_1.1.xml”>

<wml><card id=“main” title=“An Example”>

<p>Hello World!

</p></card>

</wml>

Page 23: November 2003Bent Thomsen - FIT 6-21 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

November 2003 Bent Thomsen - FIT 6-2 23

WML output from PHP<?php // send wml headers header("Content-type: text/vnd.wap.wml"); echo "<?xml version=\"1.0\"?>"; echo "<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\"" . " \"http://www.wapforum.org/DTD/wml_1.1.xml\">"; ?>

<wml> <card id="card1" title="Example 1"> <p> <?php // format and output date $the_date = date("M d Y"); print $the_date; print "<br/>Welcome to a PHP-enabled site!"; ?> </p> </card> </wml>

Page 24: November 2003Bent Thomsen - FIT 6-21 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet

November 2003 Bent Thomsen - FIT 6-2 24

PHP and Mobile Applications