You have to call session_start()
before you start assigning variable sessions.
PHP SESSION from jQuery $.post
문제
I have read plenty of articles, both here and elsewhere, regarding this issue without finding an answer that could help me. And since the subject seem to be rather broad, I presume the best way is to ask.
I am currently sending login-data from a form to a php-script through a $.post-script in a jquery-file. No problems there. I have a class that walks through a chain of function to validate and secure the strings, so no issue at all. I get all the way to setting the session, which I can print out and get by adding function(data) to my $.post.
I have added session_start() to both the php-script and to the index.php. I'm using Chromium and can see that a session has been set, but when I make a print_r($_SESSION); it comes out empty. Which also, evidently, leads to my if(isset($_SESSION['uid'])) won't work either.
js:
$('#login').submit(function(e){
e.preventDefault();
values = {};
var url = '../admin/php/signIn.php';
console.log('Clicked on submit.');
$.each($(this).serializeArray(), function(i, field){
values[field.name] = field.value;
});
$.post(url, {username: values.username, password: values.password})
.done(function(data){ location.reload(); /*alert(data);*/ })
.fail(function(){ alert('Fail.'); });
});
php:
private function login($uid, $uname){
$_SESSION['uid'] = $uid;
echo $_SESSION['username'] = $uname;
session_start();
}
I added an echo on one of the sessions, just to be able to see it through jquery. Is the issue with "private function [...]", or elsewhere? (Playing with classes, as I said before.)
I hope this makes sense to someone, since I am rather lost here.
해결책