PHP Lesson: $_SERVER

$_SERVER adalah variable yang menyimpan maklumat tentang headers, paths dan lokasi script. Contoh penggunaan $_SERVER adalah bagi mendapatkan nama file dan maklumat device yang melayari page tersebut seperti di bawah.

<?php
    $file_name = basename($_SERVER['PHP_SELF']);
    $device = strtolower($_SERVER['HTTP_USER_AGENT']);

    echo "File name: ".$file_name;
    echo "<br>"
    if(stripos($device,'windows')==true)
    {
        echo "Windows";
    }
    else if(stripos($device,'android')==true)
    {
        echo "Android";
    }
    else if(stripos($device,'iphone')==true)
    {
        echo "Iphone";
    }
    else
    {
        echo $device;
    }
?>

Hasil daripada maklumat $_SERVER adalah

File name: index.php
Device: ccbot/2.0 (https://commoncrawl.org/faq/)

Penerangan lanjut di https://www.w3schools.com/php/php_superglobals_server.asp

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.