Commit a7427a211b

a7427a211b7cc26f6c1fc2abc439300248c7f641

parent: 3b90c5f28c

Unsigned

Sergey Briskin <sergey@briskin.org> · 2014-03-07 20:15 UTC

	modified:   README.md
	modified:   ifconfig.php

Layout: unified · split

README.md +4 −5
@@ -1,16 +1,15 @@
1ifconfig.php 1ifconfig.php
2============ 2============
3 3
4**Simple PHP script to show IP, UserAgent and other info** 4**Simple PHP script to show IP-address, UserAgent and other info**
5 5
6 6
7Added all basic features, such as output in HTML, plain text, XML and JSON. 7Added all basic features, such as output in HTML, plain text, XML and JSON.
8By default it will be in HTML. Use 'f=' arg to select another output format. 8By default it will be in HTML. Also it is possible to request single value.
9Also it is possible to request single value by defining 'q=' arg.
10 9
11Visit some of the links below to view it in action: 10Visit some of the links below to view it in action:
12* [all in plain text](http://briskin.org/ifconfig.php?f=text) 11* [all in plain text](http://briskin.org/ifconfig.php?q=text)
13* [all in xml](http://briskin.org/ifconfig.php?f=xml) 12* [all in xml](http://briskin.org/ifconfig.php?q=xml)
14* [useragent only](http://briskin.org/ifconfig.php?q=ua) 13* [useragent only](http://briskin.org/ifconfig.php?q=ua)
15 14
16P.s. 15P.s.
ifconfig.php +16 −19
@@ -2,7 +2,7 @@
2/* 2/*
3 * User connection info 3 * User connection info
4 * 4 *
5 * Version: 1.0 2014/03/06 5 * Version: 1.1 2014/03/08
6 * License: GPL v2 (https://www.gnu.org/licenses/gpl-2.0.html) 6 * License: GPL v2 (https://www.gnu.org/licenses/gpl-2.0.html)
7 * 7 *
8 * Copyright (C) 2014 Sergey Briskin (http://briskin.org) 8 * Copyright (C) 2014 Sergey Briskin (http://briskin.org)
@@ -43,29 +43,22 @@ $user = array(
43 'dnt' => $_SERVER['HTTP_DNT'] 43 'dnt' => $_SERVER['HTTP_DNT']
44 ); 44 );
45 45
46// Single HTTP-HEADER value request (ex. ifconfig.php?q=ip) 46// Check request (ex. ifconfig.php?q=ip)
47$rqst=trim(preg_replace('/ +/', ' ', preg_replace('/[^A-Za-z0-9 ]/', ' ', urldecode(html_entity_decode(strip_tags($_GET['q'])))))); 47$query=trim(preg_replace('/ +/', ' ', preg_replace('/[^A-Za-z0-9 ]/', ' ', urldecode(html_entity_decode(strip_tags($_GET['q']))))));
48 48
49// Output format (ex. ifconfig.php?f=xml) 49// Return single value on request & die
50$type=trim(preg_replace('/ +/', ' ', preg_replace('/[^A-Za-z0-9 ]/', ' ', urldecode(html_entity_decode(strip_tags($_GET['f'])))))); 50if (isset($query) && !empty($user[$query]) && array_key_exists($query, $user)) {
51 51 die($user[$query]."\n");
52// Return Single value on request & die
53if (isset($rqst) && !empty($user[$rqst]) && array_key_exists($rqst, $user)) {
54 die($user[$rqst]);
55} 52}
56unset($rqst); 53// Return full output in one of supported formats (html, text, xml, json. default: html)
57 54elseif (isset($query) && ($query=="text")) {
58// Return full output in one of supported formats (xml, json, plain text. default: html)
59if (isset($type) && ($type=="text")) {
60 header('Content-Type: text/plain'); 55 header('Content-Type: text/plain');
61 foreach($user as $key => $value) { 56 foreach($user as $key => $value) {
62 echo $key.": ".$value."\n"; 57 echo $key.": ".$value."\n";
63 } 58 }
64 die(); 59 die();
65} elseif (isset($type) && ($type=="json")) { 60
66 header('Content-Type: application/json'); 61} elseif (isset($query) && ($query=="xml")) {
67 die(json_encode($user));
68} elseif (isset($type) && ($type=="xml")) {
69 header('Content-Type: text/xml'); 62 header('Content-Type: text/xml');
70 63
71// Function for SimpleXML creation 64// Function for SimpleXML creation
@@ -80,6 +73,10 @@ function array_to_xml(array $arr, SimpleXMLElement $xml)
80} 73}
81echo array_to_xml($user, new SimpleXMLElement('<info/>'))->asXML(); 74echo array_to_xml($user, new SimpleXMLElement('<info/>'))->asXML();
82 75
76} elseif (isset($query) && ($query=="json")) {
77 header('Content-Type: application/json');
78 die(json_encode($user));
79
83} else { 80} else {
84 header('Content-Type: text/html'); 81 header('Content-Type: text/html');
85?><!DOCTYPE html> 82?><!DOCTYPE html>
@@ -95,7 +92,7 @@ echo array_to_xml($user, new SimpleXMLElement('<info/>'))->asXML();
95 font-size: .9em; 92 font-size: .9em;
96 } 93 }
97 p { 94 p {
98 font-family: "Source Code Pro", "Courier New", "Consolas", "Terminal", "Droid Mono", fixed; 95 font-family: "Source Code Pro", "Droid Mono", "Courier New", "Consolas", "Terminal", fixed;
99 line-height: 1em; 96 line-height: 1em;
100 } 97 }
101 .small, .small > a { 98 .small, .small > a {
@@ -120,4 +117,4 @@ echo array_to_xml($user, new SimpleXMLElement('<info/>'))->asXML();
120<? 117<?
121} 118}
122die(); 119die();
123?> 120?>\ No newline at end of file