Presentation is loading. Please wait.

Presentation is loading. Please wait.

Building Web Applications

Similar presentations


Presentation on theme: "Building Web Applications"— Presentation transcript:

1 Building Web Applications
BIS1523 – Lecture 18

2 Web Applications Often, a web site isn’t a single page, or even just 2 pages. A lot of our applications or web sites are made up of numerous pages. For example: Notice this web site has a number of different links, all with a similar look. They have the same header, and navigation options at the top. When we select an option, the URL changes. These are all different html (or php) files.

3 Web Applications When we have sections of web pages that are all repeated, it is often times better to have that HTML or PHP code in a single location. Imagine if each of those 5 pages had their own copy of the header and nav options. If you wanted to change the look of those, you’d have to edit them in 5 locations. Now imagine how that scales, if you have 20 different pages, or 50. Any time you have to do multiple edits, the chance for errors goes up, and the time to make the change goes up drastically. A better system is to create something called a “template”, store the HTML in one file, and then reference that file.

4 Templates We can use the “include” PHP command to reference other files in our pages. In each of these three pages, we can put the command: This will add the contents of header.html into the web page as it is loading. Each time the page loads, it goes back and gets header.html

5 Templates This means changes header.html can be done in a single location, and those changes will be updated when the other pages load. Included files may contain HTML and/or PHP. Notice that the include command is PHP, so the including file needs to have a .PHP extension

6 Example Index.php: Results: Example.html:

7 Functions in Includes Another useful use for inclusion is code you want to re-use on multiple projects. For example, you write a complex function to format some output, or do a calculation, and it can be used in several different pages. You could put it in its own file, and then just use include commands in the pages that need to access the function. Much like HTML updates this makes updates to the functions easier to accomplish as well.

8 Sessions Sessions allow you to store data between several web pages.
Normal variables only exist while the PHP script is running, they don’t exist out of their local area. With a Session variable, you could have values stored that would be accessible from all of your web pages. Session variables are stored on the server.

9 Session Start In order to use session variables, you have to perform the command session_start() This command must be the very first thing in every PHP file that wants to use these variables.

10 Session Variables Once you have started a session, you have access to the $_SESSION array. You can set variables using the command: You can access those variables just as you would any other variable:

11 Ending a Session You can end a session with the command
session_destroy(); This stops the session, removing all the data stored on the server. Most servers are configured to automatically do this after a certain period of time. Note: The course web site uses sessions in several ways, so executing this command inside one of your PHP pages will effectively log you out of the course web site.

12 Example Let’s assume we have a web site with a few pages, and we’d like to track how often people visit the page, and what pages they visit. If a user visits several pages in a row, we’d like to know which pages she visits. If they leave and come back later, we’ll start a different log. We will use 2 different pieces of data to tell when a user visits our page: A file that stores a single number. That is the number of the “last user who visited the page” A session variable, that will remember what visitor number is currently assigned to this session.

13 Example So, when a user visits our page, we will:
Check to see if they are already assigned a visitor number by reading in the session variable. If it is blank, then we Read the file to assign them a number. Increment the number in the file for the next person Set the session variable for other pages Set a normal variable for this page.

14 Example If a user has visited this page recently, the session variable $_SESSION[‘visitor’] will have already been set, so we just read it in. If they haven’t, we get the next number from the file

15 Example Once the PHP variable $visitor is set, it can be accessed in HTML anywhere on the page

16 Example If we include that same script to read the session variable on other pages, they will all track that visitor number. So user goes to session1.php and the session variable gets set. Then, when they visit session2.php, that variable is still set, and we can use it. We could also then use that variable to track page visits. Lets say we’d like to create a unique log file for each user. We’ll name it “log”, and then add their visitor number. So visitor 18 will have a file “log.18”.

17 Example To create the filename, we make use of PHP’s ability to do variable substitution, and put the value of $visitor into the name: So if $visitor has the value “18”, the variable $filename will have the value “log.18”. Then, we write information to that file:

18 Example We use FILE_APPEND so the new information is just added to the end of the file. We concatenate the name of the page with PHP_EOL, so when we look at the file each name is on its own line. The resulting log file might contain information like:

19 Example We could include this header on all of our web pages.
The only thing different on each page would be the name that we are logging. If this is going to be repeated, we should instead, use an include.

20 Example To use an include, first, we create a new file, with just the header information we want in it. File: visit.include.php: Then, in each of our web pages, we include it with the command:

21 Example So now, each of our web pages looks like:


Download ppt "Building Web Applications"

Similar presentations


Ads by Google