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;
}
Widor
  • 13,003
  • 7
  • 42
  • 64
Rene
  • 11
  • 3

2 Answers2

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'];
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • 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: http://stackoverflow.com/a/14985633/1516246 but it wasnt working. Every $_SERVER opinion output was the proxy IP – Rene Apr 10 '14 at 01:06
-1

change

data: "name="+varip,

to

data: { name: varip  },
Joseph118
  • 505
  • 6
  • 21