building an e:commerce site with php

29
Server-side Technology Dave Elliman

Upload: webhostingguy

Post on 13-Dec-2014

743 views

Category:

Documents


4 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Building an e:commerce site with PHP

Server-side Technology

Dave Elliman

Page 2: Building an e:commerce site with PHP

To Implement Megagamez’s Web Site:

• We need to build shopping trolley contents in a database

• We need to keep track of our customers and orders

• We will use MySQL and PHP to do this

Page 3: Building an e:commerce site with PHP

Client-side and Server-side Scripting

• Client-side– Javascript– VBScript (MS)

• Can be used to validate input

• Can implement mouse-over effects etc

• Server-side– cgi– PHP– ASP– ASP.NET– JSP– Perl & Ruby – these

ugly sisters will not be studied in ELC

• Communicates with database

• Application logic

Page 4: Building an e:commerce site with PHP

04/10/23 PHP for MegaGamez 4

G53ELC

Server Side Processing

Page 5: Building an e:commerce site with PHP

Web Server Usage

Page 6: Building an e:commerce site with PHP

There Are Two Worlds

• Apache/Open Source

• PHP • JSP J2EE Servlets• Linux Servers• MySQL DB• ASP possible with

Chillisoft (now Sun ASP)

• Microsoft IIS• ASP, ASP.NET• Windows 2000

Server• Write in C# • MS SQL Server

2000• Develop with

Visual Studio .Net

Page 7: Building an e:commerce site with PHP

Which World Is Best?

• Open Source• Free or nearly so• Cheap hosting• Excellent

Performance• May need to spend

money to make scalable (JBOSS free)

• Development tools OK but the best ones not free

• Microsoft• Expect to spend

£10,000 on software• Hosting expensive• Excellent

Performance – more scalable

• Compelling ease of development

Page 8: Building an e:commerce site with PHP

Megagamez Has Only £70 in the Bank

• We will use for Apache/MySQL/PHP• We will design our database tables• We will design our security idea• We will make it work• First of all let’s understand PHP

Page 9: Building an e:commerce site with PHP

PHP History

• 1994: Created by Rasmis Lesdorf, a software engineer in Apache Team

• 1995: Called Personal Home Page Tool• 1997: used by 50,000 web sites• 1998: used by 100,000 websites• 1999: used by 1,000,000 websites• 2002: 30% of Web sites uses PHP

Page 10: Building an e:commerce site with PHP

04/10/23 PHP for MegaGamez 10

G53ELC

http://www.netcraft.com/Survey

Page 11: Building an e:commerce site with PHP

PHP Is

• Open-source• Easy to use but not pretty• Stable and fast• Runs on all platforms• Supports all databases • Has some useful libraries• Pre-installed in Linux distributions

Page 12: Building an e:commerce site with PHP

04/10/23 PHP for MegaGamez 12

G53ELC

How It Works

Cache of compiled pages

Page 13: Building an e:commerce site with PHP

Or in Words…

• The Client from browser sends an HTTP request (with POST/GET variables)

• Apache recognizes that a PHP script is requested and sends the request to PHP module

• The PHP interpreter executes the PHP script and returns the script output

• Apache replies to client using the PHP script output interspersed with HTML output

Page 14: Building an e:commerce site with PHP

04/10/23 PHP for MegaGamez 14

G53ELC

“Hello World” in PHP

<html><head> <title>My personal Hello World! PHP script</title></head><body><?

echo “Hello World!”;?></html>

Page 15: Building an e:commerce site with PHP

04/10/23 PHP for MegaGamez 15

G53ELC

How Do You Run It?

• See: http://www.cs.nott.ac.uk/TSG/manuals/webpages/php/

• #!/usr/local/bin/php // 1st line of script• mkdir ~/cgi-bin • chmod 711 ~/. ~/cgi-bin (sorry!)• chmod 755 *.php• URL is http://

robin.cs.nott.ac.uk/~<you>/cgi-bin/test.php

Page 16: Building an e:commerce site with PHP

04/10/23 PHP for MegaGamez 16

G53ELC

Using Variables

• Variables start with a $• The assignment operator is =• No need to declare variables• Variables type is implicit• No check on variable used before it

is assigned• Arrrrrrrrrgh… Reminiscent of BASIC

- for the cowboys? (wja says so)

Page 17: Building an e:commerce site with PHP

04/10/23 PHP for MegaGamez 17

G53ELC

An Example

• A string is a sequence of characters in quotes for example “This is a String” and ‘so is this’

• A string can be assigned to a variable:

• $Homer = “The next president”• $Homer = ‘The next president’• Both work…

Page 18: Building an e:commerce site with PHP

04/10/23 PHP for MegaGamez 18

G53ELC

Substitution in Strings

$age = 37$Homer = “Mr Simpson is $age”$Homer is Mr Simpson is 37This does not work in single quotesWhich would come out as:Mr Simpson is $age

Page 19: Building an e:commerce site with PHP

04/10/23 PHP for MegaGamez 19

G53ELC

This Is Slightly Scary to Me

$A = 1;

$B = “2”;

$C = ($A + $B); // Integer sum

$D = $A . $B; // String catenation

echo $C; // prints 3

echo $D; // prints 12

Page 20: Building an e:commerce site with PHP

04/10/23 PHP for MegaGamez 20

G53ELC

I Do Like the Explode Function – Good for csv

$sequence = “A,B,C,D,E,F,G”;

$elements = explode (“,”,$sequence);

// Now elements is an array with all substrings between “,” char

echo $elemets[0]; // output: A;

echo $elemets[1]; // output: B;

echo $elemets[2]; // output: C;

echo $elemets[3]; // output: D;

echo $elemets[4]; // output: E;

echo $elemets[5]; // output: F;

echo $elemets[6]; // output: G;

Page 21: Building an e:commerce site with PHP

04/10/23 PHP for MegaGamez 21

G53ELC

Arrays

$books = array( ”Enduring Love”,”The Child in Time”,”The Cement Garden”);

for ($i=0; $i < count($books); $i++) print ($i+1).”: $books[$i]”;

Page 22: Building an e:commerce site with PHP

04/10/23 PHP for MegaGamez 22

G53ELC

Arrays As a Key->element Mapping

• $students = array( 0=>”Mike”, 1=>”Bill”, 2=>”Fred”, 3=>”Ann”)

• echo $students[2]; Fred• Echo $students[“Fred”]; 2• In other words PHP arrays are

associative

Page 23: Building an e:commerce site with PHP

04/10/23 PHP for MegaGamez 23

G53ELC

More on Array Maps

$books = array(”Enduring Love”=>1,” The Child in Time”=> 2,” The Cement Garden”=>3);

while ($item = each( $books )) print $item[“value”].” : ”.$item[“key”];1 : Enduring Love2 : The Child in Time3 : The Cement Garden

Page 24: Building an e:commerce site with PHP

04/10/23 PHP for MegaGamez 24

G53ELC

Connecting to a Database

<?php //connect to database $conn=mysql_connect(“sauron.cs.nott.ac.uk",“usr",“pwd") or die(“could not connect to database “.mysql_error());mysql_select_db(“megagamez”) or die(“message”); 

// do something

………

//disconnect from database mysql_close($conn);

?>

Page 25: Building an e:commerce site with PHP

04/10/23 PHP for MegaGamez 25

G53ELC

Executing Database Queries

<?php $query = "SELECT name, age, phone FROM students 

WHERE (name LIKE '%Smith%')"; //execute query $result = mysql_execute($query); while($row = mysql_fetch_array($result)) {

echo($row[“name”].”, “);echo($row[“age”]);

}

?>

Page 26: Building an e:commerce site with PHP

04/10/23 PHP for MegaGamez 26

G53ELC You Will Want More Control Over the Format –

So Make a Table<table width="75%" border="1" cellspacing="1" cellpadding="1" bgcolor="#FFFFFF">   <tr bgcolor="#CCFFFF">      <td height="22"><b>Name</b></td>     <td height="22"><b>Age</b></td>     <td height="22"><b>Telephone</b></td>   </tr>

…some php stuff

</table>

Page 27: Building an e:commerce site with PHP

04/10/23 PHP for MegaGamez 27

G53ELC

What About Inserting Data?

• I cannot do better than the excellent tutorial which makes a web site of jokes:– http://www.zend.com/zend/tut/tutorial-yank.php

Page 28: Building an e:commerce site with PHP

04/10/23 PHP for MegaGamez 28

G53ELC

Learning More About PHP

• Try http://www.php.net/manual/• Books:• O’Reilly Press, Web Database

Applications with PHP and MySQL  Hugh E Williams, David Lane

Page 29: Building an e:commerce site with PHP

04/10/23 PHP for MegaGamez 29

G53ELC

Learning More About PHP