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;
}