Thursday 5 June 2014

Difference Between UNSET & UNLINK.

Posted By Amit Tiwari

Hello friends there are two main functions used for deleting the file and destroy the variable.Namely UNLINK & UNSET.

Example for Unlink function:-

-The unlink() function deletes a file.
-This function returns TRUE on success, or FALSE on failure.

Syntax-

unlink(filename,context)








Code for this,



<?php
                    $file = "test.txt";
       if (!unlink($file))
            {
                                 echo ("Error deleting $file");
             }
else
             {
                                 echo ("Deleted $file");
             }
?>

Now,before starting to know about Unset we need to know about what is Php Session Variables.

PHP Session Variables-


When you are working with an application, you open it, do some changes and then you close it. This is much like a Session. The computer knows who you are. It knows when you start the application and when you end. But on the internet there is one problem: the web server does not know who you are and what you do because the HTTP address doesn't maintain state.

A PHP session solves this problem by allowing you to store user information on the server for later use (i.e. username, shopping items, etc). However, session information is temporary and will be deleted after the user has left the website. If you need a permanent storage you may want to store the data in a database.

Sessions work by creating a unique id (UID) for each visitor and store variables based on this UID. The UID is either stored in a cookie or is propagated in the URL.

Now..you can destroy the session means use of Unset function.
Follow the steps,

1)Start the session.
2)Storing a session variable.
3)Destroying a session.

Starting the Php session-


The "session_start() function" is placed before the <html> tag ..or we can say that this code is placed on the top of the file where you start coding for the website.

For Example,

<?php session_start(); ?>

<html>
<body>

</body>
</html>

Storing a Session Variable:-

<?php
              session_start();
// store session data
              $_SESSION['follower']=1;
?>

<html>
       <body>

<?php
//retrieve session data
                 echo "Facebook=". $_SESSION['follower'];
?>

</body>
</html>

Output-

Facebook=1

Destroying a Session:-

If you wish to delete some session data, you can use the unset() or the session_destroy() function.

<?php
session_start();
if(isset($_SESSION['follower']))
  unset($_SESSION['follower']);
?>

Or you can use directly the session destroy function..

<?php
session_destroy();
?>

Note: session_destroy() will reset your session and you will lose all your stored session data.

Posted by Amit Tiwari


No comments:

Post a Comment