0% found this document useful (0 votes)
12 views31 pages

Week 7

The document discusses PHP functions and superglobals. It defines what functions are in PHP and provides examples of built-in and user-defined functions. It also explains key PHP superglobals like $_SERVER, $_GET, $_POST, $_COOKIE and $_SESSION and how they can be used.

Uploaded by

santhirans770
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views31 pages

Week 7

The document discusses PHP functions and superglobals. It defines what functions are in PHP and provides examples of built-in and user-defined functions. It also explains key PHP superglobals like $_SERVER, $_GET, $_POST, $_COOKIE and $_SESSION and how they can be used.

Uploaded by

santhirans770
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

HNDIT3022

Web Programming
Week7:-PHP functions & Superglobals
PHP functions
Introduction
• A function is a set of statements that perform a particular function
and optionally returns a value.

• You can pull out a section of code that you have used more than once,
place it into a function, and call the function by name when you want
the code.
PHP functions
• PHP comes with hundreds of ready-made, built-in functions.

• To use a function, call it by name.

• For example, you can see the print function in action here:
print("print is a pseudo-function");

• The parentheses tell PHP that you’re referring to a function.

• Besides the built-in PHP functions, it is possible to create your own


functions.(User defined fuctions)
PHP functions[2]
• The general syntax for a function is:
• A definition starts with the word function.
function function_name([parameter [, ...]])
• A name follows, which must start with a letter
{ or underscore, followed by any number of
// Statements letters, numbers, or underscores.
} • The parentheses are required.
• One or more parameters, separated by
commas, are optional
Activity 7.1
• Write down a PHP function to print “Hello world”
Activity 7.2
• Write down a PHP function to print the sum of two numbers when
two numbers are passed as parameters.
PHP function[3]
• Returning values
function function_name([parameter [, ...]])
{ • A definition starts with the word function.
// Statements • A name follows, which must start with a letter
or underscore, followed by any number of
return value; letters, numbers, or underscores.
} • The parentheses are required.
• One or more parameters, separated by
commas, are optional
• Return keyword with the return value
Activity 7.3
• Write down a PHP function to return the sum of two numbers when
two numbers are passed as parameters.
Superglobals
Introduction
• Superglobals were introduced in PHP 4.1.0, and are built-in variables that
are always available in all scopes.
• The PHP superglobal variables are:
• $GLOBALS
• $_SERVER
• $_REQUEST
• $_POST
• $_GET
• $_FILES
• $_ENV
• $_COOKIE
• $_SESSION
$_SERVER
• $_SERVER is a PHP super global variable which holds information
about headers, paths, and script locations.
• Example
• $_SERVER['PHP_SELF'] Returns the filename of the currently executing
script
• $_SERVER['SERVER_NAME'] Returns the name of the host server.
• $_SERVER['REQUEST_METHOD'] Returns the request method used to access
the page (such as POST)
• $_SERVER['SERVER_PORT'] Returns the port on the server machine being
used by the web server for communication (such as 80)
Activity 7.4
$_GET
• $_GET is used to collect form data after submitting an HTML form
with method="get".

• $_GET can also collect data sent in the URL.


Activity 7.5
• Try out the following code segment.
Cont..
• Information sent from a form with the GET method is visible to
everyone (all variable names and values are displayed in the URL).

• GET also has limits on the amount of information to send. The limitation
is about 2000 characters.

• However, because the variables are displayed in the URL, it is possible to


bookmark the page. This can be useful in some cases.

• GET may be used for sending non-sensitive data.


$_POST
• $_POST is used to collect form data after submitting an HTML form
with method="post“.

• Example
Cont..
• Information sent from a form with the POST method is invisible to others
(all names/values are embedded within the body of the HTTP request)
and has no limits on the amount of information to send.

• POST supports advanced functionality such as support for multi-part


binary input while uploading files to the server.

• However, because the variables are not displayed in the URL, it is not
possible to bookmark the page.

• Developers prefer POST for sending form data.


Activity 7.6
• Create a login form using HTML.
• Write a PHP code segment to capture the user-entered username &
password.
Cookies & Session
Cookies
• A cookie is an item of data that a web server saves to your computer’s
hard disk via a web browser.

• It can contain almost any alphanumeric information (as long as it’s under
4 KB)

• Can be retrieved from your computer and returned to the server.

• Because of their privacy implications, cookies can be read only from the
issuing domain.
Cookies[2]
• Common uses include
• session tracking
• maintaining data across multiple visits
• holding shopping cart contents
• storing login details
Cookies[3]
• You can call the setcookie function
setcookie(name, value, expire, path, domain, secure, httponly);
Parameter Description Example

name The name of the cookie. This is the name that your server will use to access the cookie on subsequent browser username
requests.

value The value of the cookie, or the cookie’s contents. This can contain up to 4 KB of alphanumeric text. Tom

expire (Optional.) Unix timestamp of the expiration date. Generally, you will probably use time() plus a number of seconds. If time() + 2592000
not set, the cookie expires when the browser closes.
path (Optional.) The path of the cookie on the server. If this is a / (forward slash), the cookie is available over the entire /
domain, such as www.webserver.com. If it is a subdirectory, the cookie is available only within that subdirectory. The
default is the current directory that the cookie is being set in, and this is the setting you will normally use.
domain (Optional.) The Internet domain of the cookie. If this is .webserver.com, the cookie is available to all of webserver.com .webserver.com
and its subdomains, such as www.webserver.com and images.webserver.com. If it is images.webserver.com, the cookie
is available only to images.webserver.com and its subdomains such as sub.images.webserver.com, but not, say, to
www.webserver.com.
secure (Optional). Specifies whether or not the cookie should only be transmitted over a secure HTTPS connection. TRUE
indicates that the cookie will only be set if a secure connection exists. Default is FALSE
httponly (Optional.) If set to TRUE the cookie will be accessible only through the HTTP protocol (the cookie will not be
accessible by scripting languages). This setting can help to reduce identity theft through XSS attacks. Default is FALSE
Setting up a cookie

• Run the above code


• Now check for cookies in your browser settings.
$_COOKIE
• Reading the value of a cookie is as simple as accessing the $_COOKIE
(superglobal) system array.
Sessions
• Because your program can’t tell what variables were set in other programs or
even what values the same program set the previous time it ran.

• You’ll sometimes want to track what your users are doing from one web page to
another.

• PHP provides a much more powerful and simpler solution in the form of sessions.

• Session variables solve this problem by storing user information to be used


across multiple pages (e.g. username, favorite color, etc). By default, session
variables last until the user closes the browser.
Sessions[2]
• A session is started with the session_start() function.

• Session variables are set with the PHP global variable: $_SESSION.

• To remove all global session variables and destroy the session, use
session_unset() and session_destroy():
Example
Activity 7.7
• Use the login form created in Activity 7.6.
• Set the login credentials(Username) into a session variable.
• Check the session value set in the above in another page.
Questions ?
Thank You !

You might also like