PHP - Part 2. More operators... Arithmetic and Assignment Operators e.g., using + and =  $IntA=5; $intB=8; $intC=$intA+$intB; //intC is 13  // Same.

Slides:



Advertisements
Similar presentations
PHP Form and File Handling
Advertisements

PHP I.
UFCE8V-20-3 Information Systems Development 3 (SHAPE HK) Lecture 3 PHP (2) : Functions, User Defined Functions & Environment Variables.
Cookies, Sessions. Server Side Includes You can insert the content of one file into another file before the server executes it, with the require() function.
Introducing JavaScript
Lecture 6/2/12. Forms and PHP The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input When dealing with HTML forms.
HTML Form Processing Learning Web Design – Chapter 9, pp Squirrel Book – Chapter 11, pp
PHP Server-side Programming. PHP  PHP stands for PHP: Hypertext Preprocessor  PHP is interpreted  PHP code is embedded into HTML code  interpreter.
FILE UPLOADS CHAPTER 11. THE BASIC PROCESS 1.The HTML form displays the control to locate and upload a file 2.Upon form submission, the server first stores.
PHP Overview CS PHP PHP = PHP: Hypertext Preprocessor Server-side scripting language that may be embedded into HTML One goal is to get PHP files.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
PHP Security.
Advance Database Management Systems Lab no. 5 PHP Web Pages.
Introduction to PHP and Server Side Technology. Slide 2 PHP History Created in 1995 PHP 5.0 is the current version It’s been around since 2004.
ITM 352 © Port,KazmanFile I/O - 1 File I/O in PHP Persistent Data.
INTERNET APPLICATION DEVELOPMENT For More visit:
Chapter 4 – The Building Blocks Data Types Literals Variables Constants.
Lecture 7 – Form processing (Part 2) SFDV3011 – Advanced Web Development 1.
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap.
CHAPTER 12 COOKIES AND SESSIONS. INTRO HTTP is a stateless technology Each page rendered by a browser is unrelated to other pages – even if they are from.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
School of Computing and Information Systems CS 371 Web Application Programming PHP - Basics Serving up web pages.
Nael Alian Introduction to PHP
Variables, Operators and Data Types. By Shyam Gurram.
Week seven CIT 354 Internet II. 2 Objectives Database_Driven User Authentication Using Cookies Session Basics Summary Homework and Project 2.
PHP1-1 PHP Lecture 2 Xingquan (Hill) Zhu
NMD202 Web Scripting Week3. What we will cover today Includes Exercises PHP Forms Exercises Server side validation Exercises.
Introduction to PHP A user navigates in her browser to a page that ends with a.php extension The request is sent to a web server, which directs the request.
1 Chapter 9 – Cookies, Sessions, FTP, and More spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science.
ITM © Port, Kazman1 ITM 352 More on Forms Processing.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
Intro to PHP IST2101. Review: HTML & Tags 2IST210.
What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved.
PHP Open source language for server-side scripting Works well with many databases (e.g., MySQL) Files end in.php,.php3 or.phtml Runs on all major platforms.
Storing and Retrieving Data
Global Variables - Superglobals Several predefined variables in PHP are "superglobals", which means that they are always accessible, regardless of scope.
Server-Side Scripting with PHP ISYS 475. PHP Manual Website
ITM © Port, Kazman1 ITM 352 More on Forms Processing.
הרצאה 4. עיבוד של דף אינטרנט דינמי מתוך Murach’s PHP and MySQL by Joel Murach and Ray Harris.  דף אינטרנט דינמי משתנה עפ " י הרצת קוד על השרת, יכול להשתנות.
Creating Databases for Web applications Server side vs client side PHP basics Homework: Get your own versions of sending working: both html and Flash!
 Previous lessons have focused on client-side scripts  Programs embedded in the page’s HTML code  Can also execute scripts on the server  Server-side.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
Since you’ll need a place for the user to enter a search query. Every form must have these basic components: – The submission type defined with the method.
 A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Basics.
1 PHP HTTP After this lecture, you should be able to know: How to create and process web forms with HTML and PHP. How to create and process web forms with.
PHP Syntax You cannot view the PHP source code by selecting "View source" in the browser - you will only see the output from the PHP file, which is plain.
PHP Form Processing * referenced from
NMD202 Web Scripting Week2. Web site
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
CGS 3066: Web Programming and Design Spring 2016 PHP.
IST 210: PHP Basics IST 210: Organization of Data IST2101.
Radoslav Georgiev Telerik Corporation
Web Programming with PHP (3) Superglobals, Form & File processing.
Simple PHP Web Applications Server Environment
A pache M ySQL P hp Robert Mudge Reference:
PHP using MySQL Database for Web Development (part II)
PHP (Session 1) INFO 257 Supplement.
Receiving form Variables
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
DBW - PHP DBW2017.
PHP Introduction.
WEB PROGRAMMING JavaScript.
PHP.
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
Web Programming Language
PHP an introduction.
Presentation transcript:

PHP - Part 2

More operators...

Arithmetic and Assignment Operators e.g., using + and =  $IntA=5; $intB=8; $intC=$intA+$intB; //intC is 13  // Same +, -, *, / and % as C  $intA + = $intB; //as in C Bitwise:  &, |, ^, ~, >  e.g., $intA=7; $intB=9;  $intC = $intA & $intB;

Comparison Operators == //true if equal === //true if identical (i.e., same type and value) !=, <> //true if not equal !==//true if not identical >, = Ternary operators:  (expre1) ? (expre2) : (expre3); //expre2 if expre1 true  $strA = ($intValue >0) ? “positive” : “zero or negative”;

String Operators Concatenate with.  $strResult = $strOne. $strTwo; Convert other types to string: $intNumber = 45; $strAgeis = “My age is”; $strResult = $strAgeis. “ “. $intNumber; echo $strResult;

String Processing Strings specified using single or double quotes $str=“hello”; $str=‘hello’; Single quotes are literal $myStr=‘$str one’; Double quotes substitute the content of variables $myStr=“$str world”; Use curly braces if variable name is joined to more text $myStr=“${str}world”;

Substrings $subStr=substr($str, int $start [, int $length ]);  Extracts portion of $str $count=substr_count($str, ‘text’) ;  Counts the number of occurrences of text in the string $restStr=strstr($str, ‘text’) ;  Extract substring of $str from first occurrence of ‘text’ strlen($str)  Length of a string $str{0}  Access individual characters in a string $newStr=$Str.‘more text’  Concatenate strings using the dot ‘.’ operator

Logical Operators And Or Xor ! && ||

Error Control Error Control Operator Example: $intA = 58; $int B = = $intA / $intB; //no error message... print " Is it possible that ". "$intA/$intB". "=". "$intC". "? ";

PHP Built in variables $GLOBALS $_SERVER $_GET $_POST $_COOKIE $_FILES $_ENV $_REQUEST $_SESSION But be careful: many are server-dependent Try using print_r() on these.

print " My host name is ". $_SERVER['HTTP_HOST']. " \n"; print " I'm viewing this page from ". $_SERVER['HTTP_USER_AGENT']. " "; print "We can split the Browser string into a new array using split() "; $userBits = split(" ", $_SERVER['HTTP_USER_AGENT']); print "The browser is identified as ". $userBits[0]. " "; print "or you can split this up too! "; $theBrowserID = split("/", $userBits[0]); print "The browser is advertising itself as ". $theBrowserID[0]. " "; print $theBrowserID[1]. " \n"; print "Of course the real browser is "; $lastIndex = count($userBits) - 1; $realBrowser = split("/", $userBits[$lastIndex]); print $realBrowser[0]. " version ". $realBrowser[1]. " \n"; print "My browser can accept ". $_SERVER['HTTP_ACCEPT']. " \n"; print "My proxy server (if I have one) is ". $_SERVER['HTTP_VIA']. " \n"; print "Document root is ". $_SERVER['DOCUMENT_ROOT']. " \n"; print "This page is called ". $_SERVER['PHP_SELF']. " \n"; PHP Built in Variables.

Sample Output My host name is localhost:8080 webpages/phptest/php-built-in-variables.php I'm viewing this page from Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/ Safari/533.4 We can split the Browser string into a new array using split() The browser is identified as Mozilla/5.0 or you can split this up too! The browser is advertising itself as Mozilla 5.0 Of course the real browser is Safari version My browser can accept application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/* ;q=0.5 My proxy server (if I have one) is Document root is This page is called webpages/phptest/php-built-in-variables.php

PHP Processing Form Variables Recall how CGI used POST and GET methods In PHP: Extract submitted form variables from: $_GET $_POST $_REQUEST (also contains variables but may violate security by using the wrong method compared to the application design) Submitted files can be extracted from: $_FILES $_FILES (...more details later)

Example using POST - HTML Number 1: Number 2:

Action using POST - PHP Multiply Using PHP with POST <?php print "Apache receives the following array: "; print_r($_POST) $intResult = $_POST['m'] * $_POST['n']; print "The result of ". (int)$_POST['m']. "*". $_POST['n']. "=". $intResult; ?>

Exercise6 Copy the previous code and change the method to GET and REQUEST. Try to combine the array examples with forms.

Combining PHP with forms Recall the code for a self-generating CGI script Combining HTML with PHP HTML / PHP User request Data processing

HTML/PHP ' method="post"> Number 1: Number 2: Self generating Multiply Using single PHP file with POST <?php if (isset($_POST['submit'])) { $intResult = $_POST['m'] * $_POST['n']; print "The result of ". (int)$_POST['m']. " * ". $_POST['n']. " = ". $intResult; } else { echo "This is the first time the page is loaded ";} ?>

File Processing The normal technique for storing permanent information on the server side is using a database Sometimes storage in flat files is useful  When database storage is overkill  During development and testing of code  Rapid prototyping  When saving specific formats

Basic File Processing Open a file for writing Write to the file Close the file Open a file for reading Read from the file Close the file

Opening Files $fp = fopen("file.txt", "r");  Open a file for reading $fp = fopen("file.txt", "w");  Open a file for writing  Note depending on operating system (i.e., Windows) file paths might need to be escaped  "\\pathtofile\\filename.txt"

Reading a File $contents = fread($fp, filesize($filename));  Reads whole of file into one string  Poor performance for large files $contents = fgets($fp, 4096);  Reads one line or the number of bytes specfied Whichever is less $contents =file_get_contents($filename)  Efficient way to read whole file into string

Writing to a File fwrite($fp, $outputstring);  Write string out to given file pointer fwrite($fp, $outputstring, 80);  Write first 80 characters to output string

Closing Files fclose($fp);  Close given file pointer  Normally won’t be an error.

Superglobals From version onward, PHP provides an additional set of predefined arrays containing variables from the web server (if applicable), the environment, and user input. automatically global--i.e., automatically available in every scope. For this reason, they are often known as "superglobals". There is no mechanism in PHP for user-defined superglobals. ($HTTP_*_VARSYou'll notice how the older predefined variables ($HTTP_*_VARS) still exist. As of PHP 5.0.0, the long PHP predefined variable arrays may be disabled with the register_long_arrays directive.

Demo welcome_html.htm, welcome.php php_superglobals.php part2_example1.php part2_example2.php part2_example3.php Inspect using web browser, try modifying the URL’ to indicate new parameters Look for QUERY_STRING, $_GET

EasyPHP Apache: (httpd.conf) cgi.force_redirect = 0 Listen :5080 PHP.ini variables_order = "EGPCS" request_order = "" register_long_arrays = Off register_globals = Off