Home > Backend Development > PHP Tutorial > Two examples of php global variable $_SERVER

Two examples of php global variable $_SERVER

WBOY
Release: 2016-07-25 08:56:31
Original
1092 people have browsed it
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);
?>
Copy after login

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'] . '!';
}
?>
Copy after login


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template