Meljun_cortes_php

PHP A Server-side Scripting Technology Dennis Gonzales Kathleen Dimaano Saturday, July 12, 2008 3/12/2012 Objectives To introduce PHP  To differentiate client-side and serverside scripting technology  To differentiate the 3 passing parameters in PHP such as Hyperlink, Form Method and Session / Cookies.  To apply database to your web  To publish your WebPages  What is PHP? • • • • Originally, PHP stood as Personal Homepage Rasmus Lerdorf, 1994 It was renamed as Perl HyperText Preproces
View more...
   EMBED

Share

Preview only show first 6 pages with water mark for full document please download

Transcript

PHP A Server-side Scripting Technology Dennis Gonzales Kathleen Dimaano Saturday, July 12, 2008 3/12/2012 Objectives To introduce PHP  To differentiate client-side and serverside scripting technology  To differentiate the 3 passing parameters in PHP such as Hyperlink, Form Method and Session / Cookies.  To apply database to your web  To publish your WebPages  What is PHP? • • • • Originally, PHP stood as Personal Homepage Rasmus Lerdorf, 1994 It was renamed as Perl HyperText Preprocessor It is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. PHP is easy to learn and runs efficiently on the server side It is an Open Source System (OSS) PHP runs on different platforms (Windows, Linux, Unix, etc.) PHP is compatible with almost all servers used today (Apache, IIS, etc.) PHP is FREE to download from the official PHP resource: • • Where to start?    Install an Apache server on a Windows or Linux machine Install PHP on a Windows or Linux machine Install MySQL on a Windows or Linux machine Definition of Terms  A web server is software that sends out web pages in response to requests from web browsers. Ex. Windows 95/98/NT – Personal Web Browser Windows 2000, XP, etc. – Internet Information Server Linux – Apache Server Web Server http://friendster.com/login.php http://192.168.0.12/login.php http://CL1-12/login.php http://localhost/login.php Web Browser Welcome to PHP Web Browser Welcome to PHP Web Browser Welcome to PHP http://friendster.com/login.php Definition of Terms  An application server is software that helps a web server process web pages containing server-side scripts or tags. When such a page is requested from the server, the web server hands the page off to the application server for processing before sending the page to the browser. Examples: MicroSoft – Active Server Pages (ASP) Sun MicroSystems – Java Server Pages (JSP) Open Source System – Perl HyperText Preprocessor Static WebPages Dynamic WebPages Welcome”; ?> Welcome Client and Server-Side Scripting Client Side Server-Side $a\n”; $a=$a+1; } ?> Then save your work as sample6.php at c:\wamp\www\dennis Example # 7: Passing Parameter using Hyperlink

The Area of 10 Then save your work as sample7.htm at c:\wamp\www\dennis Example # 7: Passing Parameter using Hyperlink Notice the URL in the address bar: http://localhost/h1.php?x=10 Note: If the global variable is one time process only $area=$_GET[„x‟] * $_GET[„x‟] Then save your work as h1.php at c:\wamp\www\dennis Example # 8: Passing Parameter using Hyperlink (2 variables)

The sum of 10 and 5 Then save your work as sample8.htm at c:\wamp\www\dennis Example # 8: Passing Parameter using Hyperlink (2 variables) Then save your work as h2.php at c:\wamp\www\dennis Example # 9: Passing Parameter using Hyperlink - pass data using single page

The sum of 10 and 5 Note: Notice the output once you executes the PHP program it will automatucally display “The sum of 0 and 0 is 0” Then save your work as sample9.php at c:\wamp\www\dennis Example # 9: Passing Parameter using Hyperlink - pass data using single page Note: isset() – used to set a value before the execution of the php script

The sum of 10 and 5 Then save your work as sample9.php at c:\wamp\www\dennis Activity # 1: Create a picture gallery that will display the selected picture and caption within the same page Then save your work as gallery.php at c:\wamp\www\dennis Activity 1: sagot


$b"; } ?>
Passing Parameter using Form Method get and post GET method = pass the query string = 8192 characters as the maximum = using GET it always has (?) ex. Friendster.com/login.php?username=dennis&password=dennis POST method = pass the form data = unlimited ex. Friendster.com/login.php Example # 10: Passing Parameter using Form Get - pass data using single page
num1
num2
The sum of $a and $b is $sum"; } ?> Then save your work as formget1.php at c:\wamp\www\dennis Example # 11: Passing Parameter using Form POST - pass data using single page
num1
num2
The sum of $a and $b is $sum"; } ?> Then save your work as formpost.php at c:\wamp\www\dennis Passing Parameter using Session and cookies What is Cookies? Cookies are a way for a server to store information about the user (on the user‟s machine) so that the server can remember the user over the course of the visit or through several visits. Advantages of Cookies Marginally easier to create and retrieve Require slightly less work from the server Normally persist over a longer a period of time Security is less of an issue and only a minumum of data is being stored. What is Session? Session provides a way for you to track data for a user over a series of pages. Data stored on the server. Advantages of Sessions Sessions are more secure, because the data isn‟t transmitted back and forth between the client and server repeatedly. Sessions let you store more information than you can in a cookie. Example of Cookies and Sessions Information Name : PHPSESSID Content : 4bcc48dc87cb4b54d63f99da23fb41e1 Host : Localhost Path :/ Server Source : No Expires :Tues, February 6, 2007 2:41:23 PM (Cookies) At end of session Passing Parameter using Session To implement the session you need the following method •session_start() – use to start session •session_unset() – to reset the session value •session_destroy() – to destroy the session id Creating session id ex. $_SESSION[„VARNAME‟]; Example # 12: Passing Parameter using Session Figure 1: Content of session id To view the session id stored to your webserver Open c:\wamp\tmp\ then right click the session id Then open with – in any text editor (ex. Notepad) Then save your work as session1.php at c:\wamp\www\dennis Example # 12: Passing Parameter using Session – call the defined session To view the session id stored to your webserver Open c:\wamp\tmp\ then right click the session id Then open with – in any text editor (ex. Notepad) Then save your work as session2.php at c:\wamp\www\dennis Example # 12: Passing Parameter using Session – reset and destroy the value of session To view the session id stored to your webserver For unset value: username s:6 “ ” and password s:6 “ ” Press F5 then notice the effect: Then save your work as session3.php at c:\wamp\www\dennis Scenario: Login page is existing but you can redirect your page to the linking pages. http://localhost/Login.htm http://localhost/Welcome.htm http://localhost/logout.htm Enter your username Enter your Password Welcome to my personal homepage Goodbye Login Clear Example # 12: Passing Parameter using Session – call the defined session, to validate username Sorry, you cannot view this page!"; exit(); } ?>

sample session script Note: notice the effect once you run session3.php then click back button Then save your work as session2.php at c:\wamp\www\dennis Example # 12: Passing Parameter using Session – reset and destroy the value of session Today is " . date(“F d, y “); unset_session(); destroy_session(); } else { print "Sorry, you cannot view this page!"; exit(); } ?>

Session start again Then save your work as session3.php at c:\wamp\www\dennis Implementing Database Database – it is a collection of related files File / Table – it is a collection of records Record – it is a collection of fields Fields – it is a collection of bytes Byte – it is a collection of bits Bit – it is a collection of 0 and 1 MySQL is a cross platform open source database server software. It is used extensively in web development. Implementing Database using PHPAdmin Create database: student Create table name: login Field Username Password type varchar varchar size 15 15 To insert new record: click Insert Ex. Username: ama password: ama Implementing Database with Basic commands in mysql Click SQL menu: 1. Display all records select * from login; 2. Display specific column select username from login; 3. Display records with condition select * from login where username=„ama‟; 4. Display all username that start with letter „a‟ select * from login where username like „a%‟; 5. Display all username consisting letter „m‟ select * from login where username like „%m%‟; 6. Insert another record Insert into login values („dennis‟,‟dennis‟) 7. Delete all record whose username is ama Delete from login where username=„ama‟; Implementing Database with PHP Some basic mySQL function The mysql_connect() function is used to connect to an mySQL data source. The function takes four parameters: the data source name, username, password, and an optional cursor type. The mysql_query() function is used to execute an SQL statement. The mysql_select_db() function is used to read mySQL database The mysql_query() function is used to execute an SQL statement. The mysql__fetch_rows() function is used to return records from the result-set. This function returns true if it is able to return rows, otherwise false. Insertion of record with PHP Username:
Password:
no connection”); mysql_select_db (“student”); $username=$_GET[„username‟]; $password=$_GET[„password‟]; $insert=“insert into login values („$username‟,‟$password‟)”; mysql_query($insert); print “

Record Saved!”; } ?> Then save your work as insert.php at c:\wamp\www\dennis Publishing of your website We need the ff: for the implementation of online website • Domain Host • Control Panel • Compiler • Database • Web server Then save your work as display.php at c:\wamp\www\dennis