Showing posts with label Web Features. Show all posts
Showing posts with label Web Features. Show all posts

Monday, April 1, 2013

HTTP Authentication

HTTP authentication 
with PHP is used to authenticate a webpage. By using this when user open a website, it will prompt a username and password.
Username will stored in "PHP_AUTH_USER" and password will stored in "PHP_AUTH_PW"


Basic example of of Authentication is below:

$username='test';
$password='test123';
function authenticate(){
    header('WWW-Authenticate: Basic realm="My Realm"');
    header('HTTP/1.0 401 Unauthorized');
}
if (!isset($_SERVER['PHP_AUTH_USER'])) {
  authenticate();
    echo 'Text to send if user hits Cancel button';
    exit;
} else {
    $u=$_SERVER['PHP_AUTH_USER'];
    $p=$_SERVER['PHP_AUTH_PW'];
    if(!($u==$username && $p==$password)){
    authenticate();
    }
    
}

HTTP Headers

HTTP Headers is used to send a raw HTTP header. It tell the client what data is going to render as html in browser.

Following are the same cases where we use header.


Used to specify that file not found.
header("HTTP/1.0 404 Not Found");

Redirect from current page to another page
header("Location: http://200-530.blogspot.in");

Specify we are going to download a pdf file.
// We'll be outputting a PDF
header('Content-type: application/pdf');

// It will be called downloadfile.pdf
header('Content-Disposition: attachment; filename="downloadfile.pdf"');

// The PDF source is in originalFile.pdf
readfile('originalFile.pdf');

Used to specify, Don't cache a page.
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past

HTTP cookie

What is a Cookie?
Cookie is also known as HTTP cookie or web cookie or browser cookie. When you access web pages, some data from that webpages store in your browser known as cookie.
OR
Cookies are a mechanism for storing data in the remote browser and thus tracking or identifying return users.
When you Login to some website, then session is stored in cookie.



How to Create a Cookie?
setcookie($name, $value, $expireDateTime, $path, $domain, $secure, $httpOnly);

How to Retrieve a Cookie Value?
echo $_COOKIE[$name];

How to Delete a Cookie?
Cookie can not be deleted. You can delete cookie by providing the expire date in set cookie. For Example
setcookie($name, "", time()-3600);



PHP -> GET and POST Methods

Form is used to send the information to website.

There is two method GET & POST. By Default it is GET method, if you don't specify in method attribute.
Both method have their own advantage & disadvantage.


GET Method

Advantage  of GET method


  • Data is visible in Address Bar (URL), when form is submit through GET method.
  • As data is visible in URL, It good for search.  when can copy URL and past in address bar then he need not to re-select the option for submit.
  • Access through $_GET  (SUPER GLOBAL variable in PHP
  • Access through $_REQUEST (SUPER GLOBAL variable )


Disadvantage  of GET method
  • Data is visible in Address Bar (URL), sometimes when don't want to let the user to know what we are sending in form like checkout page.
  • It has limit 2KB, you can't send more than 2 KB
  • Data is not secured as visible in address bar.
  • Can't Access data with $_POST 
  •  you cannot send file with GEt Method.

POST Method

Advantage  of  POST method

  • Data is not visible in Address Bar, when form is submit through POST method.
  • Data is more secure in POST method as compare to GET method.
  • Access through $_POST  (SUPER GLOBAL variable in PHP.
  • Access through $_REQUEST (SUPER GLOBAL variable ).
  • You can send file like image, zip file in POST Method.
  • By Default "php_value post_max_size" is 8MB, but we can increase it from php.ini file


PHP-Form

What is Form?
Form is group of fields which allow user to add information.
Fields like input, checkbox, radio, single select, multiple select etc

Why we use HTML Form?
HTML form is used to get the information from the user, which is in client. User will the information and submit the form, then admin get all information which is filed by user.

What are all the fields of form?
http://www.w3schools.com/html/html_forms.asp

Sunday, March 31, 2013

PHP-Sessions

Sessions is an object which is used to store single OR multiple values in form of array OR string OR an object. These values are maintained/stored when you navigate  one more more pages. Normal variable destroy automatically when you navigate the pages.

Difference between Session variable and Normal variable.
Normal variable Session variable
Destroy when refresh the page value are maintain when refresh the page
Store in Memory Store in cookie
Stored location cannot be change Location can be changed
-NA- In Login/Logout functionalities use session
You can declare/use any where in php pages Need to start session befor use the session
use session_start() to start the session
variable deleted automatically when out of scope  Session need to delete manually.
Session delete automatically when it expire.
delete automatically when out of scope use "session_destroy()" to destroy

How to Create Session variable
session_start(); //use only once in whole website

/** create variable **/
$_SESSION['count']=1;

/** use variable **/
$count = $_SESSION['count'];


How to Destroy session variable
unset($_SESSION['count']);