2. PHP GET and POST Methods
Before the browser sends the information, it encodes using a
scheme called URL encoding.
There are two ways the browser client can send information to the
web server.
The GET Method
The POST Method
The PHP $_GET and $_POST variables are
used to retrieve information from forms, like user input.
3. The GET Method
The GET method sends the encoded user information
appended to the page request. The page and the encoded
information are separated by the ? character.
http://www.test.com/index.htm?name1=value1&name2=v
alue2
The GET method is restricted to send upto 1024 characters
only.
Never use GET method if you have password or other
sensitive information to be sent to the server.
GET can't be used to send binary data, like images or word
documents, to the server.
The information is encoded as described in case of GET
method and put into a header called QUERY_STRING.
4. The POST Method
The POST method transfers information via HTTP headers.
The POST method does not have any restriction on data size to
be sent.
The POST method can be used to send ASCII as well as binary
data
The data sent by POST method goes through HTTP header so
security depends on HTTP protocol. By using Secure HTTP you can
make sure that your information is secure.
5. Differences
The main difference between GET and POST is how the form data is
passing. Both are used for passing form field values.
All the values which is submitted by the GET method will be appended to
the URL.
Where as POST method send the data with out appending the URL(hidden)
In GET Method we can bookmark the URLs where as in POST method its
not possible
In GET Method there is a limit for passing the data from one page to
another(ie 256 characters according to w3c standards)
But in POST we can send large amount of data
Compared to POST get is Faster
POST is more secure than get method
All the informations which is passed by GET method will be stored in
webserver(log file) but in POST we can not
6. Validating Form data
isset() determines whether a certain variable has already
been declared by PHP. It returns a boolean value true if
the variable has already been set, and false otherwise, or
if the variable is set to the value NULL.
Consider the following script:
1. if (isset($first_name)) {
2. print '$first_name is set';
3. }
This code snippet checks whether the variable
$first_name is defined. If $first_name is defined, isset()
returns true , which will display ' $first_name is set. ' If it
isn’t, no output is generated.
7. PHP Cookies
1.
2.
3.
Cookies are text files stored on the client computer and
they are kept of use tracking purpose. PHP transparently
supports HTTP cookies.
There are three steps involved in identifying returning
users:
Server script sends a set of cookies to the browser. For
example name, age, or identification number etc.
Browser stores this information on local machine for future
use.
When next time browser sends any request to web server
then it sends those cookies information to the server and
server uses that information to identify the user.
Cookies are usually set in an HTTP header. A PHP script
that sets a cookie might send headers that look something
like this:
setcookie(name, value, expire, path, domain,
security);
8. PHP Cookies
Name - This sets the name of the cookie and is stored in an environment
variable called HTTP_COOKIE_VARS. This variable is used while accessing
cookies.
Value -This sets the value of the named variable and is the content that
you actually want to store.
Expiry - This specify a future time in seconds since 00:00:00 GMT on 1st
Jan 1970. After this time cookie will become inaccessible. If this parameter
is not set then cookie will automatically expire when the Web Browser is
closed.
Path -This specifies the directories for which the cookie is valid. A single
forward slash character permits the cookie to be valid for all directories.
Domain - This can be used to specify the domain name in very large
domains and must contain at least two periods to be valid. All cookies are
only valid for the host and domain which created them.
Security - This can be set to 1 to specify that the cookie should only be
sent by secure transmission using HTTPS otherwise set to 0 which mean
cookie can be OK Date: Tue, 19 Apr
HTTP/1.1 200sent by regular HTTP. 2011 21:03:38 GMT
Server: Apache/1.3.9 (UNIX) PHP/4.0b3
Set-Cookie: name=xyz; expires=Friday, 22-Apr-11 22:03:38
GMT; path=/; domain=Yourdomian.com
Connection: close Content-Type: text/html
9. PHP Sessions
An alternative way to make data accessible across the
various pages of an entire website is to use a PHP Session.
PHP has a great set of functions that can achieve the same
results of Cookies and more without storing information on
the user's computer. PHP Sessions store the information on
the web server in a location that you chose in special files.
These files are connected to the user's web browser via the
server and a special ID called a "Session ID". This is nearly
99% flawless in operation and it is virtually invisible to the
user.
The location of the temporary file is determined by a
setting in the php.ini file called session.save_path. Bore
using any session variable make sure you have setup this
path.
10. Avoid Error PHP - Sessions
PHP Example: <?php
echo "Look at this nasty error below:<br />";
session_start();
?>
Error!
Warning: Cannot send session cookie - headers
already sent by (output started at
session_header_error/session_error.php:2) in
session_header_error/session_error.php on line 3
Warning: Cannot send session cache limiter headers already sent (output started at
session_header_error/session_error.php:2) in
session_header_error/session_error.php on line 3
PHP Example: <?php
session_start();
echo "Look at this nasty error
below:";
?>
Correct