/    Sign up×
Community /Pin to ProfileBookmark

PHP error when dealing with classes

I am treading into some new territory working with API’s and classes. I have worked some with including files so I understand kind of how that works.

I don’t really understand how API’s and classes work. I downloaded the API class file for an ip geolocation so I can see where the people who come to my site are from but I have run into a programming problem.

I am logging the ip addresses in a MySQL database with some basic PHP, then I have a button on my control page that shows all the ip addressds. What I am trying to do is display the ip address and next to it the country.

I developed and tested a basic function using the API and thought I would be easy to implement with my current code that just loops through my database displaying the ip addresses. When i tried this i the following error:

Fatal error: Cannot redeclare class ip2location_lite

and the code that produced the error:

[code]
<html>
<head>
<link rel=”stylesheet” type=”text/css” href=”/index_files/format.css” />
<title>SZ</title>
</head>

<body>

<div id=”wrapper”>
<div id=”header”>
<h1>SZ</h1>
</div>

<div id=”main”>

<div id=”content”>
<table id=”table” >
<tr>
<th>Post Number</th>
<th>Title</th>
</tr>
</table>

<?php
//declaration of variables
$con = mysql_connect(“localhost”,”username”,”password”) or die(“Connection Lost”);
$db = “database”;
$table = “table”;

//function to list entries
mysql_select_db($db, $con);
$sql = “SELECT * FROM $table”;
$result = mysql_query($sql) or die(“List Entries Error:” . mysql_error());

function ip_locate($ip)
{
include(‘path_to_class/ip2locationlite.class.php’);
$ipLite = new ip2location_lite;
$ipLite->setKey(‘personal_key’);

$locations = $ipLite->getCity($ip);
$errors = $ipLite->getError();

if(!empty($locations) && is_array($locations))
{
foreach($locations as $field => $val)
{
$field . ” : ” . $val . “<br />”;
}
}
}

while($row = mysql_fetch_array($result))
{
$number = $row[‘Number’];
$ip = $row[‘ip_address’];
$date = date(“F d Y”, $row[‘timestamp’]);

?>

<table id=”table” >
<tr>
<td id=”post_no”><?php echo $number; ?></td>
<td id=”posttitle”><?php echo $ip; ?></td>
<td id=”ipaddress”><?php echo $date; ?></td>
<td id=”location”><?php echo ip_locate($ip); ?></td>
</tr>
</table>

<?php
}
?>
</div>
</div>
</div>

</body>
</html>
[/code]

i get that i am calling the function multiple times and that redeclares the class but i don’t know how else to make it work. i tried taking the class declaration out of the loop but then it would give other errors.

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogAug 12.2011 — Use include_once() instead of include() (or possibly better would be require_once(), since it's pretty much mandatory for the following code to work).
Copy linkTweet thisAlerts:
@spotted_zebraauthorAug 12.2011 — thanks that worked, i used the require_once.

i had tried that already but i thought i was messing something up because it was taking forever for the page to load, like 1.5 min. The API must be really slow or something.

Do you have any tips to speed up that page or am I stuck?
Copy linkTweet thisAlerts:
@ScottyBoyAug 13.2011 — The speed issue might be caused by a loop that doesn't wanna stop running. A minute and a half for a page load is crazy.
Copy linkTweet thisAlerts:
@NogDogAug 13.2011 — Or a loop that makes HTTP or cURL requests, so you're waiting for internet lag on each iteration of the loop?
Copy linkTweet thisAlerts:
@spotted_zebraauthorAug 14.2011 — If I had to guess this is the problem but I don't really know how to resolve it. The API uses this function to get information for it's database and if i had to guess this is the HTTP request you were talking about:

<i>
</i>
private function getResult($host, $name){
$ip = @gethostbyname($host);

<i> </i> if(preg_match('/^(?:25[0-5]|2[0-4]d|1dd|[1-9]d|d)(?:[.](?:25[0-5]|2[0-4]d|1dd|[1-9]d|d)){3}$/', $ip)){
<i> </i> $xml = @file_get_contents('http://' . $this-&gt;service . '/' . $this-&gt;version . '/' . $name . '/?key=' . $this-&gt;apiKey . '&amp;ip=' . $ip . '&amp;format=xml');

<i> </i> try{
<i> </i> $response = @new SimpleXMLElement($xml);

<i> </i> foreach($response as $field=&gt;$value){
<i> </i> $result[(string)$field] = (string)$value;
<i> </i> }

<i> </i> return $result;
<i> </i> }
<i> </i> catch(Exception $e){
<i> </i> $this-&gt;errors[] = $e-&gt;getMessage();
<i> </i> return;
<i> </i> }
<i> </i> }


I don't load this page all that often and none of the users of the site can access it, so it is not a huge deal expect for me waiting.
×

Success!

Help @spotted_zebra spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.8,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...