why is geoplugin api doesnt work on online server? - php

I'm trying to get the IP and location data of my visitors but I'm getting an error.
My Code works on localhost but refer no data but not on my online server
there is the code:
<?php
//GET th passed data
$page = $_GET['page'];
//GET VISITOR IP
function get_ip(){
if(isset($_SERVER['HTTP_CLIENT_IP'])){
return $_SERVER['HTTP_CLIENT_IP'];
}elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
return $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else{
return (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '');
}
}
$ip =get_ip();
if($ip){
$query = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$ip));
if($query){
//getting th variabales
$country = $query['geoplugin_countryName'];
$countrycode = $query['geoplugin_countryCode'];
$city = $query['geoplugin_city'];
$regionname = $query['geoplugin_regionName'];
$lat = $query['geoplugin_latitude'];
$long = $query['geoplugin_longitude'];
$timezone = $query['geoplugin_timezone'];
//move the variabales
echo "<script> window.location.assign('../".$page.".php?IP=".$ip."&country=".$country."&countrycode=".$countrycode."&city=".$city."&latitude=".$lat."&longtitude=".$long."&timez=".$timezone."&regionName=".$regionname."&gettingdata= success');</script>";
}else{
echo "<script type='text/javascript'>alert('حدث خطأ ما والرجاء المحاولة في ما بعد')</script>";
echo "<script> window.location.assign('../index.php?getting data = failed');</script>";
}
}else{
echo "<script type='text/javascript'>alert('حدث خطأ يرجى المحاولة في ما بعد')</script>";
echo "<script> window.location.assign('../index.php?getting IP= failed');</script>";
}
?>

Most likely your live server doesn't allow file_get_contents to load files via HTTP, which is a security measure.
You should look into your php.inito find the value set for allow_url_fopen
A similar problem was solved here

Related

json_decode return null i guess

I try to run this on my server:
<?php
$ip = $_SERVER['REMOTE_ADDR'];
$details = json_decode(file_get_contents("http://ipinfo.io/{$ip}/json"));
echo $ip;
$city = $details -> city;
echo $city;
?>
But, this print ip only.
Maybe a server problem or configuration?
You need to code a bit more defensively, if that site has no data for the ip address you give it, then it wont return any city property
This is a little safer
$ip = $_SERVER['REMOTE_ADDR'];
$details = json_decode(file_get_contents("http://ipinfo.io/{$ip}/json"));
echo $ip . ' ';
if (isset($details->city)){
echo $details->city;
} else {
echo 'data not available';
}
Judging from what it returned for my IP Address, the details it provides are not very accurate anyway

TeamSpeak3 server status

I have big problem because, when I'm trying to show status of my ts3 server I have blank page... What am I doing wrong?
require_once('libraries/TeamSpeak3/TeamSpeak3.php');
try
{
// connect to server, authenticate and grab info
$ts3 = TeamSpeak3::factory("serverquery://query_admin:query_pass#host:10011/?server_port=9987");
// show server as online
$serverinfo[$j]['hostname'] = $ts3->virtualserver_name;
$serverinfo[$j]['online'] = 'online';
$serverinfo[$j]['players'] = $ts3->virtualserver_clientsonline;
$serverinfo[$j]['max'] = $ts3->virtualserver_maxclients;
}
catch(Exception $e)
{
// grab errors and show server as offline
$serverinfo[$j]['online'] = 'offline';
$serverinfo[$j]['players'] = '-';
$serverinfo[$j]['max'] = '-';
}
When I comment this code the page shows as normal...
EDIT:
I see it now, if I only add this
require_once('libraries/TeamSpeak3/TeamSpeak3.php');
and nothing more to my code it shows blank page... Is it possible, that library from here doesn't work properly?
You forgot to add echo before each info so the code becomes :
require_once('libraries/TeamSpeak3/TeamSpeak3.php');
try
{
// connect to server, authenticate and grab info
$ts3 = TeamSpeak3::factory("serverquery://query_admin:query_pass#host:10011/?server_port=9987");
// show server as online
echo $serverinfo[$j]['hostname'] = $ts3->virtualserver_name;
echo $serverinfo[$j]['online'] = 'online';
echo $serverinfo[$j]['players'] = $ts3->virtualserver_clientsonline;
echo $serverinfo[$j]['max'] = $ts3->virtualserver_maxclients;
}
catch(Exception $e)
{
// grab errors and show server as offline
echo $serverinfo[$j]['online'] = 'offline';
echo $serverinfo[$j]['players'] = '-';
echo $serverinfo[$j]['max'] = '-';
}

php- File Type Restriction not working

I am trying to restrict Image file type to only JPG,PNG,JPEG but this script is not allowing any file (even if its above mentioned formats) and the NO FILE selected part is also not working. though I am selecting the file it's still alerting you have to select profile picture(the JS alert msg mentioned in the code) what's wrong in the code?
if(isset($_POST['submit'])){
//this part is part of the HTML form. please neglect this. this's working
$user = $_POST['username'];
$pass = $_POST['password'];
$email =$_POST['email'];
if($_FILES["profpic"]["tmp_name"] =="") { //even $_FILES['profpic']['name']=="" is not working
echo "<script>alert('You have to upload Profile Picture.');</script>";
exit();
}
//$check = getimagesize($_FILES["profpic"]["tmp_name"]);
if(getimagesize($_FILES["profpic"]["tmp_name"])) {
echo "<script>alert('Invalid Image.');</script>";
exit();
}
if ($_FILES["profpic"]["size"] > 1000000) {
echo "<script>alert('Your Image is too big,Maximum Image Size is 1MB');</script>";
exit();
}
$type = $_FILES['profpic']['type'];
$allowedImageType = array('image/jpg','image/png','image/jpeg');
function imageType($image){
global $allowedImageType;
if(in_array($image,$allowedImageType))
return true;
else
return false;
}
if(!imageType($type)) {
echo "<script>alert('Only JPG,JPEG,PNG Image Allowed!');</script>";
exit();
}
//the code if everything went right-------------------------
$sql = $conn->query("INSERT INTO users(id,username,password,email) VALUES('','$user','$pass','$email')");
$id = $conn->insert_id;
$picname = "$id.jpg";
move_uploaded_file($_FILES['profpic']['tmp_name'], "images/$picname");
echo "<script>document.getElementById('noti').innerHTML='Account Creation Successful. <a href=\'login.php\'>Click here</a> to Login';</script>";
}

Issue with script look at the website

I could not find this problem anywhere and nothing really related that I can see. I have a URL Shortening script that I purchased, this script works great on my first server, installed the database, FTP'd the files and was all set and shortening URLs.
But since then I moved it to a VPS server, I setup the server exactly same, with Centos 5.6. Installed the script, everything is fine. But the only thing that I find that does not work is the shortening process. I have no idea why it won't work. I have little knowledge of Ajax and JavaScript which I think the problem is with.
Here is ajax.php
<?php
require_once '../config.php';
function url_exist($url)
{
$c=curl_init();
curl_setopt($c,CURLOPT_URL,$url);
curl_setopt($c,CURLOPT_HEADER,1);
curl_setopt($c,CURLOPT_NOBODY,1);
curl_setopt($c,CURLOPT_RETURNTRANSFER,1);
if(!curl_exec($c)){
return false;
}else{
return true;
}
}
function shorteURLFromID ($integer)
{
$base = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$length = strlen($base);
$out = '';
while($integer > $length - 1)
{
$out = $base[fmod($integer, $length)] . $out;
$integer = floor( $integer / $length );
}
return $base[$integer] . $out;
}
if(isset($_POST['url']))
{
$l_type = (string) $db->escape(trim(strip_tags($_POST['type'])));
$url = trim(strip_tags($_POST['url']));
$url = str_replace(array("http://"), array(''), $url);
if(strlen($url) < 3) {
print '<style>.alert-300{width:325px;margin-left:182px;}</style>';
die("<div class='alert alert-error alert-300'>".translate('empty_url_error')."</div>");
}
if(stristr($url, $_SERVER['SERVER_NAME'])) {
print '<style>.alert-300{width:325px;margin-left:182px;}</style>';
die("<div class='alert alert-error alert-300'>".translate('same_site_error')."</div>");
}
if(url_exist($url))
{
//check if exists for this user
$rs = $db->get_row("SELECT string FROM links WHERE uID = '".$usersObject->ID()."' AND
destination = '".mysql_real_escape_string($url)."' LIMIT 1");
if(count($rs)) {
$string = $rs->string;
print "http://".$_SERVER['SERVER_NAME']."/".$string;
}else{
$rs = $db->query("INSERT INTO links (destination, added, ip, uID, type) VALUES
('".$db->escape($url)."', '".time()."', '".ip2long($_SERVER['REMOTE_ADDR'])."',
'".$usersObject->ID()."', '".$l_type."')");
if($rs) {
$string = shorteURLFromID($db->insert_id);
$rs = $db->query("UPDATE links SET string = '$string' WHERE linkID = '".mysql_insert_id()."'");
print "http://".$_SERVER['SERVER_NAME']."/".$string;
}else{
print '<style>.alert-300{width:325px;margin-left:182px;}</style>';
print "<div class='alert alert-error alert-300'>Could not create shortened link ".mysql_error()."</div>";
}
}
}else{
print '<style>.alert-300{width:325px;margin-left:182px;}</style>';
print "<div class='alert alert-error alert-300'>".translate('invalid_url_error')."</div>";
}
}else{
print '<style>.alert-300{width:325px;margin-left:182px;}</style>';
print '<div class="alert alert-error alert-300">'.translate('invalid_url_error').'</div>';
}
?>
The only thing that comes to mind without further info (or source of the script) is the version of PHP you're using. Is it different than that of the old server? If so, is it pre-5.2?
Try adding in this option:
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, 0);
It looks like the default was changed with cURL 7.10 to true, so it is possible PHP 5.2.17 was using a version prior to 7.10.
Levi

Link PHP file as PNG image? - Image is a variable based on server status

Here is my code, you can view an example of it by going to:
www.craftquake.com/statusChecker.php?site=MCnet
<?php
$getter = $_GET['site'];
if ($getter == 'ts3')
{ $site = test_port('ts3.craftquake.com',10011,4); }
if ($getter == 'MCquake')
{ $site = test_port('play.craftquake.com',25565,4); }
if ($getter == 'MCnet')
{ $site = test_port('minecraft.net',80,4); }
$teamspeak = test_port('ts3.craftquake.com',10011,4);
$online = '<img src="/online.png">';
$offline = '<img src="/offline.png">';
$unknown = '<span class="status-unknown" id="status-image">Unknown</span>';
function test_port($host,$port=80,$timeout=1)
{
$fsock = fsockopen($host, $port, $errno, $errstr, $timeout);
if ( ! $fsock )
{
return FALSE;
}
else
{
return TRUE;
}
}
?>
##HEADER & CSS, ETC
<?php
if ($site == 1)
{ $status = $online;
} else if ($site == 0) {
$status = $offline;
} else {
$status = $unknown;
}
header('content-type: image/png');
readfile($status);
echo $status;
?>
I want to, in the footer of my page, link to this page to display the status. I was doing this with another site's script by linking their status of Minecraft.net's servers as the and it worked perfectly, however I have no idea how they made that work. The images are PNG's, but if there is only one format that works, I can convert them.
I have tried the header(blablabla) function, but it doesn't seem to work...
Thank you very much!
Your variables contain HTML instead of the path name to the image files:
$online = '<img src="/online.png">';
should be:
$online = 'online.png';
Create a unknown status image and put it in $unknown too.
An image should be a seperate request (so, put an <img src="/yourimagescript.php"> in your html page, and in that seperate script output only the image, no html. You could embed (small) images with the data: protocol, but I strongly advise against it.

Categories