1 CHAPTER 10 ADVANCED PHP.

Slides:



Advertisements
Similar presentations
Chapter 6 Server-side Programming: Java Servlets
Advertisements

PHP Form and File Handling
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.
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.
Chapter 12 Introduction to PHP. © 2006 Pearson Addison-Wesley. All rights reserved Origins and Uses of PHP - Origins - Rasmus Lerdorf
Chapter 9 Using Perl for CGI Programming. Computation is required to support sophisticated web applications Computation can be done by the server or the.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
Advance Database Management Systems Lab no. 5 PHP Web Pages.
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
PHP Forms and User Input The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input.
INTERNET APPLICATION DEVELOPMENT For More visit:
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Cookies & Sessions.
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.
Comp2513 Forms and CGI Server Applications Daniel L. Silver, Ph.D.
CSC 2720 Building Web Applications Cookies, URL-Rewriting, Hidden Fields and Session Management.
A little PHP. Enter the simple HTML code seen below.
Chapter 3 Servlet Basics. 1.Recall the Servlet Role 2.Basic Servlet Structure 3.A simple servlet that generates plain text 4.A servlet that generates.
PHP1-1 PHP Lecture 2 Xingquan (Hill) Zhu
Lecture 8 – Cookies & Sessions SFDV3011 – Advanced Web Development 1.
Chapter 6 Server-side Programming: Java Servlets
Slide 7-1 CHAPTER 7 Managing Multiple-Form Applications: Writing scripts with multiple screens.
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.
PHP2. PHP Form Handling The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input. Name: Age:
Cookies & Session Web Technology
CSC 2720 Building Web Applications Server-side Scripting with PHP.
Chapter 12 © 2003 by Addison Wesley Longman, Inc Origins and Uses of PHP - Origins - Rasmus Lerdorf Developed to allow him to track visitors.
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
Sessions in PHP – Page 1 of 13CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: Sessions in PHP Reading: Williams.
8 th Semester, Batch 2008 Department Of Computer Science SSUET.
ITM © Port, Kazman1 ITM 352 More on Forms Processing.
>> PHP: Insert Query & Form Processing. Insert Query Step 1: Define Form Variables Step 2: Make DB Connection Step 3: Error Handling Step 4: Define the.
 2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
SESSIONS 27/2/12 Lecture 8. ? Operator Similar to the if statement but returns a value derived from one of two expressions by a colon. Syntax: (expression)
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.
Web Page Designing With Dreamweaver MX\Session 1\1 of 9 Session 3 PHP Advanced.
Unit-6 Handling Sessions and Cookies. Concept of Session Session values are store in server side not in user’s machine. A session is available as long.
CGS 3066: Web Programming and Design Spring 2016 PHP.
PHP: Further Skills 02 By Trevor Adams. Topics covered Persistence What is it? Why do we need it? Basic Persistence Hidden form fields Query strings Cookies.
Lesson 11. CGI CGI is the interface between a Web page or browser and a Web server that is running a certain program/script. The CGI (Common Gateway Interface)
A little PHP. Enter the simple HTML code seen below.
12.1 Origins and Uses of PHP 12.2 Overview of PHP - Origins
CHAPTER 5 SERVER SIDE SCRIPTING
A little PHP.
CS 371 Web Application Programming
CGS 3066: Web Programming and Design Spring 2016
9.1 Origins and Uses of PHP 9.2 Overview of PHP - Origins
เอกสารประกอบการบรรยายรายวิชา Web Technology
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
DBW - PHP DBW2017.
Web Technologies PHP 5 Basic Language.
PHP Functions Besides the built-in PHP functions, we can create our own functions. A function is a block of statements that can be used repeatedly in.
19.10 Using Cookies A cookie is a piece of information that’s stored by a server in a text file on a client’s computer to maintain information about.
Chapter 19 PHP Part III Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall ©
ITM 352 Cookies.
Passing variables between pages
Cookies and Sessions in PHP
8th Semester, Batch 2008 Department of Computer Science SSUET.
>> PHP: Form Processing
PHP FORM HANDLING Post Method
Introduction to Web programming
Dr. John P. Abraham Professor UTRGV eCommerce CSCI 6314
<?php require("header.htm"); ?>
Lecture 5: Functions and Parameters
Chapter 12 Introduction to PHP.
9.1 Origins and Uses of PHP 9.2 Overview of PHP - Origins
Cookies and Sessions.
PHP-II.
PHP By Prof. B.A.Khivsara Note: The material to prepare this presentation has been taken from internet and are generated only for students reference and.
[Based in part on SWE 432 and SWE 632 materials by Jeff Offutt, GMU]
Presentation transcript:

1 CHAPTER 10 ADVANCED PHP

USER-DEFINED FUNCTIONS 2 USER-DEFINED FUNCTIONS Return Values Any type may be returned, including objects and arrays, using the return If a function returns a reference, the name of the function must have a pre-appended ampersand function &newArray($x) { … } The Scope of Variables An undeclared variable in a function has the scope of the function To access a non-local variable, it must be declared to be global, as in global sum; The Lifetime of Variables Normally, the lifetime of a variable in a function is from its first appearance to the end of the function’s execution static sum = 0; # sum is a static variable

EXAMPLE – PHP FUNCTION The output of this script 1 + 16 = 17 3 <html> <body> <?php function add($x,$y) { $total = $x + $y; Return $total; } echo "1 + 16 = " . add(1,16) ?> </body> </html> The output of this script 1 + 16 = 17

EXAMPLE – PHP FUNCTION 4 $big_sum = 0; function summer ($list){ global $big_sum; $sum = 0; foreach ($list as $value) $sum += $value; $big_sum += $sum; return $sum; } ……….. $ans1 = summer($list1); $ans2 = summer($list2); ………. Print (“The sum of all array elements is : $big_sum );

5 EXAMPLE – PHP FUNCTION The following example will redirect the browser to the following URL: http://www.w3schools.com/: <?php //Redirect browser header("Location: http://www.w3schools.com/"); ?><html> <body>......</body> </html>

FORM HANDLING Simpler with PHP than either CGI or servlets 6 FORM HANDLING Simpler with PHP than either CGI or servlets Forms could be handled by the same document that creates the form, but that may be confusing PHP particulars: It does not matter whether GET or POST method is used to transmit the form data PHP builds a variable for each form element with the same name as the element

EXAMPLE – PHP FORM 7 Look at the following example of an HTML form: <body><form action="welcome.php" method="POST"> Enter your name: <input type="text" name="name" /> Enter your age: <input type="text" name="age" /> <input type="submit" /> </form></body> </html> The example HTML page above contains two input fields and a submit button. When the user fills in this form and hits the submit button, the "welcome.php" file is called. <body>Welcome <?php echo $_POST["name"]; ?>.<br /> You are <?php echo $_POST["age"]; ?> years old!</body></html>

8 FILES PHP can: Deal with any files on the server Deal with any files on the Internet, using either http or ftp Instead of filehandles, PHP associates a variable with a file, called the file variable (for program reference) A file has a file pointer (where to read or write) $fptr = fopen(filename, use_indicator) Use indicators: r read only, from the beginning r+ read and write, from the beginning w write only, from the beginning (also creates the file, if necessary) w+ read and write, from the beginning (also creates the file, if necessary) a write only, at the end, if it exists (creates the file, if necessary) a+ read and write, read at the beginning, write at the end Because fopen could fail, use it with die

9 FILES Use file_exists(filename) to determine whether file exists before trying to open it Use fclose(file_var) to close a file Reading files Read all or part of the file into a string variable $str = fread(file_var, #bytes) To read the whole file, use filesize(file_name) as the second parameter Read the lines of the file into an array $file_lines = file(file_name) Need not open or close the file Read one line from the file $line = fgets(file_var, #bytes) Reads characters until eoln, eof, or #bytes characters have been read

FILES Reading files (continued) Writing to files 10 Read one character at a time $ch = fgetc(file_var) Control reading lines or characters with eof detection using feof (TRUE for eof; FALSE otherwise) while(feof($file_var)) { $ch = fgetc($file_var); } Writing to files $bytes_written = fwrite(file_var, string) fwrite returns the number of bytes it wrote Files can be locked (to avoid interference from concurrent accesses) with flock (just like Perl)

11 EXAMPLE – PHP FILES The example below reads a file character by character, until the end of file is true: <?php if (!($f=fopen("welcome.txt","r"))) exit("Unable to open file."); while (!feof($f)) { $x=fgetc($f); echo $x; } fclose($f); ?>

COOKIES Create a cookie with setcookie 12 COOKIES Create a cookie with setcookie setcookie(cookie_name, cookie_value, lifetime) e.g., setcookie("voted", "true", time() + 86400); Cookies must be created before any other HTML is created by the script Cookies are obtained in a script the same way form values are gotten There could be conflicts between GET, POST, and cookie variables PHP puts all POST form variables in their own array (hash) , HTTP_POST_VARS Ditto for GET form variables (HTTP_GET_VARS)

13 EXAMPLE – COOKIES The following example sets a cookie named "uname" - that expires after ten hours. <?php setcookie("uname", $name, time()+36000); ?><html> <body><p> A cookie was set on this page! The cookie will be active when the client has sent the cookie back to the server. </p></body> </html>

14 EXAMPLE – COOKIES The following example tests if the uname cookie has been set, and prints an appropriate message. <html> <body><?php if (isset($_COOKIE["uname"])) echo "Welcome " . $_COOKIE["uname"] . "!<br />"; else echo "You are not logged in!<br />"; ?></body> </html>

15 SESSION TRACKING For session tracking, PHP creates and maintains a session tracking id Create the id with a call to session_start with no parameters Subsequent calls to session_start retrieves any session variables that were previously registered in the session To create a session variable, use session_register The only parameter is a string literal of the name of the session variable (without the dollar sign) Example: count number of pages visited Put the following code in all documents session_start(); if (!IsSet($page_number)) $page_number = 1; print("You have now visited $page_number"); print(" of pages <br />"); $page_number++; $session_register("page_number");

16 SERVER-SIDE INCLUDES Server Side Includes (SSI) are used to create functions, headers, footers, or elements that will be reused on multiple pages. You can insert the content of one file into another file before the server executes it, with the require() function. The require() function is used to create functions, headers, footers, or elements that will be reused on multiple pages. The following example includes a header that should be used on all pages: <html> <body><?php require("header.htm"); ?><p> Some text </p><p> </p></body></html>