Problematic PHP/JavaScript

If I put the below code in a file called ip.php and reference this file on some web page as JavaScript it will spit out the following: Uncaught SyntaxError: Unexpected token <. That's from Chrome 51.0.2704.103 m. Firefox would produce SyntaxError: expected expression, got '<'.

Here is how I would've included the script on an example web page: javascript <script src="https://domain.tld/ip.js?var=ip"></script>. Alas, it doesn't work and it feels like I have tested practically everything and I still can't figure it out. Yeah, embarrassing.

Would anyone know why this occurs and how to solve it? I can't seem to figure it out. See contact details below if you think you can give me a compelling fix for this issue.

The problematic pseudo-JavaScript code:

function get_client_ip_server() {  
    $ipaddress = '';
        $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
        $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    else if($_SERVER['HTTP_X_FORWARDED'])
        $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
        $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
    else if($_SERVER['HTTP_FORWARDED'])
        $ipaddress = $_SERVER['HTTP_FORWARDED'];
    else if($_SERVER['REMOTE_ADDR'])
        $ipaddress = $_SERVER['REMOTE_ADDR'];
        $ipaddress = 'UNKNOWN';

    return $ipaddress;
<script >  
document.write('<?php echo '' . get_client_ip_server() . ''; ?>');  

I know that the code is very basic and very ugly. However, I needed to quickly put together a self-hosted alternative to As it stands now I have been unsuccessful with my attempt at creating something similar like that service and have ironically ended up simply using theirs — until I can find out how to resolve my issue.

Yes, browsing to http://domain.tld/ip.php directly works fine, as per:

<script >  

It's only a problem when I try to run it as a script, like so:

<script src="http://domain.tld/ip.php"></script>  

If you think you can help me to solve this issue please contact me via e-mail: or chat with me directly via IRC.

UPDATE: fixing the space in the <script> tag doesn't change a thing. I was almost certain this would have been the issue. But nope!