Week 7
Week 7
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.
• For example, you can see the print function in action here:
print("print is a pseudo-function");
• GET also has limits on the amount of information to send. The limitation
is about 2000 characters.
• 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.
• However, because the variables are not displayed in the URL, it is not
possible to bookmark the page.
• It can contain almost any alphanumeric information (as long as it’s under
4 KB)
• 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
• 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 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 !