PHP script works fine outside of EE, but not so well within EE
Posted: 09 January 2009 12:00 AM   [ Ignore ]  
Lab Assistant
Avatar
RankRank
Total Posts:  282
Joined  06-15-2008

I’m having a strange problem with a PHP script that detects a visitor’s IP. When I call the script directly (e.g., by going to http://www.example.com/iplocation.php), it always returns the correct IP.

However, when I call the script through an EE template using the following code:

<?php
   
include("http://www.example.com/iplocation.php");
?>

I get the IP of the site’s hosting provider.

Is there anything in EE that could be causing this to happen, or is it entirely unrelated? I thought I’d ask here, since I’m asking everyone who could possibly know wink

I tried removing all of the template tags, so the include statement is the only thing in the template file. No cigar.

 Signature 

RYAN BURNEY • Lead Web Developer, 3 Roads Media
Denver based web and graphic design

Profile
 
 
Posted: 09 January 2009 07:54 AM   [ Ignore ]   [ # 1 ]  
Moderator
Avatar
RankRankRankRankRank
Total Posts:  2825
Joined  01-07-2008

You need to include the script using a server path.

What you’re doing right now is including it remotely.  In other words, PHP is making a request to the site (sort of like opening a browser) and returns the result.

 Signature 
Profile
MSG
 
 
Posted: 09 January 2009 12:13 PM   [ Ignore ]   [ # 2 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  282
Joined  06-15-2008

Pascal,

Thank you - that did the trick (almost). If you go to my EE test template, you’ll see that I’m unable to open a binary input file. I can now see the correct IP address, but the country codes (which are pulled from the BIN file) aren’t showing.

The include file looks like this:

include("IP2Location.inc.php");

$ip = IP2Location_open("IP-COUNTRY.BIN", IP2LOCATION_STANDARD);

..
code to grab IP address..

..echo
country code (e.g., US)..
..echo
country name (e.g., UNITED STATES)..
..echo
IP address..
..echo
IP number..

The BIN file is in the same directory as the location script.

I tried adding the server paths in the include file, but that didn’t work. I also checked to make sure I have the latest version of the file. Is there something else I should be doing?

Thanks Pascal.

 Signature 

RYAN BURNEY • Lead Web Developer, 3 Roads Media
Denver based web and graphic design

Profile
 
 
Posted: 09 January 2009 12:19 PM   [ Ignore ]   [ # 3 ]  
Moderator
Avatar
RankRankRankRankRank
Total Posts:  2825
Joined  01-07-2008

All the includes seem to be working fine now.

You’ll need to check the IP2Location_open function to see what code it’s using to open the bin file.

 Signature 
Profile
MSG
 
 
Posted: 09 January 2009 12:33 PM   [ Ignore ]   [ # 4 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  282
Joined  06-15-2008

Pascal,

Here is the function call again:

$ip = IP2Location_open("IP-COUNTRY.BIN", IP2LOCATION_STANDARD);

And the function:

function IP2Location_open ($filename, $flags) {
    $ip
= new IP2Location;
    
$ip->flags = $flags;
    
$ip->filehandle = fopen($filename,"rb");
    if (
$ip->filehandle === false) {
        
echo FILEHANDLE_NULL . $filename . ".\n";;
        return
0;
    
}
    $ip
= IP2Location_initialize($ip);
    return
$ip;
}
 Signature 

RYAN BURNEY • Lead Web Developer, 3 Roads Media
Denver based web and graphic design

Profile
 
 
Posted: 09 January 2009 12:57 PM   [ Ignore ]   [ # 5 ]  
Moderator
Avatar
RankRankRankRankRank
Total Posts:  2825
Joined  01-07-2008

Ok, we’re quickly moving away from EE tech support, so I’m going to move this down to How-To.

First make sure that you have the proper permissions set for the bin file.  Then also make sure it’s spelled the same (all caps?).  Lastly you could try to call “./IP-COUNTRY.BIN” (note the ./).

 Signature 
Profile
MSG
 
 
Posted: 09 January 2009 02:59 PM   [ Ignore ]   [ # 6 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  282
Joined  06-15-2008

Pascal,

Thanks again - I realize this is in the fuzzy area between EE support and PHP support, but since the script isn’t working within the EE framework I’m hoping you have another trick up your sleeve.

File permissions are 644, though I also tried 777 to be sure. Filename is all caps. BIN file is in the same folder as the script calling it, but I’ve tried all kinds of filename permutations (./IP-COUNTRY.BIN, /IP-COUNTRY.BIN, [DOCUMENT_ROOT]/IP-COUNTRY.BIN, etc.).

Again, I understand this isn’t entirely an EE issue.

 Signature 

RYAN BURNEY • Lead Web Developer, 3 Roads Media
Denver based web and graphic design

Profile
 
 
Posted: 10 January 2009 06:42 PM   [ Ignore ]   [ # 7 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  282
Joined  06-15-2008

Still no luck with this.

Is there any reason a BIN file wouldn’t load within EE but would load outside of it? The applicable code is posted above.

Basically, the IP2Location_open function above is unable to open the file within the EE template, so I get an error. When I call the same script (which loads the same BIN file) outside of EE, everything works fine.

What’s the deal?

 Signature 

RYAN BURNEY • Lead Web Developer, 3 Roads Media
Denver based web and graphic design

Profile
 
 
Posted: 13 January 2009 02:15 PM   [ Ignore ]   [ # 8 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  282
Joined  06-15-2008

I also tried changing the extension of the file to txt, but no luck. Using absolute paths also failed to do the trick.

Has anyone else had trouble using the PHP fopen function within EE?

 Signature 

RYAN BURNEY • Lead Web Developer, 3 Roads Media
Denver based web and graphic design

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1743, on December 02, 2009 03:47 PM
Total Registered Members: 120045 Total Logged-in Users: 49
Total Topics: 126169 Total Anonymous Users: 38
Total Replies: 663631 Total Guests: 371
Total Posts: 789800    
Members ( View Memberlist )