Tuesday, 5 April 2016

UNSET FUNCTION IN PHP


UNSET

Use : It Unset a given variable

But is this enough? I think it behaves more than that, depends on how and with respect to what you are using this function.For example you are destroying the local variable, global variable, static variable, or as a casting function. we will discuss later about the all given examples

HOW TO USE THIS

SYNTAX

void unset ($var1 [,$var2, ... ] )

unset is the function which will unset your variable.

$var1, $var2, ... are the variables which you wants to destroy. you can give any number of variables to destroy.

Return value: 

 It return nothing

BEHAVIOR OF UNSET FUNCTION 

The behavior of unset() function depends on what type of variable you are attempting to destroy.

Three types of variable you want to destroy

1.GLOBAL VARIABLE

If a GLOBAL variable is unset() inside of a function, only the local variable is destroyed not the GLOBAL variable. The GLOBAL variable inside the function will retain the same value as before unset() was called.

Let's discuss it with the help of an example

EXAMPLE

<?php
function destroyMe() 
{
    global $var;
    unset($var);
}

$foo = 'bar';//global variable
destroyMe();
echo $foo;
?>

OUTPUT

bar


The above function will only destroy the local variable
If you want to destroy global variable the you have do like this 



<?php
function desctroyMe() 
{
    unset($GLOBALS['var']);
}

$var = "destroyME";
echo "Before destroy value is:".$var;
desctroyMe();
echo "After destroy value is:".$var;
?>

OUTPUT

Before destroy value is:destroyME 

PHP Notice:  Undefined variable: var in /home/tarun/temp.php on line 11

After destroy value is:


2. LOCAL VARIABLE


If you want to destroy the local variable then just use the function unset($var).
We will not discuss about this in detail because its easy to do, just try yourself.

3. STATIC VARIABLE

<?php
function unsetMe()
{
   static $a;
   $a++;
   echo "Before unset a value is-->$a\n";
   unset($a);
   echo "$a\n";
   static $a;
   echo "After Unset a value is-->$a\n";

}


unsetMe();
unsetMe();
unsetMe();
?>


OUTPUT

PHP version  PHP 5.5.9-1

Before unset a value is-->1
PHP Notice:  Undefined variable: a in /home/tarun/temp.php on line 8

After Unset a value is-->1
Before unset a value is-->2
PHP Notice:  Undefined variable: a in /home/tarun/temp.php on line 8

After Unset a value is-->2
Before unset a value is-->3
PHP Notice:  Undefined variable: a in /home/tarun/temp.php on line 8

After Unset a value is-->3

You can see in the example that after unset is is again referencing to the same variable. surprised?. Yes it happen so when you will destroy the static variable, only refernece is destroyed to that variable, not the value.

Thats why when you have again get the reference using statement after unset

static $a;

It has linked you to the address of the variable, and printing the value of variable a.


4. AS A CASTING FUNCTION 

When you are using unset() as a casting function then it will now originally destroy the value of the variable. here is the expaple which will described everything. 

<?php
$name = 'Tarun';

var_dump((unset) $name);
var_dump($Tarun);
?>

OUPUT

NULL
string(5) "Tarun"



USEFUL LINKS

You can try here online

http://www.w3resource.com/php/function-reference/unset.php

Know how it behaves with array

http://stackoverflow.com/questions/6376702/php-unset-array-value

HOW TO LOAD DATA AFTER PAGE LOAD

HOW TO LOAD DATA AFTER PAGE LOAD


Nowdays you have seen in many sites that some data get loaded after the entire page load. They show a loader image, and when data retrieved from the backend side,area is filled with that data and image get invisble.

Like this image



So here We discuss how we can achieve this technique

You can achieve this with the help of .load() function.

You need to write the below code in your file

HOW TO USE

$( window ).load(function() {
  // Run code
});

OR 

script>
      function load() {
        //Your code
      }
      window.onload = load;
</script>



WHEN TO USE THIS

  • While working in the project you come to a situation when there is a huge amount of data need to be displayed on the page. But by default PHP has some limitation when loading the page. You can change the settings but this is hack. Sometime you have no permission to change the config file of the system, then what?So the Solution is use this technique.

  •  Faster page load

  •  Page is loaded only once, any all data will be get with the asynchronous request of the .load() function

 

.LOAD() COMPARED WITH .READY()

.load() function is always get compared with the .ready() function. But they are quiet different in their behaviour


.LOAD()


load is called when all assets are done loading, including images. ready is fired when the DOM is ready for interaction.

The load event fires at the end of the document loading process. They are stored in the stack and when page finished loading, these events get executed one after another. At this point, all of the objects in the document are in the DOM, and all the images and sub-frames have finished loading.



.READY()


$(document).ready(function(){
//code here
});


Save the below code in test.php or test.html

Example

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>MyTechStopage</title>
  <style>
  p {
    color: red;
  }
  </style>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
  <script>
 
  $( document ).ready(function() {
    $( "p" ).text( "See the changed text" );
  });
 
  </script>
 
</head>
<body>
 
<p>Not loaded yet.</p>
 
 
</body>
</html>

When control reaches the "p" DOM element this event get fired, it will not wait for the entire page load like .load() function

Some Developers try to achieve more task when somthing is loaded. if you want to know the issues that you can face, then please follow the link:



USEFUL LINKS

Know more about .load() function




Know more about .ready() function 

Monday, 4 April 2016

DIFFERENCE BETWEEN GET AND POST METHOD

DIFFERENCE BETWEEN GET  AND POST METHOD



Get and Post both are used to send the data from client to server. But there are several reason that POST is more preferred choice for the security purpose. You have hear of this line many time, but have you ever questioned to yourself or try to know why POST is more secure than GET?

Let's not get panic, I will discuss it in the below section.

Remember about the query string .When you submit any form, your data transfer as a query string. For Example:

http:/myDomain/somePage.php?pin='AFADACV'&id='SADDA'

If you are making a request with GET method then all form data will be appear on the browser URL, making it insecure. Where this does not happen in case of POST method.

POST is more secure than GET method in various reasons:


  • GET requests can be cached

  • GET requests can remain in the browser history

  • GET parameters are passed via URL


This means that parameters are stored in server logs, and browser history. When using GET, it makes it very easy to alter the data being submitted the the server as well, as it is right there in the address bar to play with.

However this does not happen with POST method.

But here are some questions now. if the data appear in the URL then how it is unsafe, after all you are working in your personal computer?
If you are not working in your computer then there can be issue.



Security with GET request

Encode the data with some encoded scheme and then decode data at server side.

They both submit plain text data in the HTTP header, and base64 isn't exactly rocket science (and you can base64 encode your POST version).

When to use which method depends on your requirements. Here are some points to be noted for GET and POST.



GET 


  • The GET method can send data up-to 2048 characters only.

  • Never use GET method if you have password or other sensitive information to be sent to the server, because it can be hacked in the mid way.

  • GET can't be used to send binary data, like images or word documents, to the server. 


  • Get data deals only with the ASCII value not with the binary data.

Example

Save the below code in myfile.php and then fill the form:

<?php
   if( $_GET["firstname"] && $_GET["firstname"]) 
  {
      echo "Welcome ". $_GET['firstname']. "<br />";    
      exit();
   }
?>
<html>
   <body>
   
      <form action = "<?php $_PHP_SELF ?>" method = "GET">
         First Name: <input type = "text" name = "firstname" />
         Last Name: <input type = "text" name = "lastname" />
         <input type = "submit" />
      </form>
      
   </body>
</html>



POST


  • The POST method does not have any restriction on data size to be sent, you can sent any size of data.

  • The POST method can be used to send ASCII as well as binary data, for example images.

  • you can access the post data from $_POST['variable_name'] method


Example

Save the below code in myfile.php and then fill the form:

<?php
   if( $_POST["firstname"] && $_POST["firstname"]) {
      echo "Welcome ". $_POST['firstname']. "<br />";    
      exit();
   }
?>
<html>
   <body>
   
      <form action = "<?php $_PHP_SELF ?>" method = "GET">
         First Name: <input type = "text" name = "firstname" />
         Last Name: <input type = "text" name = "lastname" />
         <input type = "submit" />
      </form>
      
   </body>
</html>


Please open the below link which tells a lot about these two method choice and security. 


Please open the below link which tells what HTTP explained meant to these two