PDA

View Full Version : Cant get user's IP address when calling a script from a plugin


cronjob78
12-03-2019, 03:06 PM
I have a plugin that displays events from another part of my website unrelated to vBulletin. I want to write each information about each time an event is displayed to a database table and include the user's IP address in the data.

I'm adding the plugin as "global_start" and calling a variable called $show_event in my template files. In the plugin code I assign $show_event = implode('', 'www.mywebsite.com/forums/event.php');

$_SERVER['REMOTE_ADDR'] gives me my server's IP rather than the user's IP because of the way vBulletin calls up the plugins. I need the user's IP.

I note that plugins using "style_fetch" as the hook location use $_SERVER['REMOTE_ADDR'] successfully, but I have to use "global_start".

I can't access any global variables from event.php. If anyone can suggest anything I'd appreciate it.

Dave
12-03-2019, 03:12 PM
If the script is visited as expect in a browser then the proper IP should always be present in $_SERVER['REMOTE_ADDR'], unless you use some sort of proxy. Can you access the IPADDRESS or ALT_IP constants?

cronjob78
12-05-2019, 10:08 AM
Thanks for the reply Dave. I have solved the problem.


For years, using this code in the plugin window, had worked fine for me:

$var = implode('', file('path/to/this/file/myfile.php1'));

and I then included $var in my template, where I wanted the content of my PHP file to appear.
However, when you try to access the $_SERVER['HTTP_USER_AGENT'] in that PHP file it will return the results of the server's IP rather than that of the client IP.

However, if you use this code for your plugin instead:

ob_start();
include('path/to/this/file/myfile.php');
$includedphp = ob_get_contents();
ob_end_clean();

$_SERVER['HTTP_USER_AGENT'] will return the user's IP correctly.

Dave
12-05-2019, 10:12 AM
Ah that makes more sense. Yes, you'd have to do it like that.