I have created a site which has a PHP side to it this runs on port 80 (index.php), when I login it sets the session variable with the userid.
I then click a link which takes me to port 8080, this is a JavaScript file which carries out node.js processes (live.html).
From this node.js client file (live.html) I request the userid from a file called getUserSession.php which echos out the userId from the session.
When I run the AJAX request (live.html), it returns that the session userid is empty however if I manually insert the URL to the web browser it echos the user session (http://localhost:8080/getUserSession.php
).
I have dumped the array into the response and it tells me that the $_SESSION
variable is empty.
I also attempted to returned a plain text string test
- this returns with no problem, so it seems to be an issue with accessing the session variable from a different port, and not the actual AJAX call itself.
UPDATE: This is how I eventually resolved the problem.
I added this to the index.html file:
$.getJSON("`http://localhost:80/getUserSession.php?callback=?`", { username: "lazy"}, function(json){
username = json.message;
});
And added the following to the php file:
<?php
session_start();//start the session
$user2 = (string)$_SESSION['username'];
$callback = $_GET["callback"];
$user = $_GET["username"];
if($user == "lazy") {
$response = array("message" => $user2);
} else {
$response = array("message" => $user2);
}
echo $callback . "(". json_encode($response) . ");";
Still needs tidied up but it works. Thanks for the help