HTTP Request Methods-PHP



HTTP Request Methods The Internet’s HTTP protocol, commonly used to fetch Web pages, defines a number of “methods” that browsers can use to send requests and data to Web servers. Of the available methods, the two most important are the GET method and the POST method.
GET is the “default” method for the Internet, used whenever you request a page with your browser. All data in the request must be encoded in the URL.

POST is most often used for submitting forms. It allows additional form data to be sent with the request. HTML lets you specify the method to use for each formtag. Although GET is the default, it is most common to use POST, which avoids cluttering the URL with the submitted data.


Use the POST method when declaring your form in HTML. This prevents
form values from appearing in the URL, and allows a larger amount of data
to be submitted through the form.

Use PHP’s htmlspecialcharsfunction when populating form fields with
PHP values, to avoid malformed HTML.
PHP has its own wrappers for Curl, so we can use the same tool from within
PHP. A simple GETrequest looks like this:
<?php
$url = "http://oreilly.com";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);

?>
The previous example is the simplest form, setting the URL, making a request to its
location (by default this is a  GET  request), and capturing the output. Notice the use of
curl_setopt(); this function is used to set many different options on Curl handles and
it has excellent and comprehensive documentation on http://php.net. In this example,
it is used to set the  CURLOPT_RETURNTRANSFERoption to  true, which causes Curl to  return
the results of the HTTP request rather than  outputthem. In most cases, this option
should be used to capture the response rather than letting PHP echo it as it happens.
We can use this extension to make all kinds of HTTP requests, including sending custom
headers, sending body data, and using different verbs to make our request.
If you use normal HTTP, form data will be sent in “clear text” over the Internet
from the browser to the server. This means it can be intercepted by someone
using a packet sniffer. When you send confidential information such as financial details,
 use an encryption technology such as SSL.

<?php
$url = "http://requestb.in/example";
$data = array("name" => "Lorna", "email" => "lorna@example.com");
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER,
array('Content-Type: application/json')
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);

?>
Again,  curl_setopt()is used to control the various aspects of the request we send.
Here, a POSTrequest is made by setting the CURLOPT_POSToption to 1, and passing the
data we want to send as an array to the  CURLOPT_POSTFIELDSoption. We also set a
Content-Typeheader, which indicates to the server what format the body data is in; the
various headers

Assuming magic quotes is disabled on your server, and you have no other measures
in place to prevent it, this clever attack alters the meaning of the query:
SELECT * FROM users
WHERE username='' AND password='' OR username LIKE '%'

The modified query will select allrecords in the user table! When the script checks
whether any users matched the supplied user name and password combination,
it will see this big result set and grant access to the site

This can be prevented if we escape the incoming variables:

$sql = "SELECT * FROM users
WHERE username='" . safeEscapeString($_POST['username']). "'
AND password='" . safeEscapeString($_POST['password']). "'";
In some cases, depending on the circumstances, this may not be necessary.

Related Posts:
  • $_ENV and $_SERVER ? PHP sets several variables for you containing information about the server, the environment, and your visitor's request. These are stored in the superglobal arrays $_ENV and $_SERVER, but their availability depends on whe… Read More
  • magic_quotes_gpc, magic_quotes_runtime Magic quotes is the name of a PHP feature that automatically quotes inputdata, by using the addslashes() function. Historically, this was used so thatform data could be used directly in SQL queries without any security or qu… Read More
  • $_GET , $_POST,$_COOKIE?? $_GET contains any variables provided to a script through the GET method.  $_POST contains any variables provided to a script through the POST method.  $_COOKIE contains any variables provided to a script through a… Read More
  • Reading DOC file in php   read PDF and DOC files using PHP Reading PDF Files   $content = shell_exec('/usr/local/bin/pdftotext '.$filename.' -');     Reading DOC Files  $content = shell_exec('/usr/local/bin/antiword '.$fi… Read More
  • How we know browser properties? echo $_SERVER['HTTP_USER_AGENT']; $browser = get_browser(); foreach ($browser as $name => $value) { echo “$name $value \n”; } get_browser   returns the capabilities of the user's browser. … Read More
  • Finding the Position of a Value in an Array Use array_search( ) . It returns the key of the found value. If the value is not in the array, it returns false:   $position = array_search($value, $array); if ($position !== false) { // the element in position&n… Read More
  • Web server compression? The best way to understand web server compression is to think of sending ZIP filesinstead of uncompressed files from your web server to your web user. Sending less dataover the network will minimize network latency and your … Read More
  • Setting Default Values for Function Parameters Assign the default value to the parameters inside the function prototype: function wrap_html_tag($string, $tag = 'b') { return "<$tag>$string</$tag>"; }     The example in the Solution sets the… Read More
  • Cookies Versus Sessions?  Cookies The setcookie( ) call needs to be before the HTML form because of the way the web works. HTTP operates by sending all "header" information before it sends "body" information. In the header, it sends t… Read More
  • What is triggers? A trigger is a database object which is associated with particular database table. Triggers gets called automatically when particular event(INSERT, UPDATE, DELETE) occurs on table. … Read More
  • Extracting Substrings You want to extract part of a string, starting at a particular place in the string. For example, you want the first eight characters of a username entered into a form.  Extracting a substring with substr( ) &l… Read More
  • Checking Variable Values and Types FUNCTION is_numeric() True if number or numeric stringctype_digit() True if all digits are numeric charactersis_bool() True if variable is a Booleanis_null() True if variable is NULLis_float() True if variable type is a fl o… Read More
  • Associative Arrays? You can use string values as keys. For example, you might create an arraylike this: $myStuff = array();$myStuff[“name”] = “andy”;$myStuff[“email”] = “andy@fsdsd.ca”;Print $myStuff[“name”]; Associative arrays are different t… Read More
  • Turning an Array into a String convert it into a  formatted string. Use join( ): // make a comma delimited list $string = join(',', $array); Or loop yourself: $string = ''; foreach ($array as $key => $value) { $string .= ",$value"; } $str… Read More
  • PHP Classes Classes do not use scope keywords, but you can prevent people from instantiating the class by makingthe __construct() method and the __clone() methods private or protected. The __construct()method is used to create the objec… Read More