Lighting up the Magic LAMP - PowerPoint PPT Presentation

1 / 57
About This Presentation
Title:

Lighting up the Magic LAMP

Description:

Lighting up the Magic LAMP – PowerPoint PPT presentation

Number of Views:571
Avg rating:3.0/5.0
Slides: 58
Provided by: foss
Category:
Tags: lamp | lighting | magic | php

less

Transcript and Presenter's Notes

Title: Lighting up the Magic LAMP


1
Lighting up the Magic LAMP
  • Nirav Mehta
  • CEO, MagNet WebPublishing Pvt. Ltd.

2
Welcome
  • Welcome
  • What would you like to hear?
  • Objectives of this session
  • Understand what is LAMP
  • Learn how does it light up
  • We are going to go fast! So be prepared!

3
Introduction
  • GNU/Linux Apache MySQL PHP
  • Open Source, Free!
  • Apache 66.42 of all active sites
  • MySQL most popular open source DB
  • PHP 27.65 of domains, 45.54 of Apache
  • Very popular web development platform

4
Apache Web Server
  • HTTP daemon
  • Apache Software Foundation
  • Apache Modules
  • httpd.conf
  • What is .htaccess?
  • What is Virtual Hosting?

5
MySQL Database
  • Fast, stable, most popular open source DB
  • ANSI SQL 99, additional functionalities
  • Data storage formats MyISAM, InnoDB
  • Transactions, Views / SP / Triggers
  • Good security system, SSL
  • Replication
  • Embedded DB Library

6
MySQL Basics
  • SQL Basics
  • Create
  • Insert
  • Select
  • Update
  • Delete
  • Alter
  • etc.
  • MySQL command prompt
  • mysql -u username -p dbname

7
MySQL command line tool
8
PHPMyAdmin
9
Web - The Good Old Days
  • HTML
  • SSI
  • CGI Programming
  • C
  • Complex
  • Lengthy
  • Perl
  • Fantastic text processing
  • CPAN modules

10
Handling form data in C
  • include
  • include
  • include
  • include
  • define ishex(x) (((x) '0' (x) ((x) 'a'
  • (x) 'A'
    (x)
  • int htoi(char s)
  • int value
  • char c
  • c s0
  • if(isupper(c)) c tolower(c)
  • value(c '0' c 'a' 10) 16
  • c s1
  • if(isupper(c)) c tolower(c)
  • value c '0' c 'a' 10

11
Handling form data in C II
  • data getenv("QUERY_STRING")
  • if(data data)
  • params data dest data
  • while(data)
  • if(data'') dest' '
  • else if(data ''
    ishex((data1))ishex((data2)))
  • dest (char) htoi(data 1)
  • data2
  • else dest data
  • data
  • dest
  • dest '\0'
  • s strtok(params,"")
  • do
  • tmp strchr(s,'')
  • if(tmp)
  • tmp '\0'
  • if(!strcmp(s,"name")) name
    tmp1

12
Handling form data in Perl
  • use CGI qw(standard)
  • print header
  • print start_html('Form Example'),
  • h1('My Example Form'),
  • start_form,
  • "Name ", textfield('name'),
  • p,
  • "Age ", textfield('age'),
  • p,
  • submit,
  • end_form
  • if(param())
  • print "Hi ",em(param('name')),
  • "You are ",em(param('age')),
  • " years old"
  • print end_html

13
PHP Makes it Simple!
  • Form Example
  • Name

  • Hi ! You are years
    old

14
Enter PHP!
  • PHP Hypertext Preprocessor

15
The PHP Approach
  • Pragmatic approach to the Web Problem
  • Direct and obvious solution for a simple problem
  • Scalability driven by this simplicity
  • Rapid Application Development

16
The Road to PHP
  • Rasmus Lerdorf
  • June 8, 1995 first official release
  • October 17, 1995 public release - PHP/FI
  • June 6, 1998 - PHP3
  • Currently PHP 4.3

17
PHP Usage
  • December 2002
  • On more than 10 million domains
  • About 28 of all the domains

18
PHP Works Everywhere!
  • Operating Systems
  • UNIX, Linux, Win32, QNX, MacOS, OSX, OS/2, BeOS,
    AS/400, OS/390
  • Server Interfaces CGI and Module
  • Apache, CGI/FastCGI, thttpd, fhttpd, phttpd,
    ISAPI (IIS, Zeus), NSAPI (Netscape iPlanet), Java
    servlet, AOLServer, Roxen/Caudium, SRM (Script
    Running Machine), Tux, pi3web
  • Command Line Interpreter

19
Embedded Scripting Language
  • Embedded in HTML
  • PHP Tags
  • Switching between HTML and PHP

20
Server Side Language
  • Server processes and outputs HTML
  • So if the HTML file contains
  • "view source" in the browser
  • Hello World

21
Basics Variables Operators
  • Variables Expressions
  • foo 1
  • bar "Testing"
  • xyz 3.14
  • foo foo 1
  • ?
  • Operators
  • Arithmetic, Assignment, Bitwise, Comparison
  • Error control, Execution, Incr/Decr, Logical

22
Basics Arrays
  • Arrays
  • foo1 1
  • foo2 2
  • bar12 3
  • ?

23
Basics Functions
  • Functions calling them
  • phpinfo()
  • foo()
  • len strlen(foo)

24
Basics Control Structures
  • for, while, do while
  • while(foo)
  • ...
  • ?

25
Basics Output, Comments
  • echo, printf()
  • echo foo
  • printf(".2f",price)
  • ?
  • Comments
  • // Single line comments
  • /
  • Multi line comments
  • /

26
Form Handling with PHP - I
  • Form Code
  • " method"POST"
  • Your name
  • You age
  • Output in Browser

27
Form Handling with PHP II
  • Processing PHP Script
  • Hi .
  • You are years old.
  • Output in browser
  • Register Globals Off
  • Hi .
  • You are years old.

28
Language Syntax
  • PHP is mixture of C, Perl HTML

for (loop -5 loop 0)
echo "" echo
"loop
\n" while(--loop) switch(i
2) case 0 echo "Even
\n"
break case 1 echo
"Odd
\n" break
29
Basic Data types - I
  • Numbers (integer and real)
  • Strings
  • Dynamic typing
  • a 1234
  • b 1.25
  • c 'Hello'
  • d c World
  • e 10 1.5
  • echo "a - b - c - d"
  • // 1234 - 1.25 - Hello - Hello World - 6.5

30
Basic Data types II
  • Booleans
  • status true
  • if (status) status false
  • Ordered and Associative
  • a0 1
  • a1 "foo"
  • a 1.57
  • catch_it'cat' "mouse"
  • catch_it'dog' "cat"

31
User Defined Functions
  • Pass by value / reference
  • Default values
  • function calc (op1, op2, debugtrue)
  • val op1 op2
  • if (debug)
  • echo "op1 op2 val
    \n
  • return val
  • operand1 3 operand2 2
  • echo calc (operand1, operand2)

32
Object Oriented PHP
  • Workable OO support, mature in PHP5
  • Classes, objects, inheritance, this
  • class Cart
  • var items var id
  • function Cart()
  • this-id uniqid()
  • function add_item(prodID, num)
  • this-itemsprodID num
  • cart new Cart
  • cart-add_item(1, 3) // add 3 qty of product 1

33
Date / Time Functions
  • Unix Epoch time
  • date(), time(), mktime()
  • Feb 15, 2003 122545
  • Many complex calculations possible

34
String Manipulation
  • Lot of functions strlen, substr, trim, strstr,
    str_replace, strtolower ...
  • Regular Expressions ereg, preg
  • String Tokens explode, implode, strtok
  • a explode("", "Thisstringhasdelimiters.")
  • while (list(,value) each(a))
  • if (strcmp(value, "has") 0)
  • echo "had "
  • else echo value." "
  • // This string had delimiters.

35
File Operations
  • fopen, fclose, fread, fwrite (like C)
  • file, read_file, file_get_contents
  • Possible to open HTTP and FTP URLs
  • file "sample.txt"
  • fp fopen(file, "r")
  • contents fread(fp, filesize(file)
  • fclose(fp)
  • ?

36
Variable Scope
  • Default file scope, local in functions
  • Global scope in functions
  • GLOBAL var
  • Static variables
  • Static var
  • System Global variables
  • _POST, _GET, _COOKIE, _SESSION, _REQUEST,
    _SERVER, _ENV, _FILES

37
Guest Book application I
  • My Guestbook
  • Welcome to my Guestbook
  • Please write me a little note below
  • " method"POST"
  • wrapvirtual
  • fp fopen("/tmp/notes.txt","a")
  • fputs(fp,nl2br(note.'').'
    ')
  • fclose(fp)
  • ?The entries so far

38
Guest Book application II
39
SQL Example
  • Typical DB interaction
  • mysql_pconnect("db.server.com","username","passwo
    rd")
  • mysql_select_db("products")
  • result mysql_query("SELECT FROM details")
  • if (result mysql_num_rows(result))
  • echo "\n"
  • echo "Name
  • Description\n"
  • while (a mysql_fetch_array(result))
  • echo "aname
  • adescr"
  • echo ""
  • else echo "Nothing in here!"
  • Supports almost all DBs
  • IBM DB2, Informix, Ingres, MS-SQL, MySQL, ODBC,
    Oracle, PostgreSQL, Sybase,dBase, FilePro, dbm...

40
Guest Book on steroids
  • My Guestbook
  • Please write me a little note
    below
  • "
    method"POST"
  • wrapvirtual
  • mysql_connect('localhost', 'user', 'pass')
  • mysql_select_db('database')
  • if(isset(note))
  • ts date("Y-m-d His")
  • mysql_query("insert into comments (ID, note,
    ts)
  • values (0,'note','ts')")
  • ?
  • The entries so far
  • comments")
  • while(rowmysql_fetch_row(result))
  • echo row0 ." " . row1 . " " .
    row2 ."
    \n"

41
DB Abstraction
  • PEAR DB Class
  • require_once 'DB.php'
  • db DBconnect('odbc//userpw_at_host/db')
  • stmt db-prepare('SELECT FROM comments')
  • result db-execute(stmt)
  • while(row db-fetchrow(result))
  • while(row as field value )
  • echo "field value
    \n"
  • db-disconnect()
  • ?

42
Headers Cookies
  • Header
  • Header('Location http//www.php.net')
  • SetCookie
  • SetCookie('name', 'value')
  • echo HTTP_COOKIE_VARS'name'
  • Headers must be sent before content

43
Sessions
  • PHPSESSID
  • Using sessions
  • session_start()
  • session_register(variable)
  • _SESSION'name' value
  • echo _SESSION'name'
  • session_destroy()

44
Image Generation
  • GD Library
  • Header("Content-type image/png")
  • im ImageCreate(630,80)
  • blue ImageColorAllocate(im,0x5B,0x69,0xA6)
  • white ImageColorAllocate(im,255,255,255)
  • black ImageColorAllocate(im,0,0,0)
  • ImageTTFText(im, 45, 0, 10, 57, black, "CANDY",
    text)
  • ImageTTFText(im, 45, 0, 6, 54, white, "CANDY",
    text)
  • ImagePNG(im)
  • ?
  • "

45
What more can PHP do?
  • PDF Generation

46
What more can PHP do?
  • Smarty templates
  • Separation of logic and layout
  • Templates
  • It's own language
  • Caching

47
Smarty Example tpl file
  • My Guestbook
    Welcome to my
    GuestbookPlease write me a little note
    belowmethod"POST"name"note" wrapvirtualtype"submit" value"Send it"The
    entries so far
  • section namemsgs loopnotes  notesmsgs.id
    notesmsgs.ts notesmsgs.comment
    /s
    ection

48
Smarty Example PHP file
  • Smartysmarty-debugging truemysql_connect(
    'localhost','user','pass')mysql_select_db('datab
    ase')/ insert new note /if(isset(note))
        ts date("Y-m-d His")    mysql_query("
    insert into comments values                  (0,'
    note','ts')")/ read existing notes
    /result mysql_query('select from
    comments')if(!result) echo mysql_error()while
    (notesmysql_fetch_array(result,MYSQL_ASSOC))
    smarty-assign('notes',notes)smarty-displa
    y("guestbook.tpl")?

49
What more can PHP do?
  • LDAP
  • SNMP
  • IMAP (POP, NNTP)
  • FTP
  • Flash
  • XML parser / XSLT
  • DCOM (Win32 only)
  • Java Connectivity
  • Curl
  • zlib (compressed IO)
  • Charset conversion
  • ASPELL/PSPELL
  • MCRYPT
  • WDDX
  • Ncurses
  • 100 extensions!

50
Latest Developments
  • PEAR and PECL
  • SOAP
  • Zend Engine 2
  • New Object model
  • Unified Constructors and Destructors
  • Objects are references
  • Exceptions

51
More
  • PHP5
  • Java and PHP

52
LAMP
  • Bringing all the forces together
  • LAMP - AMP on Linux
  • WAMP - AMP on Windows
  • Most popular web development platform
  • Many resources available
  • Manuals, tutorials, newsgroups
  • Open source applications
  • hotscripts.com, freshmeat.net
  • Yahoo uses PHP!

53
Development Environment
  • Apache with PHP as module / CGI
  • MySQL
  • PhpMyAdmin
  • IDEs Zend Studio / PHPEd / PHP Editor ...
  • Any text editor would do!
  • Commercial products / support available

54
Zend Studio IDE
55
Resources
  • Apache - http//httpd.apache.org
  • PHP
  • Home Page http//www.php.net
  • Manual http//php.net/manual
  • Tutorial http//php.net/tut.php
  • Books http//php.net/books.php
  • MySQL - http//www.mysql.com
  • Zend - http//www.zend.com

56
Credits
  • Open Source / FSF
  • PHP
  • Rasmus Lerdorf's presentation
  • MagNet

57
Thank you!
  • Nirav Mehta
  • nirav_at_magnet-i.com
Write a Comment
User Comments (0)
About PowerShow.com