1

I am using JQuery Ajax (and I am sure that things have changed since the last time I used it) but I am having trouble pulling the information from the PHP variable. Basically I am getting the IP address and logging how long it took that IP to load the page fully and then display it.

Here is my code...

getIP.php

<?php
    if (!empty($_SERVER['HTTP_CLIENT_IP']))
    {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
    {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else 
    {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    echo json_encode(array('ip' => $ip));
?>

Event listener that calls it

var IPAddresses = [];

//Anonymous functions - used for quality control and logging

(function() { //Used to test how long it took for a user to connect - uses a php script with it
    window.addEventListener("load", function() {
        $.ajax({
            url: '../php/getIP.php',
            type: 'POST',
            success: function(result) 
            {
                setTimeout(function alertUser(){IPAddresses.push(result.ip);}, 40);
            }
        });
    }, false);
})();
(function() {
    window.addEventListener("load", function() {
        setTimeout(function() {
            for (var i = 0; i < IPAddresses.length; i++)
            {
                var timing = performance.timing;
                console.log(IPAddresses[i] + " " + timing.loadEventEnd - timing.responseEnd);
            }
        }, 0);
    }, false);
})();

EDIT

Now I don't get errors but it does not seem to print the IP address or push it into the array at all. I am basically trying to get it to [ip] [loadtime] It gives a NaN error

2 Answers 2

3

Your output is a string:

echo $ip; //Just to check if it worked - it shows the IP
      ^---e.g. 127.0.0.1

and then you try to treat it as an array:

setTimeout(function alertUser(){alert(result['ip']);}, 40);
                                            ^^^^^^

Since it's not an array, this won't work. try just alert(result).

Sign up to request clarification or add additional context in comments.

1 Comment

Worked like a charm. I usually use arrays so apologies. In 10 minutes I will accept the answer.
0

try to use "json_encode"

echo json_encode(array('ip' => $ip));

and in ajax

   success: function(result) 
        {
            setTimeout(function alertUser(){alert(result.ip);}, 40);
        }

6 Comments

return json_encode(array('ip' => $ip)); won't work. You mean echo json_encode(array('ip' => $ip));
That converts in into a string yes?
yes but if you want an array in php and read as array you need more parameters
So what if I use a JavaScript array and use push? Will it then store it as a string or an integer as it has been doing?
Edited my question take a look
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.