You are on page 1of 13

$_SERVER is an array containing information

such as headers, paths and script locations. So


it will depend on web servers configuration.
e.g.
PHP_SELF
SERVER_ADDR
SERVER_NAME
QUERY_STRING
REQUEST_METHOD
REMOTE_ADDR
SCRIPT_NAME
CONTENT_TYPE
CONTENT_LENGTH

$_ENV : It contains variable from environment


under which PHP parser is running.
e.g. PATH

Cookies
PHP transparently supports HTTP
cookies. Cookies are a mechanism for
storing data in the remote browser. You
can set cookies using the setcookie()
function.
$_COOKIE auto-global array will always
be set with any cookies sent from the
client.

bool setcookie (name , [value], [expire],


[path], [domain], [secure] )
Cookies are part of the HTTP header, so setcookie()
must be called before any output is sent to the browser.
If output exists prior to calling this function, setcookie()
will fail and return FALSE. If setcookie() successfully
runs, it will return TRUE.
Expire time is time() function plus the number of
seconds before you want it to expire. Or you can use
mktime().
time()+60*60*24*30 will set the cookie to expire in 30
days. If not set, the cookie will expire when the user
closes browser window.

References
References in PHP are a means to access the same
variable content by different names. They are not like C
pointers.
PHP references allow you to make two variables to refer
to the same content. Meaning, when you do:
$a =& $b ;
Pass by reference:
function add( &$var)
{
$var++;
}
$a=5;
add($a);

Files
$fp=fopen(filepath, mode);
Mode

Description

Open for reading only; place the file pointer at the beginning of
the file.

r+

Open for reading and writing; place the file pointer at the
beginning of the file.

Open for writing only; place the file pointer at the beginning of
the file and truncate the file to zero length. If the file does not
exist, attempt to create it.

w+

Open for reading and writing; place the file pointer at the
beginning of the file and truncate the file to zero length. If the
file does not exist, attempt to create it.

Open for writing only; place the file pointer at the end of the
file. If the file does not exist, attempt to create it.

a+

Open for reading and writing; place the file pointer at the end
of the file. If the file does not exist, attempt to create it.

fread(file_handle,number_of_bytes)
Reads up to length bytes from the file
pointer referenced by handle and return that
content into string.
$file=file1.php;
$fp=fopen($file,r);
$str=fread($fp,20);
fwrite (filehandle, string)
Writes the contents of string to the file
stream pointed to by handle.
fclose (filehandle)

is_file(filename)
is_link(filename)
is_readable(filename)
is_writable(filename)
is_dir()
unlink()
realpath(relative_path)
mkdir(dirpath)
filesize()
file_exists()
copy(source,dest)
fpassthru(file_handle)

header
In a network transmission, a header is part of the data
packet and contains transparent information about the
file or the transmission.
Headers can be separated in 2 main types:
1) request header
2) response header
Request header is sent by client browser to web-server
when client browser makes request for any web-page
Response header is sent from web-server to clientbrowser when it serves the file requested by the client.
header function in php sends response header to client.
Http response header has so many fields through which
you can control output of the response page. In php,
header function allows you to set these fields.

Location For page redirection


<?php
header("Location: message.php");
exit();

?>

Uploading Files

Managing file uploads via PHP is the result of cooperation between various
configuration directives and the $_FILES superglobal array.
Directives in php.ini
file_uploads (boolean)
It determines whether PHP scripts on the server can accept file uploads.
max_execution_time (integer)
Default value: 30
It directive determines the maximum amount of time, in seconds, that a PHP
script will execute before registering a fatal error.
upload_max_filesize (integer)
The upload_max_filesize directive determines the maximum size, in
megabytes, of an uploaded file. Default is 2MB.
upload_tmp_dir (string)
Before subsequent processing on the uploaded file can begin, a staging area
of sorts must be designated for such files as the location where they can be
temporarily placed until moved to their final location. This location is specified
using the this directive.

Uploaded files information is stored in $_FILES array which is two


dimentional array.

1) $_FILES['userfile']['name']
The $_FILES['userfile']['name'] variable specifies the original name of the file,
including the extension, as declared on the client machine. Therefore, if you
browse to a file named vacation.jpg, and upload it via the form, this variable will
be assigned the value vacation.jpg.
2 $_FILES['userfile']['size']
The $_FILES['userfile']['size'] variable specifies the size, in bytes, of the file
uploaded from the client machine. Therefore, in the case of the vacation.jpg
file, this variable could plausibly be assigned a value like 5253, or roughly 5
kilobytes.
3) $_FILES['userfile']['tmp_name']
The $_FILES['userfile']['tmp_name'] variable specifies the temporary name
assigned to the file once it has been uploaded to the server. This is the name of
the file assigned to it while stored in the temporary directory (specified by the
PHP directive upload_tmp_dir).

4) $_FILES['userfile']['type']
The $_FILES['userfile']['type'] variable specifies the mime-type of the
file uploaded from the client machine. Therefore, in the case of the
vacation.jpg file, this variable would be assigned the value image/jpeg.
If a PDF were uploaded, then the value application/pdf would be
assigned.
5) $_FILES['userfile']['error']
The $_FILES['userfile']['error'] array value offers important information
pertinent to the outcome of the upload attempt. In total, five return
values are possible, one signifying a successful outcome, and four
others denoting specific errors which arise from the attempt.
is_uploaded_file(file)
<?php
if (is_uploaded_file($_FILES['classnotes']['tmp_name'])) {
copy($_FILES['classnotes']['tmp_name'], $_FILES['classnotes']['name']);
}
?>

move_uploaded_file(uploaded_file,dest
ionation)
This function checks to ensure that the file
designated by filename is a valid upload file
(meaning that it was uploaded via PHP's
HTTP POST upload mechanism). If the file
is valid, it will be moved to the filename
given by destination.