3

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.

Androidicus
  • 1,448
  • 4
  • 21
  • 33

1 Answers1

1

Thanks to user n-dru I figured it out. I called the script like "http://myside.com/script.php" from "http://www.myside.com/index.html". Because the 'www' was missing from the script-call it was a call to a different origin. So the cookie got lost. I added the www, removed the "Allow Orgin"-stuff from php and now it works.

Thanks everyone!

Androidicus
  • 1,448
  • 4
  • 21
  • 33