6

I'm trying to build my AJAX login system but I'm having some problems with PHP sessions.

This is the AJAX code I use in my index.php:

$("#buttonLogin").click(function(){
    $.post("<?php echo $AJAX ?>/ajaxLogin.php",{
        Username : $("#loginUsername").val(),
        Password : $("#loginPassword").val()
    }, 
    function(result){
        if(result == "OK"){
            window.location.href = "<?php echo $PUBLIC?>/home.php";
        } else {
            $("#loginMessageError").show();
        }
    });
});

And this is ajaxLogin.php that is called via AJAX:

<?php
require_once("../settings.php");
require_once($ABS_ENGINE."/classUser.php");

$user = new User();
if($user->loginUser($_POST["Username"], $_POST["Password"])){
    $UserID = $user->getUserId($_POST["Username"]);
    session_start();
    $_SESSION['UserID'] = $UserID;
    echo "OK";
} else {
    echo "ERROR";
}
?>

When I'm in home.php and I try to echo $_SESSION["UserID"], I get the following error:

Notice: Undefined index: UserID in C:\xampp\htdocs\webname\resources\templates\headerHome.php on line 23

Probably this is not correct because session must be set before any output but if i try to echo $_SESSION['UserID'] = $UserID; line it's session variable is correctly displayed.

7
  • The only other explanation is that multiple session are being started. Check session_id() of both pages to verify this Commented Jan 1, 2012 at 17:35
  • Are you sure your php.ini is set to use session cookies and the path is an accessible one by PHP? There could be a chance that the session saving path is wrong for your setup. Commented Jan 1, 2012 at 18:01
  • @Paul Can you tell me how php.ini should be changed to allow sessions? Commented Jan 1, 2012 at 18:12
  • @MaterMorbi php.net/manual/en/session.configuration.php There you go, set those up according to what you need, read the docs and you'll figure them out. Also read the comments in php.ini at the session section. Commented Jan 1, 2012 at 18:31
  • @MaterMorbi, have you checked the session_id() of the two pages.. Commented Jan 2, 2012 at 5:38

3 Answers 3

6

You need to initiate the session first, like session_start().then only you can have the access to session variables. Have a look at this simple example , it might help you:

aj.php

<script src="jquery.js"></script>
<script type="text/javascript">
$(document).ready( function(){
    $.ajax({
        type : 'GET',
        url : 'sess.php',
        data: {
            user : 'guna',

              },
        success : function(data){
                       alert(data);
        },
        error : function(XMLHttpRequest, textStatus, errorThrown) 
        {alert ("Error Occured");}
                 });


});
</script>
</html>

sess.php

<?php
session_start();
$_SESSION['user']=$_GET['user'];
echo $_SESSION['user'];
?>  

As other guys pointed out, better you can also check for session_start() in the page where you reading the session variables.

Sign up to request clarification or add additional context in comments.

Comments

4

When I had this kind of problem, the thing that solved it was using exit();

<?php
require_once("../settings.php");
require_once($ABS_ENGINE."/classUser.php");

$user = new User();
if($user->loginUser($_POST["Username"], $_POST["Password"])){
    $UserID = $user->getUserId($_POST["Username"]);
    session_start();
    $_SESSION['UserID'] = $UserID;
    echo "OK";
    exit();
} else {
    echo "ERROR";
}
?>

1 Comment

Yup, this worked for me too, loosely. If you're using Drupal with a non-core delivery callback, call drupal_session_commit(); in your delivery callback.
0

Better check if session_start() is present in home.php. Without this you will not be able to read the session data.

When you are doing echo $_SESSION['UserID'] = $UserID; you will assigning and accessing at a same line, so it will obviously work.

1 Comment

session_start() is present in home.php

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.