PHP Tutorial - Anas Jaghoub Chapter 2 Control Structures.

Slides:



Advertisements
Similar presentations
LIS651 lecture 3 taming PHP Thomas Krichel
Advertisements

LIS651 lecture 3 functions & sessions Thomas Krichel
UFCE8V-20-3 Information Systems Development 3 (SHAPE HK)
Cookies, Sessions. Server Side Includes You can insert the content of one file into another file before the server executes it, with the require() function.
PHP (2) – Functions, Arrays, Databases, and sessions.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
Intermediate PHP & MySQL
Using Session Control in PHP tMyn1 Using Session Control in PHP HTTP is a stateless protocol, which means that the protocol has no built-in way of maintaining.
CSE 154 LECTURE 13: SESSIONS. Expiration / persistent cookies setcookie("name", "value", expiration); PHP $expireTime = time() + 60*60*24*7; # 1 week.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
Advanced Web 2012 Lecture 4 Sean Costain PHP Sean Costain 2012 What is PHP? PHP is a widely-used general-purpose scripting language that is especially.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Cookies & Sessions.
CHAPTER 12 COOKIES AND SESSIONS. INTRO HTTP is a stateless technology Each page rendered by a browser is unrelated to other pages – even if they are from.
by Chris Brown under Prof. Susan Rodger Duke University June 2012
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
WEEK 3 AND 4 USING CLIENT-SIDE SCRIPTS TO ENHANCE WEB APPLICATIONS.
Week 9 PHP Cookies and Session Introduction to JavaScript.
CSE 154 LECTURE 12: COOKIES. Including files: include include("filename"); PHP include("header.html"); include("shared-code.php"); PHP inserts the entire.
Web Programming Language Week 7 Dr. Ken Cosh Security, Sessions & Cookies.
PHP1-1 PHP Lecture 2 Xingquan (Hill) Zhu
Lecture 8 – Cookies & Sessions SFDV3011 – Advanced Web Development 1.
Chapter 6: Authentications. Training Course, CS, NCTU 2 Overview  Getting Username and Password  Verifying Username and Password  Keeping The Verification.
1 Chapter 9 – Cookies, Sessions, FTP, and More spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science.
PHP2. PHP Form Handling The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input. Name: Age:
Cookies & Session Web Technology
11 1 Cookies CGI/Perl Programming By Diane Zak Objectives In this chapter, you will: Learn the difference between temporary and persistent cookies.
PHP. $_GET / $_POST / $_SESSION PHP uses predefined variables to provide access to important information about the server and requests from a browser.
COOKIES and SESSIONS. COOKIES A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each.
Cookies and Sessions IDIA 618 Fall 2014 Bridget M. Blodgett.
ASP. What is ASP? ASP stands for Active Server Pages ASP is a Microsoft Technology ASP is a program that runs inside IIS IIS stands for Internet Information.
ECMM6018 Enterprise Networking for Electronic Commerce Tutorial 7
Database Access Control IST2101. Why Implementing User Authentication? Remove a lot of redundancies in duplicate inputs of database information – Your.
PHP Session ISYS 475. Session The web server starts a session when a visitor visiting your web site and assigns a unique id, the session id for the session.
Sessions Brendan Knight A visitor accessing your web site is assigned a unique id. This id links to specific data that remains on the server. Sessions.
SESSIONS 27/2/12 Lecture 8. ? Operator Similar to the if statement but returns a value derived from one of two expressions by a colon. Syntax: (expression)
Portaportal Portaportal is a web based bookmarking utility that lets you store links to your favorite websites online. Now your bookmarks are no longer.
PHP and Sessions. Session – a general definition The GENERAL definition of a session in the “COMPUTER WORLD” is: The interactions (requests and responses)
8 th Semester, Batch 2008 Department of Computer Science SSUET.
 A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests.
1 DIG 3134 Lecture 6: Maintaining State Michael Moshell University of Central Florida Media Software Design.
8 th Semester, Batch 2009 Department Of Computer Science SSUET.
ITM © Port,Kazman 1 ITM 352 Cookies. ITM © Port,Kazman 2 Problem… r How do you identify a particular user when they visit your site (or any.
1 PHP HTTP After this lecture, you should be able to know: How to create and process web forms with HTML and PHP. How to create and process web forms with.
Web Page Designing With Dreamweaver MX\Session 1\1 of 9 Session 3 PHP Advanced.
Cookies and Sessions in PHP. Arguments for the setcookie() Function There are several arguments you can use i.e. setcookie(‘name’, ‘value’, expiration,
Unit-6 Handling Sessions and Cookies. Concept of Session Session values are store in server side not in user’s machine. A session is available as long.
Sessions and cookies (part 2) MIS 3501, Fall 2015 Brad N Greenwood, PhD Department of MIS Fox School of Business Temple University 11/19/2015.
Session 11: Cookies, Sessions ans Security iNET Academy Open Source Web Development.
CGS 3066: Web Programming and Design Spring 2016 PHP.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
CHAPTER 5 SERVER SIDE SCRIPTING
Unit 7 Learning Objectives
CHAPTER 10 JAVA SCRIPT.
19.10 Using Cookies A cookie is a piece of information that’s stored by a server in a text file on a client’s computer to maintain information about.
ITM 352 Cookies.
Web Programming Language
Open Source Programming
Cookies BIS1523 – Lecture 23.
What is Cookie? Cookie is small information stored in text file on user’s hard drive by web server. This information is later used by web browser to retrieve.
<?php require("header.htm"); ?>
Cookies Cookie :- A cookie is often used to identify a user. A cookie is often used to identify a user. A cookie is a small file that the server embeds.
Building Web Applications
PHP.
CSE 154 Lecture 21: Sessions.
Web Programming Language
Cookies and Sessions.
Web Programming Language
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Presentation transcript:

PHP Tutorial - Anas Jaghoub Chapter 2 Control Structures

PHP Tutorial - Anas Jaghoub Cookies and Sessions Before starting control structures in php, am going to illustrate two important concepts in web applications area, Cookies and Sessions. What is a Cookie? a cookie is a file embedded from the server to the user's computer. Its purpose is identify a user.

PHP Tutorial - Anas Jaghoub Cookies and Sessions When you deal with web applications, actually you don’t know who the user that uses your application is. Unlike traditional or real-life applications, For example, if you work in a bank, and a user asks you to exchange him money from his bank account, you have some remarks that identifies these person, such as: his name, identification number, his picture, his signature, and more details that distinguishes him from others.

PHP Tutorial - Anas Jaghoub Cookies and Sessions If we want to apply this job online, we don’t know the user who deals with the application. So we make some procedures that help us distinguish one user from another. First thing is asking the user for a username, and password. Second thing is checking for compatibility with registered data in our database. Thirdly, if it is compatible then we’ll create a cookie that distinguishes the user from a guest. In some cases the user has privileges more than a guest. User could access data, where guest is not allowed. Otherwise the cookie will not be created, for one of two reasons, first one is the cookie file has already been set in the past, and its expiration date was not reached, or given username and password from the user are not compatible.

PHP Tutorial - Anas Jaghoub Cookies and Sessions How to create a cookie? To create a cookie in php we use a built-in function called setcookie that has three parameters. First parameter is cookie name, second one is cookie value, and last one is expiration time setcookie(“name”,value,expiration).

PHP Tutorial - Anas Jaghoub Cookies and Sessions Example: <?php $expiration = time() + 60 *60 * 1; // this cookie will expire after an hour. $value = “anas”; $name= “user”; setcookie(“$name”, “$value”, $expiration); /* this will set a cookie called user, its value anas, and will expire after an hour from executing this script */ ?>

PHP Tutorial - Anas Jaghoub Cookies and Sessions Notes: function time() returns value of current time as a Unix timestamp. $expiration value is the time when the cookie will be expired, in our example we need the cookie to expire after an hour from the execution, time() + 60*60*1 we use this format because function time deals with seconds, which means 60 seconds multiplied by 60 minutes gives an hour, we multiplied it with 1 to determine one hour. we could multiply it with any number of hours. $value is the value of the cookie we want to set. $name is the name of the cookie we want to set.

PHP Tutorial - Anas Jaghoub Cookies and Sessions How to retrieve a cookie? We might retrieve a cookie using an array called $_COOKIE, we’ll discuss arrays in more details in chapter 4. But, now we’ll use it by only telling you that this is an array, and how to use it in your script. To retrieve a cookie that has already been set do as followed:

PHP Tutorial - Anas Jaghoub Cookies and Sessions <?php echo $_COOKIE[“user”]; // will print cookie user value ?> Notes: We use $_COOKIE[“user”] to get value of a cookie called user, in our example we used $_COOKIE to print its value in the web page using echo.

PHP Tutorial - Anas Jaghoub Cookies and Sessions How to delete a cookie? In some cases we might want to delete a cookie before the expiration time has been reached. For example the user clicked on log out link in your page. When a user asks you to log him out from the page, it means that the user wants to delete information he entered, in another words he wants to delete the cookie file, but in fact, user doesn’t know what is a cookie, or in the best case he has minimum knowledge about cookies. So it is your responsible as a programmer to delete these cookies you had set before, and prevent your user information from being accessed by unauthorized users. To delete a cookie we use the same method to set a cookie, which means that we’ll use setcookie function again, but with expiration date in the past, for example $expiration = time() - 60*60; which means any time in the past. setcookie(“$name”, ”$value”, $expiration);

PHP Tutorial - Anas Jaghoub Cookies and Sessions What is a session? Php session is a variable used to store information about single user. Sessions are available to all pages in one application. Sessions are stored on the server for later use, unlike cookies that are stored on user computer. Session information is temporarily and will be deleted after the user has left the website.

PHP Tutorial - Anas Jaghoub Cookies and Sessions How do sessions work? Sessions work by creating a unique id (UID) for each visitor, and store variables based on this UID. The UID is either stored in a cookie or is propagated with the URL.

PHP Tutorial - Anas Jaghoub Cookies and Sessions How to start a session? To start a session simply call function session_start(); which will start your session. This function call must be before the tag.

PHP Tutorial - Anas Jaghoub Cookies and Sessions How to store a session variable? As we said before sessions have many advantages such as, it is being stored on the server, deleted after user navigates from your site, has UID, and more. One of the applications that you might use session in it, is getting online users and guests.

PHP Tutorial - Anas Jaghoub Cookies and Sessions To store a session we use built-in array called $_SESSION[]; Below an example for starting a session called views, which tells the visitor of your application, how many pages he had visited, we’ll now only put the structures for this application, and it will be improved and developed after taking a deep look at control structures.

PHP Tutorial - Anas Jaghoub Cookies and Sessions <?php // Page views // programmer Anas Jaghoub // created at 11/oct/2009 session_start(); $_SESSION[‘views’] = 1; // this starts a session called views // and stored temporarily at server. ?> You have visited pages at this visit to my site

PHP Tutorial - Anas Jaghoub Cookies and Sessions How to destroy a session? In order to destroy a session in php you could use one of these two built-in functions, unset(session name) or session_destroy(); Each function has advantages and times to use, depending on its properties, for example unset($_SESSION[‘views’]); this function call will reset $_SESSION[‘views’] and return it to the beginning status which is 1. Where calling session_destroy(); will completely delete your session and you will loose all your stored data.

PHP Tutorial - Anas Jaghoub Control Structures Control structures in php are same as many of programming languages, its syntax is most similar to c/c++ and perl. Nevertheless, PHP has one special control structure, distinguishes it from other programming languages.

PHP Tutorial - Anas Jaghoub Control Structures If Statement: if(condition) statement; If … else Statement if(condition) statement; else statement; If … elseif … else statement if(condition) statement; elseif(condition) statement; else statement;

PHP Tutorial - Anas Jaghoub Control Structures If Statement used to execute some statements if condition became true; for example if($x < 3) echo “Try again”; At this example if $x has value less than 3 then the condition is true, so Try again will be echo-ed on the web page. Otherwise it will not.

PHP Tutorial - Anas Jaghoub Control Structures If… else Statement, used to execute some action if condition became true. Or do something else if condition is false; for example if($x <=3) echo “Try Again”; else echo “You don’t have permission”; At this example if $x has value less than or equal to 3 then Try Again will be echo-ed. else You don’t have permission will be echo-ed. As a result one of the two blocks will be executed depending on the condition.

PHP Tutorial - Anas Jaghoub Control Structures If … elseif … else Statement used to execute some statements if condition is meeting some rules, if not you might do some actions to deal with another condition, or do general case. Example: <?php $x = 1; If($x <3) echo { “Try again”; $x++; } elseif($x == 3) echo { “Last chance”; $x++; } else echo “You don’t have permissions”; ?>

PHP Tutorial - Anas Jaghoub Control Structures Notes: In the previous script, suppose that $x has a value of two then first block will be executed, which means Try again will be echo-ed, and $x will be incremented by 1. If $x has a value of 3 then second block will be executed. Or last block will be executed if $x greater than 3.

PHP Tutorial - Anas Jaghoub Control Structures Switch Statement: Used to execute some statements depending on one case or more. Syntax: switch(x) { case label1: executed when x == label 1; break; case label2: executed when x==label2; break; default: executed when x did not match any label; }

PHP Tutorial - Anas Jaghoub Control Structures Notes: Switch statement sometimes called multiple selection statement, that’s because you might execute multiple blocks depending on your case. Unlike if statement. Domain of switch statement is first appearance of break keyword. Which means, your code will be executed until first break stops it. Default case does not need break label, because, often it is the last thing to be executed whether no labels matched your selection.

PHP Tutorial - Anas Jaghoub Control Structures Enhancement for Page views Script <?php session_start(); if(isset($_SESSION[‘views’])) $_SESSION[‘views’]++; else { $_SESSION[‘views’] = 1; } ?> you have visited this page times

PHP Tutorial - Anas Jaghoub Control Structures Notes: At previous script, we called a new built-in function called isset(). This function returns true or false depending on the given parameter. Its job is determining the given parameter has value or not. If given parameter has value and is not null, then it will return true; else it will return false. In our application we started a session by calling session_start(); this function call depends not on session value. It is only sets the server to store a session. Next, we checked for a session called views, using function isset(). This function will tell us that session views has been set before or not.

PHP Tutorial - Anas Jaghoub Control Structures Depending on the returned value of function isset, we’ll take our action, or in other words, do the purpose of the script which is telling the visitor how many pages he had visited from the time he opened the site. As we explained before, sessions are available to all pages in one application. Which means that any page you want to track user hits on it simply include this script to it. We’ll illustrate how to include scripts in your script, next.

PHP Tutorial - Anas Jaghoub Control Structure Suppose that function isset returned true, this will mean that, there is an exist session called views, this means that it is not the first page user had viewed at this visit. So the suitable action to be performed is incrementing number of views by one, $_SESSION[‘views’]++; If isset returned false, this means that session views is not exist, so we had to initialize it. By starting the counter from one. $_SESSION[‘views’] = 1; Remember that sessions has UID, and each visitor has his own session. Last thing is printing value of session views.

PHP Tutorial - Anas Jaghoub Server Side Includes (SSI) In order to organize your work, and be a professional programmer, simplify your code. There are many things you could do, to organize your code, and facilitate development acceleration to your application. Here I’ll put some advices to you as a programmer, that might help you in your job. First thing, simplify, and simplify… Make it easy. Your script should do only one job. this will facilitate handling errors. Second thing, it is not preferred that your function has more than three parameters to finish its job, this means that you can simplify it more. Use Loops only when needed. Don’t repeat your code. Divide your script in directories, and group codes that complete each other in the same directory. Use available (OPEN SOURCE Libraries) rather than build it yourself. This will save your time, and give your application more qualities. Remember “ Don’t re-invent the wheels”. Finally, think of simplest and shortest ways to do your mission. Of course it should be legal methods.

PHP Tutorial - Anas Jaghoub Server Side Includes (SSI) Suppose that in your application, there is a menu, that you need to be showed in more than one page. of course, you have not to copy and paste the menu in every page. What about if you want to add a new item to your menu? Shall you add to all pages copying and pasting!! It is not a programming idea. The best, and logical thing is put your menu in a file, and call this file where needed.

PHP Tutorial - Anas Jaghoub Server Side Includes (SSI) How to accomplish this? Save your menu in a file. For example “menu.php”. At each page you need your menu to appear, call the file by using one of these two functions include() or require(). Both functions are identical in their job, but, they differs in handling errors. include function will search for your file, if found it then will be included in your page, and so require. But if not found. If you used include, your script will continue working, and will not stop. Where require will show an error and your script completely will not work.

PHP Tutorial - Anas Jaghoub Server Side Includes (SSI) Example <?php require(“menu.php”); include(“wrong.php”); ?> If menu is found, the script will continue working, but wrong file will not stop working your script. Where to use? If your script depends completely on the included file, then use require. else use include. In my opinion, I prefer require.

PHP Tutorial - Anas Jaghoub Control Structures End Of Chapter 2