I'm new to PHP and am having trouble using session. I call a login php-script from javascript using AJAX. There I want to create the session and set a value.
<?php
ini_set('display_errors', 1);
session_start();
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
$_SESSION['username'] = "username";
?>
I handle the response in javascript and call another php-script again using AJAX. The other file looks like this:
<?php
ini_set('display_errors', 1);
session_start();
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
$username = $_SESSION['username'];
?>
But username is null. If I request the session ID in both files, the session ID changed. From the first file a Response Cookie containing a PHPSESSID is sent. Do I have to use this id in the AJAX call calling the second script?
Update: As requested the AJAX-Code:
function callAjax(url, data, successCB, errorCB) {
$.ajax({
url: url,
type: 'post',
data: data,
success: successCB,
error: errorCB
});
}
Called like:
callAjax(GET_TEMPLATES_PHP_URL, {}, onGetTemplateSuccess, onRessourceRetrievalError);
I checked the answers in similar SO question, but they did not help.
Any ideas? Thanks in advance.