例1,记录访客信息
获取如下的信息:
IP地址 $_SERVER['REMOTE_ADDR'];
来源地址 $_SERVER['HTTP_REFERER'];
浏览器代理类型 $_SERVER['HTTP_USER_AGENT'];
代码:
<?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); ?>
例2,根据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'] . '!'; } ?>