Quick Reference Study Notes for PHP Web Development (Advanced)


PHP Web Development

Introduction
PHP (PHP Hypertext Preprocessor) is a server scripting language and a powerful tool for making dynamic and interactive Web pages. PHP scripts are executed on the server. It is integrated with a number of popular databases, including MySQL, PostgreSQL, Oracle, Sybase, Informix and Microsoft SQL Server. PHP is free to download and use. Following are some commonly used terms in the world of php web development with description:

Headers

header()

used to send a raw HTTP header

headers_sent()

Checks if or where headers have been sent

setcookie()

Send a cookie

http_response_code()

Get or Set the HTTP response code

header_remove()

Remove previously set headers

 

Cookie

Cookie

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 a page with a browser, it will send the cookie too.

Create a cookie

setcookie(name, value, expire, path, domain, secure, httponly);

$_COOKIE

Global variable to retrieve cookie variables.

Delete a Cookie

To delete a cookie, use the setcookie() function with an expiration date in the past:

 

PHP Session

PHP Session

A session is a way to store information (in variables) to be used across multiple pages. Unlike a cookie, the information is not stored on the users computer

session_start()

Function to Start a PHP Session

$_SESSION

Global variable to set/retrieve session variables like $_SESSION["favcolor"] = "green";

 

PHP Magic constants

__LINE__

The current line number of the file.

__FILE__

The full path and filename of the file. If used inside an include, the name of the included file is returned.

__FUNCTION__

Returns the function name, or {closure} for anonymous functions.

__CLASS__

returns the class name as it was declared (case-sensitive)

__METHOD__

returns the name of the class alongwith the name of the function

 

HTTP - Status Codes

1xx: Informational

It means the request has been received and the process is continuing.

2xx: Success

It means the action was successfully received, understood, and accepted

3xx: Redirection

It means further action must be taken in order to complete the request.

4xx: Client Error

It means the request contains incorrect syntax or cannot be fulfilled.

5xx: Server Error

It means the server failed to fulfill an apparently valid request.

 

HTTP - Requests Methods

GET

The GET method is used to retrieve information from the given server using a given URI. Requests using GET should only retrieve data and should have no other effect on the data.

HEAD

Same as GET, but it transfers the status line and the header section only.

POST

A POST request is used to send data to the server, for example, customer information, file upload, etc. using HTML forms.

PUT

Replaces all the current representations of the target resource with the uploaded content.

DELETE

Removes all the current representations of the target resource given by URI.

CONNECT

Establishes a tunnel to the server identified by a given URI.

OPTIONS

Describe the communication options for the target resource

TRACE

Performs a message loop back test along with the path to the target resource.

 

PHP Filesystem Functions

fopen (file, mode)

Opens file or URL

readfile (file)

Outputs a file

fread (handle, len)

Binary-safe file read

fwrite (handle, str)

Binary-safe file write

clearstatcache ()

to clear the information that PHP caches about a file

copy (source, dest)

Makes a copy of the file source to dest

fclose (handle)

Closes the file pointed to by handle.

fgets (handle, len)

Gets a line from file pointer.

file (file)

Reads entire file into an array

filemtime (file)

Gets file modification time

filesize (file)

Gets file size

file_exists (file)

Checks whether a file or directory exists

 

PHP fopen() Modes

r

read

r+

Read and write, prepend

w

Write, truncate

w+

Read and write, truncate

a

Write, append

a+

Read and write, append

 

PHP Options and Information

extension_loaded ( string $extName )

Finds out whether the extension is loaded.

get_current_user

Gets the name of the owner of the current PHP script

get_defined_constants

Returns an associative array with the names of all the constants and their values

get_loaded_extensions

Returns an array with the names of all modules compiled and loaded

get_required_files

Returns an array with the names of included or required files

get_resources

Returns active resources

getmypid

Gets PHP's process ID

ini_get_all

Gets all configuration options

ini_get

Gets the value of a configuration option

ini_restore

Restores the value of a configuration option

ini_set

Sets the value of a configuration option

phpinfo

Outputs information about PHP's configuration

phpversion

Gets the current PHP version

 

Superglobals

$GLOBALS

used to access global variables from anywhere in the PHP script (also from within functions or methods).

$_SERVER

holds information about headers, paths, and script locations.

$_REQUEST

used to collect data after submitting an HTML form.

$_POST

used to collect form data after submitting an HTML form with method="post"

$_GET

used to collect form data after submitting an HTML form with method="get"

$_FILES

It can be used to upload files from a client computer/system to a server.

$_ENV

Environment variables.

$_COOKIE

To retrieve the cookie variables

$_SESSION

To retrieve the session variables

 

Superglobals ($_SERVER)

$_SERVER['PHP_SELF']

Returns the filename of the currently executing script

$_SERVER['GATEWAY_INTERFACE']

Returns the version of the Common Gateway Interface (CGI) the server is using

$_SERVER['SERVER_ADDR']

Returns the IP address of the host server

$_SERVER['SERVER_NAME']

Returns the name of the host server

$_SERVER['SERVER_SOFTWARE']

Returns the server identification string

$_SERVER['SERVER_PROTOCOL']

Returns the name and revision of the information protocol

$_SERVER['REQUEST_METHOD']

Returns the request method used to access the page (such as POST)

$_SERVER['REQUEST_TIME']

Returns the timestamp of the start of the request

$_SERVER['QUERY_STRING']

Returns the query string if the page is accessed via a query string

$_SERVER['HTTP_ACCEPT']

Returns the Accept header from the current request

$_SERVER['HTTP_ACCEPT_CHARSET']

Returns the Accept_Charset header from the current request (such as utf-8,ISO-8859-1)

$_SERVER['HTTP_HOST']

Returns the Host header from the current request

$_SERVER['HTTP_REFERER']

Returns the complete URL of the page from which the current page was called

$_SERVER['HTTPS']

Is the script queried through a secure HTTP protocol

$_SERVER['REMOTE_ADDR']

Returns the IP address from where the user is viewing the current page

$_SERVER['REMOTE_HOST']

Returns the Host name from where the user is viewing the current page

$_SERVER['REMOTE_PORT']

Returns the port being used on the user's machine to communicate with the web server

$_SERVER['SCRIPT_FILENAME']

Returns the absolute pathname of the currently executing script

$_SERVER['SERVER_ADMIN']

Returns the value given to the SERVER_ADMIN directive in the web server configuration file

$_SERVER['SERVER_PORT']

Returns the port on the server machine being used by the web server for communication

$_SERVER['SERVER_SIGNATURE']

Returns the server version and virtual host name which are added to server-generated pages

$_SERVER['PATH_TRANSLATED']

Returns the file system based path to the current script

$_SERVER['SCRIPT_NAME']

Returns the path of the current script

$_SERVER['SCRIPT_URI']

Returns the URI of the current page


*NOTE : "This study material is collected from multiple sources to make a quick refresh course available to students."

This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Cookie Policy. More info. I Agree