
98e6e22b4896ceb9b5a29093b4c0c9a9.ppt
- Количество слайдов: 24
IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet November 2003 Bent Thomsen - FIT 6 -2 1
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 November 2003 Bent Thomsen - FIT 6 -2 2
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=”variable 1”> <input type=“text” name=”variable 2”> <input type=“submit” value=“click here”> </Form> • The above creates 2 text boxes and anything that is type into the 1 st text box will be assigned to variable 1; anything that is enter into the 2 nd textbox is assigned to variable 2. These variables are then sent to the PHP script “that. php” for processing. November 2003 Bent Thomsen - FIT 6 -2 3
In “that. php” <? PHP Print(“$variable 1, $variable 2 ”); ? > • “that. php” will output the information that is associated with the 2 text boxes. November 2003 Bent Thomsen - FIT 6 -2 4
Other Form Elements • Radio Buttons What is your favourite pet? <input type=“radio” name=“favourite_pet” value=“termite”>Termite <input type=“radio” name=“favourite_pet” value=“cockroach”>Cockroach • Checkboxes What magazine are you currently subscribed to? <input type=“checkbox” name=“t_w” value=“true”>Termite World <input type=“checkbox” name=“t_a” value=“true”>Cockroach’s Day • What is/are the variable(s) and the assigned value(s)? November 2003 Bent Thomsen - FIT 6 -2 5
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. November 2003 Bent Thomsen - FIT 6 -2 6
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); ? > November 2003 Bent Thomsen - FIT 6 -2 7
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. November 2003 Bent Thomsen - FIT 6 -2 8
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. November 2003 Bent Thomsen - FIT 6 -2 9
More Global Variables Variable 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". November 2003 Bent Thomsen - FIT 6 -2 10
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. Active. State. com/’); ? > – Can write files to FTP is username and password is sent • ftp: //username: password@host. com/path/filename November 2003 Bent Thomsen - FIT 6 -2 11
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); } ? > November 2003 Bent Thomsen - FIT 6 -2 12
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!”; ? > November 2003 Bent Thomsen - FIT 6 -2 13
PHP and SQL Databases • Wide range of SQL database supported – My. SQL, Postgre. SQL, MS-SQL, Oracle, Sybase, ODBC, DBM, Informix… – Native interfaces (My. SQL, etc), and abstracted interfaces (ODBC, dba, PEAR) – Persistent connections supported November 2003 Bent Thomsen - FIT 6 -2 14
My. SQL <? 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(“column 1: “. $rs[0]. ” column 2: “. $rs[1]. ” … n”); } mysql_close(); ? > November 2003 Bent Thomsen - FIT 6 -2 15
Postgre. SQL <? // 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); November 2003 Bent Thomsen - FIT 6 -2 16
// if records present if ($rows > 0) { // iterate through resultset for ($i=0; $i<$rows; $i++) { $row = pg_fetch_row($result, $i); ? > <li><font size="-1"><b><? echo $row[0]; ? ></b></font> <font size="-1"><? echo $row[1]; ? ></font> <p> <? } } // if no records present display message else { ? > <font size="-1">No data available. </font> <? } // close database connection pg_close($connection); ? > November 2003 Bent Thomsen - FIT 6 -2 17
ODBC <? // connect to a DSN "mydb" with a user and password "marin" $connect = odbc_connect("mydb", "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 database while(odbc_fetch_row($result)){ $name = odbc_result($result, 1); $surname = odbc_result($result, 2); print("$name $surnamen"); } // close the connection odbc_close($connect); ? > November 2003 Bent Thomsen - FIT 6 -2 18
Putting it all together Web-Client HTML-Form (+Java. Script) Reply November 2003 Web-Server Call PHP interpreter Submit Data Web-Browser PHP Script WWW Response Database Server Response Bent Thomsen - FIT 6 -2 LAN DBMS SQL commands Database Output 19
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 November 2003 Bent Thomsen - FIT 6 -2 20
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 November 2003 Bent Thomsen - FIT 6 -2 21
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> November 2003 Bent Thomsen - FIT 6 -2 22
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="card 1" 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> November 2003 Bent Thomsen - FIT 6 -2 23
PHP and Mobile Applications November 2003 Bent Thomsen - FIT 6 -2 24
98e6e22b4896ceb9b5a29093b4c0c9a9.ppt