List Variable Global $_SERVER pada PHP 5


PHP memiliki beberapa variable global yang jarang di perhatikan (ane sendiri kale….), salah satu varible global yang perlu diperhatikan lebih adalah $_SERVER. Mengapa? Karena begini gan… Situs php.net menjelaskan bahwa $_SERVER berisi informasi mengenai header, lokasi path, dan lokasi script yang dibangun oleh web server, sehingga informasi mengenai user yang mengakses web server dan apa yang sedang dijalankan web server bisa di dapatkan datanya menggunakan variable $_SERVER.

okeh tanpa bertele – tele lagi langsung saja jalankan script dibawah ini pada web server local anda…

<PRE>
<?php
echo 'List Variable $_SERVER <br/><br/>';
echo '$_SERVER[\'PHP_SELF\']   			: '.$_SERVER['PHP_SELF']." <br />";
echo '$_SERVER[\'GATEWAY_INTERFACE\']   	: '.$_SERVER['GATEWAY_INTERFACE']." <br />";

echo '$_SERVER[\'SERVER_ADDR\']   		: '.$_SERVER['SERVER_ADDR']." <br />";
echo '$_SERVER[\'SERVER_NAME\']   		: '.$_SERVER['SERVER_NAME']." <br />";
echo '$_SERVER[\'SERVER_SOFTWARE\']   		: '.$_SERVER['SERVER_SOFTWARE']." <br />";
echo '$_SERVER[\'SERVER_PROTOCOL\']   		: '.$_SERVER['SERVER_PROTOCOL']." <br />";

echo '$_SERVER[\'REQUEST_METHOD\']   		: '.$_SERVER['REQUEST_METHOD']." <br />";
echo '$_SERVER[\'REQUEST_TIME\']   		: '.$_SERVER['REQUEST_TIME']." <br />";

echo '$_SERVER[\'QUERY_STRING\']   		: '.$_SERVER['QUERY_STRING']." <br />";
echo '$_SERVER[\'DOCUMENT_ROOT\']   		: '.$_SERVER['DOCUMENT_ROOT']." <br />";

echo '$_SERVER[\'HTTP_ACCEPT\']   		: '.$_SERVER['HTTP_ACCEPT']." <br />";
echo '$_SERVER[\'HTTP_ACCEPT_ENCODING\']   	: '.$_SERVER['HTTP_ACCEPT_ENCODING']." <br />";
echo '$_SERVER[\'HTTP_ACCEPT_LANGUAGE\']   	: '.$_SERVER['HTTP_ACCEPT_LANGUAGE']." <br />";
echo '$_SERVER[\'HTTP_CONNECTION\']   		: '.$_SERVER['HTTP_CONNECTION']." <br />";
echo '$_SERVER[\'HTTP_HOST\']   		: '.$_SERVER['HTTP_HOST']." <br />";
echo '$_SERVER[\'HTTP_USER_AGENT\']   		: '.$_SERVER['HTTP_USER_AGENT']." <br />";

echo '$_SERVER[\'REMOTE_ADDR\']   		: '.$_SERVER['REMOTE_ADDR']." <br />";
echo '$_SERVER[\'REMOTE_PORT\']   		: '.$_SERVER['REMOTE_PORT']." <br />";

echo '$_SERVER[\'SERVER_ADMIN\']   		: '.$_SERVER['SERVER_ADMIN']." <br />";
echo '$_SERVER[\'SERVER_PORT\']   		: '.$_SERVER['SERVER_PORT']." <br />";
echo '$_SERVER[\'SERVER_SIGNATURE\']   		: '.$_SERVER['SERVER_SIGNATURE']." <br />";

echo '$_SERVER[\'SCRIPT_NAME\']   		: '.$_SERVER['SCRIPT_NAME']." <br />";
echo '$_SERVER[\'REQUEST_URI\']   		: '.$_SERVER['REQUEST_URI']." <br />";
?>
</PRE>

output :

List Variable $_SERVER 

$_SERVER['PHP_SELF']   			: /test.php 
$_SERVER['GATEWAY_INTERFACE']   	: CGI/1.1 
$_SERVER['SERVER_ADDR']   		: ::1 
$_SERVER['SERVER_NAME']   		: localhost 
$_SERVER['SERVER_SOFTWARE']   		: Apache 
$_SERVER['SERVER_PROTOCOL']   		: HTTP/1.1 
$_SERVER['REQUEST_METHOD']   		: GET 
$_SERVER['REQUEST_TIME']   		: 1334284051 
$_SERVER['QUERY_STRING']   		: vthink=5&team=5 
$_SERVER['DOCUMENT_ROOT']   		: /Applications/MAMP/htdocs 
$_SERVER['HTTP_ACCEPT']   		: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
$_SERVER['HTTP_ACCEPT_ENCODING']   	: gzip, deflate 
$_SERVER['HTTP_ACCEPT_LANGUAGE']   	: en-us 
$_SERVER['HTTP_CONNECTION']   		: keep-alive 
$_SERVER['HTTP_HOST']   		: localhost 
$_SERVER['HTTP_USER_AGENT']   		: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/534.51.22 (KHTML, like Gecko) Version/5.1.1 Safari/534.51.22 
$_SERVER['REMOTE_ADDR']   		: ::1 
$_SERVER['REMOTE_PORT']   		: 50032 
$_SERVER['SERVER_ADMIN']   		: you@example.com 
$_SERVER['SERVER_PORT']   		: 80 
$_SERVER['SERVER_SIGNATURE']   		:  
$_SERVER['SCRIPT_NAME']   		: /test.php 
$_SERVER['REQUEST_URI']   		: /test.php?vthink=5&team=5 

Nah, informasi yang anda lihat pada hasil output yang ditampilkan pada output diatas tentunya tidak sama dengan output pada web server local anda. Jadi, informasi yang dihasilkan oleh web server anda tergantung pada siapa yang membuka dan web server apa yang anda gunakan. Seperti pada bagian $_SERVER[‘HTTP_USER_AGENT’] anda bisa melihat bahwa saya sedang menggunakan web browser safari dengan system operasi Mac OSX versi Intel Max OS X 10.7.2. Kemudian pada output $_SERVER[‘REQUEST_URI’] anda bisa melihat saya mengakses melalui web browser dengan url /test.php?vthink=5&team=5 (yups, coz ane jalanin di http://localhost/test.php?vthink=5&team=5)…..

Jika anda ingin mempersingkat penulisan sintaks diatas dan melihat beberapa variable $_SERVER yang belum ane tulis, silahkah tulis sintaks di bawah ini :

<PRE>
	<?php
		print_r($_SERVER);
	?>	
</PRE>

output :

Array
(
    [HTTP_HOST] => localhost
    [HTTP_USER_AGENT] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/534.51.22 (KHTML, like Gecko) Version/5.1.1 Safari/534.51.22
    [HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    [HTTP_CACHE_CONTROL] => max-age=0
    [HTTP_ACCEPT_LANGUAGE] => en-us
    [HTTP_ACCEPT_ENCODING] => gzip, deflate
    [HTTP_CONNECTION] => keep-alive
    [PATH] => /bin:/sbin:/usr/bin:/usr/sbin:/usr/libexec:/System/Library/CoreServices
    [SERVER_SIGNATURE] => 
    [SERVER_SOFTWARE] => Apache
    [SERVER_NAME] => localhost
    [SERVER_ADDR] => ::1
    [SERVER_PORT] => 80
    [REMOTE_ADDR] => ::1
    [DOCUMENT_ROOT] => /Applications/MAMP/htdocs
    [SERVER_ADMIN] => you@example.com
    [SCRIPT_FILENAME] => /Applications/MAMP/htdocs/test.php
    [REMOTE_PORT] => 50164
    [GATEWAY_INTERFACE] => CGI/1.1
    [SERVER_PROTOCOL] => HTTP/1.1
    [REQUEST_METHOD] => GET
    [QUERY_STRING] => vthink=5&team=5
    [REQUEST_URI] => /test.php?vthink=5&team=5
    [SCRIPT_NAME] => /test.php
    [PHP_SELF] => /test.php
    [REQUEST_TIME] => 1334284637
    [argv] => Array
        (
            [0] => vthink=5&team=5
        )

    [argc] => 1
)

Okey, silahkan mencoba dan semoga beberapa variable diatas bisa bermanfaat untuk anda gunakan pada aplikasi web yang anda bangun (terutama yang tidak menggunakan framework)…

sumber : http://www.php.net/manual/en/reserved.variables.server.php

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s