mackeemackee mackeemackee - 7 days ago 4x
Ajax Question

Checking OOP PHP $_SESSION with Jquery Ajax

Im doing a project in school, and i need to control that the user has the right to access one of the pages. All functions need to go trough Ajax.

My problem is with the callback for the Ajax. Struggled for a couple of days, but cant seem to fix it. If i skip the Ajax, it works. But i need to get it to work with ajax. I dont know what to use as a callback, i've tried alot of solutions, none working.

What happens is that anyone can reach the page, no matter Session.


$(window).load( function checkUser() {
$(). html(data); // I think the problem is here?????


include ("php-classes/classUsers.php");
// Calling the class User and controlling that the visitor is logged in
$user = new User();
if($user->loggedIn()) {

PHP class

public function loggedIn() {
// Control, accessed page via login page.
if(isset($_SESSION["username"]) && $_SESSION["username"] != null) {
} else { header("location:index.php");
} }


You can't set header in a AJAX request. What you can do is send a logged in or request status in response to AJAX request. You can use JSON string to return the response and validate the same on client side.

If the user is logged in send the JSON like this,


If the user is not logged send the JSON like this,


Now check the value of status on client side to know if the user is logged in or not. You can also send HTML content along with status field to show it on webpage if the user is already logged in. Refresh or redirect the page to index.php in case the status field is you get from script is 0.