SUPER GLOBAL in PHP (Global Variables)

SUPER GLOBAL in PHP (Global Variables)

There are some Predefined variables in the PHP which are accessible regardless of scope also you can access them in any class, function, and any file.

Following are some super global variables in PHP:

  • $_GET

  • $_POST

  • $_REQUEST

  • $_SERVER

  • $_COOKIE

  • $_SESSION

  1. $_GET: $_GET is a super global variable that is used to fetch data from the form. We need to set the form method to 'get' to access data using $_GET variable.

     <body>
         <form action= "xyz.php" method= "get">
             <input type="text" name="user_name">
         </form>
     </body>
    

    using method = "get" we access the data through the form in the mentioned xyz.php file.

    • accessing the user name in the php file is done using $_GET['user_name']

        <?php
        //fetching the user name into the name variable through the get method.
        $name = $_GET['user_name'];
        //displaying the name variable
        echo $name;
        ?>
      
      • information sent through the get method is visible to everyone also it is visible in the URL.

      • so it is not used to send user information and credentials.

      • Instead, it is used to share youtube video links or many more because this kind of thing requires easy access.

  2. $_POST: $_POST method is similar to the $_GET method here instead of using the get method we use the "POST" method in the form tag.

     <body>
         <form action= "xyz.php" method= "post">
             <input type="text" name="user_name">
         </form>
     </body>
    
    • Similarly, if we have to access the user name through the post method we use $_POST['user_name']

        <?php
        //fetching the user name into the name variable through the post method.
        $name = $_POSt['user_name'];
        //displaying the name variable
        echo $name;
        ?>
      

      information sent using the POST method is not visible in the url

    • this method can be used for sending the credentials and user information

  3. when both $_POST and $_GET methods perform the same activity .so the question arises what are the differences between them? I have discussed them in my other blog (Difference between $_POST and $_GET method)

  4. $_REQUEST: $_REQUEST is also used to fetch data from the submitted form.

    • the $_REQUEST is an associative array that by default contains $_POST,$_GET,$_COOKIE .which means you can access the data passed through the request method using the get and post method.

    • It can also be used to access the form data within the same file using

      PHP_SELF

        // when we have to acces the form data within the same file 
        <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
          Name: <input type="text" name="fname">
          <input type="submit">
        </form>
      
        <?php
        if ($_SERVER["REQUEST_METHOD"] == "POST") {
          // collect value of input field using the post method
          $name = $_REQUEST['fname'];
         }
      
  5. $_SERVER: using the variable we can access information about the server .

    it holds the information of headers, paths, and script locations.

    when we want information like :

    • HTTP Server

    • SERVER information

    • HOST information

    • URL Information

about the "POST' and "GET" methods we use $_SERVER variable.

    <?php
    echo $_SERVER['PHP_SELF'];
    ?>
  1. $_COOKIE: Cookie is nothing but the user information file which resides in the user's local storage. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values. It reduces the multiple login process. It is used to identify the user when the request is sent to the server through which it identifies the user and access allowed.

    To implement the cookie concept

    1. we have to create a cookie using setcookie(), we have to pass a suitable parameter in the function which involves

      • name: It is the name of the cookie.

      • value: It is the value that we set for the cookie.

      • expiry time: The time after which the cookie will get expire.

      • path: a path through which it is accessible.

      • there are also some optional fields (domain, secure, and httponly).

        <?php
            $name = "cookie";
            $value = 1231234;
            setcookie($name, $value, Time + (3600),'/');
        ?>
  1. We can use or get information stored in cookies through $_COOKIE variable.

    but before accessing it we check if the cookie is set or not using method isset(). else it will throw error.

     <?php
         if(isset($_COOKIE['$name']){
             echo $_COOKIE['$name'];
         }else{
             echo "Cookie is not set";
         }
     ?>
    
    • cookie automatically gets deleted after the expiry time is hit.
  1. $_SESSION: It is yet another way to make data accessible across the various pages of an entire website is to use a PHP Session.

    • A session creates a file in a temporary directory on the server where registered session variables and their values are stored. This data will be available on all pages on the site during that visit.

    • A session ends when the user loses the browser or after leaving the site, the server will terminate the session after a predetermined period of time.

    • PHP session technique is widely used in shopping websites where we need to store and pass cart information e.g. username, product code, product name, product price etc from one page to another.

    • STARTING A PHP SESSION

      It is the first and compulsory step for making use of the session here we use the session_start() keyword to start the session.

        <?php
            session_start();
        ?>
      
    • $_SESSION

      PHP $_SESSION is an associative array that contains all session variables. It is used to set and get session variable values.

        <?php
            //starting the session
            session_start();
            //setting session variable user as ACP
            $_SESSION['user'] = "ACP";
            //geeting the value 
            echo $_SESSION['user'];
        ?>
      
    • DESTROYING THE PHP SESSION

      Destroying the php session involves the two steps

      • unsetting all the data, we use session_unset() keyword.

      • Destroying the session for this we use key word session_destroy

          <?php
              //starting the session
              session_start();
              //setting session variable user as ACP
              $_SESSION['user'] = "ACP";
              //geeting the value 
              echo $_SESSION['user'];
        
              //unsetting
              session_unset();
              //destroying session
              session_destroy();
          ?>