240 likes | 317 Views
IT – som værktøj. Bent Thomsen Institut for Datalogi Aalborg Universitet. 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. HTML Forms.
E N D
IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet Bent Thomsen - FIT 6-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 Bent Thomsen - FIT 6-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=”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. Bent Thomsen - FIT 6-2
In “that.php” <?PHP Print(“$variable1, $variable2 <br>”); ?> • “that.php” will output the information that is associated with the 2 text boxes. Bent Thomsen - FIT 6-2
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 • Checkboxes What 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)? Bent Thomsen - FIT 6-2
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. Bent Thomsen - FIT 6-2
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); ?> Bent Thomsen - FIT 6-2
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. Bent Thomsen - FIT 6-2
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. Bent Thomsen - FIT 6-2
More Global Variables Bent Thomsen - FIT 6-2
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:password@host.com/path/filename Bent Thomsen - FIT 6-2
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); } ?> Bent Thomsen - FIT 6-2
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!”; ?> Bent Thomsen - FIT 6-2
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 Bent Thomsen - FIT 6-2
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(); • ?> Bent Thomsen - FIT 6-2
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); Bent Thomsen - FIT 6-2
// 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> <br> <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); ?> Bent Thomsen - FIT 6-2
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 database while(odbc_fetch_row($result)){ $name = odbc_result($result, 1); $surname = odbc_result($result, 2); print("$name $surname\n"); } // close the connection odbc_close($connect); ?> Bent Thomsen - FIT 6-2
Putting it all together Web-Client Database Server Web-Server HTML-Form (+JavaScript) Call PHP interpreter WWW DBMS Submit Data LAN Web-Browser PHP Script SQL commands Response Response Database Output Reply Bent Thomsen - FIT 6-2
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 Bent Thomsen - FIT 6-2
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 Bent Thomsen - FIT 6-2
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> Bent Thomsen - FIT 6-2
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> Bent Thomsen - FIT 6-2
PHP and Mobile Applications Bent Thomsen - FIT 6-2