Home >Backend Development >PHP Tutorial >Two examples of php global variable $_SERVER
This article introduces two examples of the PHP global variable $_SERVER, which are to record access information and the usage of $_SERVER['REQUEST_METHOD'. Friends in need can refer to it.
Example 1, recording visitor information Get the following information: IP address $_SERVER['REMOTE_ADDR']; Source address $_SERVER['HTTP_REFERER']; Browser proxy type $_SERVER['HTTP_USER_AGENT']; Code: <?php $address = $_SERVER['REMOTE_ADDR']; $referer = $_SERVER['HTTP_REFERER']; $browser = $_SERVER['HTTP_USER_AGENT']; $file = fopen("log.html", "a"); $time = date("H:i dS F"); fwrite( $file, "<b>时 间:</b> $time<br>" ); if( $address != null) { fwrite( $file, "<b>IP 地址:</b> $address <br>"); } if( $referer != null) { fwrite( $file, "<b>来 源:</b> $referer<br>"); } fwrite( $file, "<b>浏览器:</b> $browser<br/><hr>"); fclose($file); ?> Example 2, processing data according to method <?php if ($_SERVER['REQUEST_METHOD'] == 'GET') { ?> <form action="<?php echo $_SERVER['SCRIPT_NAME'] ?>" method="post"> 您的姓名? <input type="text" name="first_name" /> <input type="submit" value="Say Hello" /> </form> <?php } else { echo '您好, ' . $_POST['first_name'] . '!'; } ?> |