altariamx2003
asked on
error with file_get_contents and unserialize
Before today I was using this function to register the visits on my site:
But today the function didnot works, checking line by line i found that the problem is in this line
I dont know whats happend and why yesterday the function was working great and now nothing works
to check whats happend i made this page: http://www.grupossc.com/index_prueba5.php
and this is the code
but nothing happpend the browser did not present the results
if you put the link (http://ip-api.com/php/216.58.192.132) on your browser, it gonna show you something like this:
a:14:{s:3:"lat";d:37.41920 08972168;s :8:"timezo ne";s:19:" America/Lo s_Angeles" ;s:6:"stat us";s:7:"s uccess";s: 7:"country ";s:13:"Un ited States";s:4:"city";s:13:"M ountain View";s:3:"org";s:6:"Googl e";s:5:"qu ery";s:14: "216.58.19 2.132";s:3 :"zip";s:5 :"94043";s :3:"lon";d :-122.0574 0356445312 ;s:2:"as"; s:19:"AS15 169 Google Inc.";s:11:"countryCode";s :2:"US";s: 6:"region" ;s:2:"CA"; s:10:"regi onName";s: 10:"Califo rnia";s:3: "isp";s:6: "Google";}
So what is happend??? why that line did not works???
Best regards
function visitas($lugar)
{
$ip = $_SERVER['REMOTE_ADDR'];
$query = @unserialize(file_get_contents('http://ip-api.com/php/'.$ip));
$host = gethostbyaddr($ip);
$agente = $_SERVER['HTTP_USER_AGENT'];
$referrer=$_SERVER['HTTP_REFERRER'];
$estado = utf8_decode($query['regionName']);
$ciudad = utf8_decode($query['city']);
if(($ip != "201.159.192.205"))
{
if((strpos($host,'Bot') === false) && (strpos($host,'bot') === false) && (strpos($host,'yse.yahoo.net') === false) && (strpos($host,'spider-100-43') === false) && (strpos($agente,'bot') === false) && (strpos($agente,'bots') === false) && (strpos($agente,'Bot') === false) && (strpos($agente,'Bots') === false) && (strpos($agente,'YandexBot') === false) && (strpos($agente,'spider') === false))
{
if (!$referrer){$referrer = $_SERVER['HTTP_REFERER']; }
$referencia = parse_url($referrer, PHP_URL_HOST);
if(($referencia == '') || ($referencia == NULL)){}
if (isset($_GET['rps']))
{ $referencia = referencias($_GET['rps']); }
else
{
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if (strpos($actual_link,"?rps=")!==false)
{
$temp = explode("?rps=",$actual_link);
$referencia = referencias($temp[1]);
}
}
if ($estado == "Mexico City") { $estado="Ciudad de México";}
elseif ($estado == "Federal District") { $estado="Ciudad de México";}
if ($ciudad == "Mexico City") { $ciudad="Ciudad de México";}
elseif ($ciudad == "Federal District") { $ciudad="Ciudad de México";}
$miarreglo = array(date('Y-m-d'), date('H:i:s'),$agente,$ip,$host,$estado,$ciudad,$referencia,$lugar,$query['country']);
$resultado = record_insert('ssc_visitas','fecha,hora,agente,ip,hostname,estado,ciudad,referencia,lugar,pais',$miarreglo,10,'el registro no pudo ser añadido');
}
}
}
But today the function didnot works, checking line by line i found that the problem is in this line
$query = @unserialize(file_get_contents('http://ip-api.com/php/'.$ip));
I dont know whats happend and why yesterday the function was working great and now nothing works
to check whats happend i made this page: http://www.grupossc.com/index_prueba5.php
and this is the code
<?php
$content = @unserialize(file_get_contents('http://ip-api.com/php/216.58.192.132'));
echo $content;
?>
but nothing happpend the browser did not present the results
if you put the link (http://ip-api.com/php/216.58.192.132) on your browser, it gonna show you something like this:
a:14:{s:3:"lat";d:37.41920
So what is happend??? why that line did not works???
Best regards
First, if you're troubleshooting something, don't use the @ symbol in your code:
@unserialize
The @ will suppress any error messages, which is the information you need if something goes wrong. You should only ever use @ when you're absolutely 100% certain that you have proper controls in place if errors happen.
Next, the serialized data begins with:
a:14
...which means that the resulting data is going to be an array with 14 elements in it. So if you try to echo an array, it's not going to show you any data. You would need to use print_r($content) instead to view a dump of the array.
@unserialize
The @ will suppress any error messages, which is the information you need if something goes wrong. You should only ever use @ when you're absolutely 100% certain that you have proper controls in place if errors happen.
Next, the serialized data begins with:
a:14
...which means that the resulting data is going to be an array with 14 elements in it. So if you try to echo an array, it's not going to show you any data. You would need to use print_r($content) instead to view a dump of the array.
Apparently, I'm too slow of a typer!
Also, no trouble found using REMOTE_ADDR with my browser - it finds me in McLean, VA. Perhaps this is a transient error on the ip-api.com service?
You probably want to remove the error control operators from your PHP scripts. They cause failures to be silent, suppressing any diagnostic information that would otherwise be written into your error logs. In my experience, it is nearly impossible to debug PHP scripts that suppress diagnostic information.
You probably want to remove the error control operators from your PHP scripts. They cause failures to be silent, suppressing any diagnostic information that would otherwise be written into your error logs. In my experience, it is nearly impossible to debug PHP scripts that suppress diagnostic information.
@Jonathan :-)
Couple of other thoughts about using file_get_contents() to read from a remote URL...
If the remote service "hangs" and does not respond, the delay time is added to your PHP script execution time. The hung condition will continue until the remote service responds, or the PHP script violates max_execution_time. In the latter case, a Fatal Error will occur. You will not be told about this Fatal Error if the error control operator is in play.
Since remote services do hang occasionally, I've found it is useful to use cURL, where you can set a timeout value. Then you're not bound to the success or failure of the remote service - your timeout can shut things down if the service takes more than a second or two to respond.
If the remote service "hangs" and does not respond, the delay time is added to your PHP script execution time. The hung condition will continue until the remote service responds, or the PHP script violates max_execution_time. In the latter case, a Fatal Error will occur. You will not be told about this Fatal Error if the error control operator is in play.
Since remote services do hang occasionally, I've found it is useful to use cURL, where you can set a timeout value. Then you're not bound to the success or failure of the remote service - your timeout can shut things down if the service takes more than a second or two to respond.
ASKER
first
I really appreciate that you use your time to answer me, you guys are awesome
Ray about your demo, i copy all your code and use it in: http://www.grupossc.com/index_prueba7.php
But it did not show anything, so i thing that the problem is on the configuration of my server, did you know what do i need to check in my php configuration to check whats happend?
I really appreciate that you use your time to answer me, you guys are awesome
Ray about your demo, i copy all your code and use it in: http://www.grupossc.com/index_prueba7.php
But it did not show anything, so i thing that the problem is on the configuration of my server, did you know what do i need to check in my php configuration to check whats happend?
Try this first, and let's see if we have a good value in REMOTE_ADDR.
<?php // demo/temp_altariamx2003.php
/**
* https://www.experts-exchange.com/questions/28950962/error-with-file-get-contents-and-unserialize.html
*/
error_reporting(E_ALL);
echo '<pre>';
$ipa = $_SERVER['REMOTE_ADDR'];
var_dump($ipa);
echo PHP_EOL;
$url = "http://ip-api.com/php/$ipa";
$ser = file_get_contents($url);
$arr = unserialize($ser);
print_r($arr);
ASKER
hi ray this is the new link: http://www.grupossc.com/index_prueba8.php
with this result: string(15) "201.159.192.205"
with this result: string(15) "201.159.192.205"
Can you please show us the PHP code for the new link? Thanks.
ASKER
<?php
/**
* https://www.experts-exchange.com/questions/28950962/error-with-file-get-contents-and-unserialize.html
*/
error_reporting(E_ALL);
echo '<pre>';
$ipa = $_SERVER['REMOTE_ADDR'];
var_dump($ipa);
echo PHP_EOL;
$url = "http://ip-api.com/php/$ipa";
$ser = file_get_contents($url);
$arr = unserialize($ser);
print_r($arr);
?>
Interesting. It works well if I address it directly. Does this work well for you, too?
http://ip-api.com/php/201.159.192.205
http://ip-api.com/php/201.159.192.205
I wonder if your script has exceeded a maximum number of calls to the API? Check this link:
http://ip-api.com/docs/
http://ip-api.com/docs/
Although this may not be relevant to the instant case of why the API is not responding, I think it's something to consider going forward. PHP serialize() data is not a standard of any sort, but JSON is. And since you can get the API response in XML or JSON, you might want to think about using JSON. One day, it will be a better choice than serialized PHP.
https://www.experts-exchange.com/articles/22519/Understanding-JSON-in-PHP-and-JavaScript-Applications.html
See also this note about unserializing a data object.
http://php.net/manual/en/function.unserialize.php#112823
https://www.experts-exchange.com/articles/22519/Understanding-JSON-in-PHP-and-JavaScript-Applications.html
See also this note about unserializing a data object.
http://php.net/manual/en/function.unserialize.php#112823
ASKER
Hi ray
I think I got it, my function is ok, yesterday I turn on a local php server to test my function and all you codes and all of them works great.
Some f"#$%"#$ck#$!"#r made something in the php service in our server
Right now the problem is that in our server the function file_get_contents cannot load plain text, , im checking wtf he did to try to solve it.
Besides I got the json function ready as alternative to the php function.
I hope you can suggest me something about this issue.
Thx a lot for your help bro
I think I got it, my function is ok, yesterday I turn on a local php server to test my function and all you codes and all of them works great.
Some f"#$%"#$ck#$!"#r made something in the php service in our server
Right now the problem is that in our server the function file_get_contents cannot load plain text, , im checking wtf he did to try to solve it.
Besides I got the json function ready as alternative to the php function.
I hope you can suggest me something about this issue.
Thx a lot for your help bro
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thx a lot for all your suggestions ray, i change to json as you suggest and as you say its a lot faster than serialize php.
one last question, im looking for whats is wrong with the php configuration of our server and i found an issue really weird
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- --
TEST WITH PLAIN TEXT
I made this page: http://www.grupossc.com/test_plaintext.php with this code:
and with this test the browser give me this error:
"Warning: file_get_contents(http://ip-api.com/php/208.80.152.201): failed to open stream: Connection timed out in /home/grupoc/public_html/t est_plaint ext.php on line 6"
I ran the same test in my local server (i use wampserver) and everything works great, and it show me the information of the page
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- --
TEST WITH FORMAT
I made this page: http://www.grupossc.com/test_withformat.php with this code:
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- --
As you see the problem is only with plain text, but only in online server, if I ran the test with plain text(http://www.grupossc.com/test_plaintext.php) in my local server everthing works ok.
besides the filesystem configuration what else do I need to check to find why this problem is happening???
or It will be better if I reset my php configuration to the initial settings???
best regards
one last question, im looking for whats is wrong with the php configuration of our server and i found an issue really weird
--------------------------
TEST WITH PLAIN TEXT
I made this page: http://www.grupossc.com/test_plaintext.php with this code:
<?php
ini_set("display_errors", 1);
error_reporting(E_ALL);
$url = "http://ip-api.com/php/208.80.152.201";
$ser = file_get_contents($url);
print_r($ser);
?>
and with this test the browser give me this error:
"Warning: file_get_contents(http://ip-api.com/php/208.80.152.201): failed to open stream: Connection timed out in /home/grupoc/public_html/t
I ran the same test in my local server (i use wampserver) and everything works great, and it show me the information of the page
--------------------------
TEST WITH FORMAT
I made this page: http://www.grupossc.com/test_withformat.php with this code:
<?php
ini_set("display_errors", 1);
error_reporting(E_ALL);
$url = "http://www.google.com";
$ser = file_get_contents($url);
print_r($ser);
?>
It shows me the information of the page without problems.--------------------------
As you see the problem is only with plain text, but only in online server, if I ran the test with plain text(http://www.grupossc.com/test_plaintext.php) in my local server everthing works ok.
besides the filesystem configuration what else do I need to check to find why this problem is happening???
or It will be better if I reset my php configuration to the initial settings???
best regards
I get instantaneous response from http://ip-api.com/php/208.80.152.201
If the connection is timing out when you use it, you might first try to unban your IP, according to this page: http://ip-api.com/docs/ I do not think the problem is on your end. I think the API has been counting your IP address' requests and has concluded that your IP address may be doing over 150 requests per minute.
If the connection is timing out when you use it, you might first try to unban your IP, according to this page: http://ip-api.com/docs/ I do not think the problem is on your end. I think the API has been counting your IP address' requests and has concluded that your IP address may be doing over 150 requests per minute.
ASKER
i check if my ip is banned but is not ray, my ip is ok
ASKER
thx a lot for your help ray,
yesterday i reset my php settings and all works great again
yesterday i reset my php settings and all works great again
ASKER
Ray help me alot with his help
Glad to hear it's working now! Thanks for the points and thanks for using E-E, ~Ray
No trouble found with this excerpt.
https://iconoun.com/demo/temp_altariamx2003.php
Open in new window
Outputs:Open in new window