Ajax and session

Kent Miller

I have a create_event.php page. After having filled out the event details, the user has the possibility to login. After a successful login, I want the user to read 'Hello $username, you are logged-in now' on the same page (without reloading).

For the login process I use the following Ajax script:

$.ajax({
                type : "POST",
                url : "system/process_login.php",
                data : query,
                cache : false,
                success : function(response) {
                    if (response === 'error') {
                        $('.feedback_message').text('Connection to database failed.');
                    };
                    if (response === 'failed') {
                        $('.feedback_message').text('Wrong login details.');
                    }
                    if (response === 'success') {
                        $('#step_login').text('Login successful!');
                    }
                }
            });

I am wondering how it is possible to initialize a session when the ajax login process is successful. In my process_login.php I write the following:

 session_start();

require_once 'db.php';

// Get values sent via ajax and prepare them for database
$username = $db -> real_escape_string($_REQUEST['username']);
$password = $db -> real_escape_string($_REQUEST['password']);

// Hash password
$password_hashed = sha1($password);


// Check if login information is correct
$query = "SELECT * FROM users WHERE username='$username' AND password='$password_hashed'";
$results = $db -> query($query);
if ($db -> error) {
    $response = 'error';
    echo $response;
}
if (mysqli_num_rows($results) > 0) {
    $_SESSION['username'] = $username;
    $response = 'success';
    echo $response;
} else {
    $response = 'failed';
    echo $response;
}

?>

My question: When the ajax login process is successful, will the $_SESSION['username'] variable be available? How can I retrieve it? In the top corner of the same page, i.e. my create_event.php, I want the user to read "Hello $username, you are logged-in now!"...

guest271314

Following comments above (untested)

php

if (mysqli_num_rows($results) > 0) {
    $user = $_SESSION['username']; // or, other method(s) of deriving accurate `username` / `user`
    $response = $user;
    echo $response;
}

js

success : function(response) {
                    if (response === 'error') {
                        $('.feedback_message').text('Connection to database failed.');
                    };
                    if (response === 'failed') {
                        $('.feedback_message').text('Wrong login details.');
                    }
                    if (response != "error" && response != "failed") {
                        $('#step_login').text("Hello" + response + ", you are logged-in now!");
                    }
                }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related