56
Intro to Dynamic Web Pages Jussi Pohjolainen Tampere University of Applied Sciences

Intro to Dynamic Web Pages

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Intro to Dynamic Web Pages

Intro  to  Dynamic  Web  Pages  

Jussi  Pohjolainen  Tampere  University  of  Applied  Sciences  

Page 2: Intro to Dynamic Web Pages

Contents  

•  IntroducAon  to  Programming  Concepts  •  Web  ApplicaAon  Development  –  Architecture  –  StaAc  vs.  Dynamic  Web  Pages?  –  Client-­‐side  vs.  Server-­‐side  scripAng  

•  PHP  Programming  –  Embedding  PHP  into  Web  Pages  –  xhtml  forms  and  user  input  via  GET  –  Examples  of  Web  ApplicaAons  

Page 3: Intro to Dynamic Web Pages

INTRODUCTION  TO  PROGRAMMING  

Page 4: Intro to Dynamic Web Pages

Program?  

•  Computer  program,  applicaAon,  is  just  a  list  of  instrucAons  to  the  computer  

•  Program  gets  input  from  the  user  •  Program  has  visible  output  

Page 5: Intro to Dynamic Web Pages

Machine  Language  

•  Program  is  just  an  instrucAons  to  the  CPU  •  CPU  understands  only  machine  language  •  Machine  language  is  very  hard  to  implement.  

Page 6: Intro to Dynamic Web Pages

Programming  Language  

•  Because  machine  language  is  so  hard  to  use  we  use  some  programming  language  

•  The  computer  understands  only  machine  language,  so  there  is  a  barrier  between  the  programming  language  and  machine  language  

•  Programming  language  can  be  compiled  to  machine  language  

Page 7: Intro to Dynamic Web Pages

Problem  

Page 8: Intro to Dynamic Web Pages

SoluAon  

Page 9: Intro to Dynamic Web Pages

Programming  Languages  

•  There  are  lot  of  different  programming  languages  

•  C++,  PHP,  Java,  C,  ...  •  All  these  programming  languages  have  same  principals.  

Page 10: Intro to Dynamic Web Pages

Three  Basic  Rules  

•  1)  Sequence  •  2)  Choice  •  3)  Repeat  

Page 11: Intro to Dynamic Web Pages

Sequence  

•  Statements  are  executed  in  the  same  sequence  as  they  are  listed  in  the  source  code.  1.  print to the screen "what is your name"!2.  read user input to variable NAME!3.  print to the screen "You have a great name, "!4.  print NAME!5.  print "!"!

Page 12: Intro to Dynamic Web Pages

Choice  

•  With  choice  one  can  choose  what  sentences  are  executed  based  on  condiAon.    

1.  print to the screen "what is your name"!2.  read user input to variable NAME!3.   if(NAME = "Jussi")!4.   print to the screen "You have a silly name, "!5.   else!6.   print to the screen "You have a great name, "!7.  print NAME!8.  print "!"!

Page 13: Intro to Dynamic Web Pages

Repeat  

•  With  repeat  one  can  repeat  statements.  !1. print to the screen "what is your name"!2. read user input to variable NAME!3.  while(NAME = "Jussi")!4.   print "This program is forbidden from Jussi"!5.   print "Give other name: "!6.   read user input to variable NAME!7. print to the screen "You have a great name, "!8. print NAME!9. print "!"!

Page 14: Intro to Dynamic Web Pages

What  is  the  output?  

i = 0!while(i < 10)! print to the screen "Hello!"! i = i + 1!

Page 15: Intro to Dynamic Web Pages

Example  print to the screen "Give your name"!put the user input into variable NAME!print to the screen "Give number"!put the user input into variable NUMBER!if(NUMBER < 1)! print to the screen "You have to give positive number"!else! i = 0! while(i < NUMBER)! print NAME! i = i + 1!!!

Page 16: Intro to Dynamic Web Pages

Pseudocode  to  PHP  

•  Previous  examples  used  pseudocode  (not  real  programming  language)  

•  Examples  are  quite  close  to  real  programming  languages,  like  PHP  

Page 17: Intro to Dynamic Web Pages

Print  

•  PrinAng  to  the  screen  •  Pseudocode  –  print to the screen "Give your name"  

•  PHP  –  print "Give your name";!

Page 18: Intro to Dynamic Web Pages

User  Input  

•  GeXng  user  input  •  Pseudocode  –  put the user input into variable NAME!

•  PHP  –  $name = fgets(STDIN);  

Page 19: Intro to Dynamic Web Pages

Simple  Example  with  PHP  

<?php!!print "Give your name: ";!$name = fgets(STDIN);!print "You have a nice name: ";!print $name;!!?>!

Page 20: Intro to Dynamic Web Pages

More  Complicated  Example:  PHP  <?php!!print "Give your name: ";!$name = fgets(STDIN);!print "Give number: ";!$number = fgets(STDIN);!!if($number < 1)!{! print "You have to give positive number";!}!else!{! $i = 0;! while($i < $number)! {! print $name;! $i = $i + 1;! }!}!!?>!!

Page 21: Intro to Dynamic Web Pages

Exercises  

hZp://Anyurl.com/my-­‐exercises  

Page 22: Intro to Dynamic Web Pages

WEB  APP  BASIC  CONCEPTS  

Page 23: Intro to Dynamic Web Pages

Intro  to  HTTP  

•  HTTP  (Hypertext  transfer  protocol)  is  a  stateless  protocol,  which  is  meant  to  transfer  informaAon  on  intranets  and  World  Wide  Web.  –  RFC2616:  –  hZp://www.w3.org/Protocols/rfc2616/rfc2616.html  

•  HTTP  is  a  request  /  response  standard  between  client  and  server  

Page 24: Intro to Dynamic Web Pages

Clients  and  Servers  

•  Client  –  Client  makes  a  hZp  request.  –  Client  can  be  a  web  browser,  spider  or  other  end-­‐user  tool  

–  Client  is  referred  as  a  user  agent  •  Server  –  Stores  informaAon  and  makes  them  available  to  the  client  

–  Gives  hZp  response  to  the  client  

Page 25: Intro to Dynamic Web Pages

Request  and  Response  

Client  User-­‐agent:  Firefox  

Client  Apache  HTTP  Server  

request  

response  

Page 26: Intro to Dynamic Web Pages

Response  when  Dealing  with  Scripts  

Page 27: Intro to Dynamic Web Pages

Three-­‐Aered  Web  Site:  LAMP  Client  User-­‐agent:  Firefox  

Server  Apache  HTTP  Server  

example  request  GET / HTTP/1.1!Host: www.tamk.fi!User-Agent: Mozilla/5.0 (Mac..)!...!!

response  

Database  MySQL  

PHP  

Page 28: Intro to Dynamic Web Pages

Server  Side  Techniques  

•  Server  side  scripAng  requires  installaAon  on  the  server  side  

•  Typically  client  sees  only  xhtml  and  it  is  unaware  that  the  xhtml  was  produced  by  a  server  side  script  

•  Does  not  require  any  installaAons  or  add-­‐ons    on  the  client.  

Page 29: Intro to Dynamic Web Pages

Server  Side  Techniques  

•  PHP  •  Java  EE:  Servlet,  JSP  •  .NET  •  CGI  /  Perl  (Very  old)  •  Ruby  •  …  

Page 30: Intro to Dynamic Web Pages

Client  Side  Techniques  

•  Requires  that  the  client  supports  the  technique  

•  JavaScript,  Applet,  Flash…  

Page 31: Intro to Dynamic Web Pages

PHP:  HYPERTEXT  PREPROCESSOR  

Page 32: Intro to Dynamic Web Pages

IntroducAon  to  PHP  

•  PHP  is  a  computer  scripAng  language.  •  Originally  designed  for  producing  dynamic  web  pages  

•  Appeared  in  1995  •  PHP  Group  is  responsible  for  the  language,  no  formal  specificaAon  

•  Free  soeware  •  Runs  on  most  operaAng  systems  and  plaforms  •  URL:  hZp://www.php.net  

Page 33: Intro to Dynamic Web Pages

PHP  in  Command  Line  

Page 34: Intro to Dynamic Web Pages

Running  the  same  Program  via  Web  Browser  

Page 35: Intro to Dynamic Web Pages

The  Source  Code  of  the  Web  Page  

Page 36: Intro to Dynamic Web Pages

Problem?  

•  The  problem  here  is  that  the  output  is  not  valid  (x)html!  

•  The  output  of  the  PHP-­‐program  should  be  (x)html!  

Page 37: Intro to Dynamic Web Pages

One  possible  soluAon  <?php!print "<html>\n";!print " <head><title>Example</title></head>\n";!print " <body>\n";!!$now = date("d.m.Y");!print " <p>" . $now . "</p>\n";!!print " </body>\n";!print "</html>\n";!?>!!!

Page 38: Intro to Dynamic Web Pages

Result  

Page 39: Intro to Dynamic Web Pages

Easier  way  <html>! <head><title>Example</title></head>! <body>! <p>! <?php! $now = date("d.m.Y");! print $now;! ?> ! </p> ! </body>!</html>!!

Page 40: Intro to Dynamic Web Pages

Valid  XHTML  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"! "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">!<html xmlns="http://www.w3.org/1999/xhtml">!<head>! <title>Example</title>! <meta http-equiv="content-type" content="application/xhtml+xml;

charset=utf-8" />!</head> !<body>! <p>! <?php! $now = date("d.m.Y");! print $now;! ?> ! </p> ! </body>!</html>!!

Page 41: Intro to Dynamic Web Pages

EXAMPLE:  SIMPLE  SLOT  MACHINE  

Page 42: Intro to Dynamic Web Pages

Exercises  

Page 43: Intro to Dynamic Web Pages

USER  INPUT  VIA  GET  

Page 44: Intro to Dynamic Web Pages

User  Input  in  CLI  

•  PHP  CLI  –  $name = fgets(STDIN);!

•  PHP  WEB  –  $name = $_GET['key'];!

Page 45: Intro to Dynamic Web Pages

Example  <html>! <head><title>Example</title></head>! <body>! ! <?php! $userInput = $_GET['key'];! print $userInput;! ?>! ! </body>!</html>!    

Page 46: Intro to Dynamic Web Pages

Result?  

Page 47: Intro to Dynamic Web Pages

Giving  user  input  

Page 48: Intro to Dynamic Web Pages

Giving  user  input  

Page 49: Intro to Dynamic Web Pages

Example  <html>! <head><title>Example</title></head>! <body>! ! <?php! $name = $_GET['name'];! $age = $_GET['age'];! print "Your name is " . $name;! print " and your are " . $age;! print " years old.";! ?>! ! </body>!</html>!    

Page 50: Intro to Dynamic Web Pages

Example  

Page 51: Intro to Dynamic Web Pages

Example  

Page 52: Intro to Dynamic Web Pages

USER  INPUT  VIA  FORMS  

Page 53: Intro to Dynamic Web Pages

Simple  StaAc  Web  Page  with  Form:  form.html!

<html>! <head>! <title>Get-example</title>! </head>! <body>!!<h1>Fill the form</h1>!!<form action="myprogram.php" method="GET">!!<p>Name!!<input type="text" name="name" /></p>!

<p>Age!!<input type="text" name="age" /></p>!!<input type="submit" value="Send" />!!</form>!!!

</html>!

Page 54: Intro to Dynamic Web Pages

form.html  

Page 55: Intro to Dynamic Web Pages

Result:  myprogram.php  

Page 56: Intro to Dynamic Web Pages

EXERCISES