0

I have 2 files "index.php" and "userip.php". I want to pass the variable varip to the file "userip.php" with ajax. If this is successful I want to share POST['name']; in a session. I thought that the session would be set but when I reload the index.php page the echo shows nothing. Can someone help me out?

index.php (jQuery section):

    <script type="text/javascript">   
    $.getJSON("http://ip.jsontest.com/", function(data) {
        var varip = "";
        $.each(data, function(k, v) {
            varip += v;
            $.ajax({
                type: "POST",
                url: "userip.php",
                data: "name="+varip,
                success: function(data){
                    alert("ok");
                }
            });
        });            
    });  
    </script>

index.php (php section):

<?php
echo $_SESSION['userip'];
?>

userip.php:

session_start();
if(!empty($_POST['name'])){ 
    $variable = $_POST['name'];
    $_SESSION['userip'] = $variable;
}
2

2 Answers 2

1

The problem is that you are missing session_start() in your index.php file, so at that point $_SESSION hasn't been loaded.

But it looks like you're getting the user's IP address?

<?php
echo $_SERVER['REMOTE_ADDR'];
Sign up to request clarification or add additional context in comments.

1 Comment

omg thats it-,- Thanks man. Im so stupid sometimes. $_SERVER['REMOTE_ADDR']; isnt working because its a proxy and all clients have the same IP. I tried this: stackoverflow.com/a/14985633/1516246 but it wasnt working. Every $_SERVER opinion output was the proxy IP
-1

change

data: "name="+varip,

to

data: { name: varip  },

Comments

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.