Last 4 hours passed with my effort to make a basic setting of a PHP session.
Here's what I have,
I have index.html that has a javascript which contains an on click function
as;
$("a.yes").on('click', function() {
$.ajax({
type: "POST",
url: "http://www.example.com/blabla/php/register.php",
data: "test=" + "test2"
})
.done(function(data){
alert($(data).filter(".meme").text());
})
.fail(function() {
alert( "Error" );
});
});
My AJAX Post handler file is (aka register.php);
<?php
session_start();
header('Access-Control-Allow-Origin: *');
include "../../includes/connect.php";
if ($_POST['test']){
$_SESSION['mysession'] = $_POST['test']
?>
<span class='meme'><?=$_SESSION['mysession']?></span>
<?php
}
?>
It alerts my posted value as test2
but not setting the session when I check it afterwards. But if it is not setting the session how it alerts test2
?
What could be wrong? PS: I tried without header('Access-Control-Allow-Origin: *');
but result is the same. I need it anyway.
UPDATE: It seems sessions for www and non www version are different... There is no problem when they are both www or non www. Solved.