Server side basics - webstepbook.com

1 Server side basics CS380 URLs and web servers 2 http://server/path/file Usually when you type a URL in your browser: Your computer looks up the server's IP address using DNS Your browser connects to that IP address and requests the given file The web server software (e.g. Apache) grabs that file from the server's local file system CS380

The server sends back its contents to you 3 URLs and web servers (cont.) Apache, Websphere SW(Java Servlets, XML Files) Web/Application Server Database 4 URLs and web servers (cont.) http://www.facebook.com/ home.php

Some URLs actually specify programs that the web server should run, and then send their output back to you as the result: CS380 The above URL tells the server facebook.com to run the program home.php and send back its output 5 Server-Side web programming Server-side pages are programs written using one of many web programming languages/frameworks

CS380 examples: PHP, Java/JSP, Ruby on Rails, ASP.NET, Python, Perl 6 Server-Side web programming (cont.) Also called server side scripting: Dynamically edit, change or add any

content to a Web page Respond to user queries or data submitted from HTML forms Access any data or databases and return the results to a browser Customize a Web page to make it more useful for individual users Provide security since your server code cannot be viewed from a browser 7 Server-Side web programming (cont.) Web server:

contains software that allows it to run server side programs sends back their output as responses to web requests Each language/framework has its pros and cons CS380 we use PHP What is PHP? 8 PHP stands for "PHP Hypertext Preprocessor"

Server-side scripting language Used to make web pages dynamic: CS380 provide different content depending on context interface with other services: database, email, etc. authenticate users process form information PHP code can be embedded 9 Lifecycle of a PHP web

request Hello.php Hello world! Users Server Why PHP? 10 Free and open source Compatible as of November 2006, there were more than 19 million websites (domain nam es) using

PHP. Simple CS380 Hello World! 11 PHP Hello world! output CS380 Viewing PHP output 12

Hello world! 13 PHP Basic Syntax CS380 PHP syntax template 14 HTML content HTML content HTML content ...

PHP Contents of a .php file between are executed as PHP code All other contents are output as pure HTML We can switch back and forth between HTML and PHP "modes" Console output: print 15 print "text"; PHP print "Hello, World!\n"; print "Escape \"chars\" are the SAME as in Java!\n"; print "You can have line breaks in a string."; print 'A string can use "single-quotes". It\'s cool!'; PHP Hello world! Escape "chars" are the SAME as in Java! You can have line

breaks in a string. A string can use "single-quotes". It's cool! output CS380 Variables 16 $name = expression; $user_name = mundruid78"; $age = 16; $drinking_age = $age + 5; $this_class_rocks = TRUE; PHP PHP

names are case sensitive names always begin with $, on both declaration and usage always implicitly declared by assignment (type is not written) a loosely typed language (like JavaScript or Python) Variables 17 basic types: int, float, boolean, string, array, object, NULL PHP converts between types

automatically in many cases: test type of variable with is_type functions, e.g. is_string gettype function returns a variable's type as a string string int auto-conversion on + int float auto-conversion on / type-cast with (type): Arithmetic operators 18

+ - * / % . ++ -= += -= *= /= %= .= many operators auto-convert types: 5 + "7" is 12 CS380 Comments 19 # single-line comment // single-line comment /* multi-line comment */ PHP like Java, but # is also allowed CS380

a lot of PHP code uses # comments instead of // String Type 20 $favorite_food = "Ethiopian"; print $favorite_food[2]; $favorite_food = $favorite_food . " cuisine"; print $favorite_food; PHP zero-based indexing using bracket notation there is no char type; each letter is itself a String string concatenation operator is . (period), not +

5 + "2 turtle doves" === 7 5 . "2 turtle doves" === "52 turtle doves" can be specified with "" or '' CS380 String Functions 21 # index 0123456789012345 $name = "Stefanie Hatcher"; $length = strlen($name); $cmp = strcmp($name, "Brian Le"); $index = strpos($name, "e"); $first = substr($name, 9, 5); $name = strtoupper($name); PHP CS380

String Functions (cont.) 22 Name strlen strpos substr strtolower, strtoupper trim explode, implode strcmp CS380 Java Equivalent length indexOf substring toLowerCase, toUpperCase trim split, join compareTo

Interpreted Strings 23 $age = 16; print "You are " . $age . " years old.\n"; print "You are $age years old.\n"; # You are 16 years old. PHP strings inside " " are interpreted variables that appear inside them will have their values inserted into the string strings inside ' ' are not interpreted: print ' You are $age years old.\n '; # You are $age years old. \n PHP

CS380 Interpreted Strings (cont.) 24 print "Today is your $ageth birthday.\n"; # $ageth not found print "Today is your {$age}th birthday.\n"; PHP if necessary to avoid ambiguity, can enclose variable in {} CS380 Interpreted Strings (cont.) 25 $name = Xenia"; $name = NULL; if (isset($name)) { print "This line isn't going to be reached.\n";

} PHP a variable is NULL if it has not been set to any value (undefined variables) it has been assigned the constant NULL it has been deleted using the unset function can test if a variable is NULL using the CS380 isset function for loop (same as Java) 26

for (initialization; condition; update) { statements; } PHP for ($i = 0; $i < 10; $i++) { print "$i squared is " . $i * $i . ".\n"; } PHP CS380 bool (Boolean) type 27 $feels_like_summer = FALSE; $php_is_great = TRUE; $student_count = 7; $nonzero = (bool) $student_count; # TRUE PHP

the following values are considered to be FALSE (all others are TRUE): 0 and 0.0 (but NOT 0.00 or 0.000) "", "0", and NULL (includes unset variables) arrays with 0 elements FALSE prints as an empty string (no output); TRUE prints as a 1 CS380 if/else statement 28

if (condition) { statements; } elseif (condition) { statements; } else { statements; } PHP CS380 while loop (same as Java) 29 while (condition) { statements; } do { statements; } while (condition); PHP

CS380 PHP Math operations 30 $a = 3; $b = 4; $c = sqrt(pow($a, 2) + pow($b, 2)); PHP math functions abs min ceil pow cos rand

floor log round sin log10 sqrt math constants M_PI CS380 M_E M_LN2 max tan Int and Float Types 31 $a

$b $c $d $e = = = = = 7 / 2; # float: 3.5 (int) $a; # int: 3 round($a); # float: 4.0 "123"; # string: "123" (int) $d; # int: 123 PHP int for integers and float for reals

division between two int values can produce a float CS380 PHP exercise 1 32 For your first PHP exercise, echo the following statement to the browser: Twinkle, Twinkle little star. Next, create two variables, one for the word Twinkle and one for the word star. Echo the statement to the browser, this time substituting the variables for the relevant words. Change the value of each variable to whatever you like, and echo the statement a third CS380 time. Remember to include code to show PHP exercise 2

33 PHP includes all the standard arithmetic operators. For this PHP exercise, you will use them along with variables to print equations to the browser. In your script, create the following variables: $x=10; $y=7; Write code to print out the following: 10 10 10 10 10 + 7 = 17

-7=3 * 7 = 70 / 7 = 1.4285714285714 %7=3 Use numbers only in the above variable assignments, not in the echo statements. CS380 PHP exercise 3 34 Arithmetic-assignment operators perform an arithmetic operation on the variable at the same time as assigning a new value. For this PHP exercise, write a script to reproduce the output below. Manipulate only one variable using no simple arithmetic operators to produce the values given in the statements.

Hint: In the script each statement ends with "Value is now $variable." Value is now 8. Add 2. Value is now 10. Subtract 4. Value is now 6. Multiply by 5. Value is now 30. Divide by 3. Value is now 10. Increment value by one. Value is now 11. Decrement value by one. Value is now 10. CS380 PHP exercise 4 35 When you are writing scripts, you will often need to see exactly what is inside your variables. For this PHP exercise, think of the ways you can do that, then write a script that outputs the following, using the echo statement only for line breaks.

string(5) "Harry" Harry int(28) NULL CS380 PHP exercise 5 36 For this PHP exercise, write a script using the following variable: $around="around"; Single quotes and double quotes don't work the same way in PHP. Using single quotes (' ') and the concatenation operator, echo the following to the browser, using the variable you created:

What goes around, comes around. CS380 PHP exercise 5 37 In this PHP exercise, you will use a conditional statement to determine what gets printed to the browser. Write a script that gets the current month and prints one of the following responses, depending on whether it's August or not: It's August, so it's really hot. Not August, so at least not in the peak of the heat. Hint: the function to get the current month is 'date('F', time())' for the month's full CS380 PHP exercise 6 38

Loops are very useful in creating lists and tables. In this PHP exercise, you will use a loop to create a list of equations for squares. Using a for loop, write a script that will send to the browser a list of squares for the numbers 1-12. Use the format, "1 * 1 = 1", and be sure to include code to print each formula on a different line. CS380 PHP exercise 7 39

HTML tables involve a lot of repetitive coding - a perfect place to use for loops. You can do even more if you nest the for loops. In this PHP exercise, use two for loops, one nested inside another. Create the following multiplication table: 1 2 3 4 5 6 7 2 4 6 8

10 12 14 3 6 9 12 15 18 21 4

8 12 16 20 24 28 5 10 15 20 25 30

35 6 12 18 24 30 36 42 7 14 21

28 35 42 49 CS380

Recently Viewed Presentations

  • pages.stolaf.edu

    pages.stolaf.edu

    Environmental Studies Department, 1520 St. Olaf Avenue, Northfield, MN 55057 USA. Abstract. The Jeffers Petroglyph site is the largest and oldest collection of rock carving on the North Eastern plains. This project estimates wetlands loss around the Jeffers petroglyph in...
  • identify the major eras in Texas history, describe

    identify the major eras in Texas history, describe

    identify the individuals, issues, and events related to Mexico becoming an independent nation and its impact on Texas, including Texas involvement in the fight for independence, José Gutiérrez de Lara, the Battle of Medina, the Mexican federal Constitution of 1824,...
  • Slide 1

    Slide 1

    Prof. Stephen Worthley, MD2 Costas Tsioufis, MD3 Mathew Worthley, MD4 Ajay Sinhal, MD5 Prof. Derek Chew, MD6 Prof. Ian Meredith, MD7 Yuvaraj Malaiapan, MD8 1, 3. Hippokration General Hospital of Athens 2, 4. Royal Adelaide Hospital 5,6. Flinders Medical Centre...
  • Slide 1

    Slide 1

    Institute of Education, University of London, University of Oxford, Birkbeck, University of London, University of Nottingham) A Quality Curriculum An appropriate curriculum: Builds on what children can do. Opportunities to engage in adult directed and child initiated activities.
  • CIVIL RIGHTS - Montana Office of Public Instruction

    CIVIL RIGHTS - Montana Office of Public Instruction

    Civil Rights: Civil rights refers ... OPI School Nutrition Programs provides a public release document for sponsor use each year on the www.opi.mt.gov. Public Notification. All informational materials, including websites used to inform the public about the School Nutrition Programs...
  • Retour d&#x27;expérience : Bonnes pratiques de déploiement et ...

    Retour d'expérience : Bonnes pratiques de déploiement et ...

    100% des clients qui ont suivi les recommandations ont vu une réduction des situations critiques et une résolution plus rapides des problèmes (sur un échantillon de 169 clients, 116 ont suivi les recommandations) Réduction de 64% du temps de résolution...
  • Simplicity is Hard - LabVIEW Craftsmen

    Simplicity is Hard - LabVIEW Craftsmen

    Houari A. Sahraoui, Robert Godin, Thierry Miceli: Can Metrics Help Bridging the Gap Between the Improvement of OO Design Quality and Its Automation? Complexity Metric: Cohesion Cohesion is the degree to which the elements of a module belong together
  • Griffin_05 - Pearson

    Griffin_05 - Pearson

    The audit is usually undertaken by a task force of high-level managers from within the firm. It requires that the organization clearly define all its social goals, analyze the resources it devotes to each goal, determine how well it is...