Search code examples
phpjqueryajaxforumphpbb

PHPBB Ajax External Login


I'm having some issues trying to get an external login for PHPBB to work with AJAX. I'm not really sure what I'm missing here, so here's what I got:

HTML:

<div id="dialogin" class="dialoghide" title="Loading...">
    <form id="loginformadj" name="loginformadj" action="forum/ucp.php?mode=login" method="post" data-ajax="true" data-refresh="false">
        <h3><a id="dialoglogin" href="forum/ucp.php?mode=login" data-ajax="true">Login</a>&nbsp; &bull; &nbsp;
        <a id="dialogregister" href="forum/ucp.php?mode=register" data-ajax="true">Register</a></h3>
        <fieldset id="userinfo">
            <label for="username">Username:</label>&nbsp;
            <input type="text" name="username" id="username" size="10" title="Username" />
            <label for="password">Password:</label>&nbsp;
            <input type="password" name="password" id="password" size="10" title="Password" />
            <label for="autologin">Log me on automatically<input type="checkbox" name="autologin" id="autologin" /></label>
        </fieldset>
        <fieldset class="submit-buttons">
            <input type="submit" name="login" value="Login" />
            <input type="hidden" name="redirect" value="./index.php" />
        </fieldset>
    </form>
</div>

JS included on page:

$(document).ready(function() {
    // handle the login form through ajax for phpbb
    $('#loginformadj').on('submit', 'form', function(event) {
        event.preventDefault();

        var url= $(this).attr('action');
        alert(url);
        $.ajax({
            url: url,
            type: 'POST',
            dataType: 'HTML',
            data: new FormData(this),
            processData: false,
            contentType: false,
            success: function (data, status) {
                alert(data);
            },
            error: function (xhr, desc, err) {
                console.log('error');
            }
        });
    });
});

And this is required at the top of the page from another file where I use phpbb sessions and other functions:

<?php
/*
 * BEGIN PHPBB STANDARD PAGE SETUP
 */
define('IN_PHPBB', true);
$phpbb_root_path = './forum/';
$website_root_path = './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);

include($phpbb_root_path . 'common.' . $phpEx);
//include($phpbb_root_path . 'includes/functions.' . $phpEx);
include($phpbb_root_path . 'includes/functions_module.' . $phpEx);
include($phpbb_root_path . 'includes/functions_user.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();
/*
 * END PHPBB STANDARD PAGE SETUP
 */
?>

The form is pulled into a jquery dialog box. It does in fact log me in, then it immediately redirects me to the forum index at ./forum instead of using the ajax and sending the alerts. I don't get any of the alerts I've set or anything. I've tried everything I can think of. :/

To add, I have the username, user avatar, and can logout all on my external page as well. Just can't get the login to work at all.


Solution

  • The problem, at least initially, was I was calling the forum submission check for jquery wrong.

    $(document).ready(function() {
    // handle the login form through ajax for phpbb
    $(document).on('submit', '#login', function(event) {
        event.preventDefault();
    
        var url= $(this).attr('action');
        alert(url);
        $.ajax({
            url: url,
            type: 'POST',
            dataType: 'HTML',
            data: new FormData(this),
            processData: false,
            contentType: false,
            success: function (data, status) {
                alert(data);
            },
            error: function (xhr, desc, err) {
                console.log('error');
            }
        });
    });
    });
    

    I also read that you need the sid called with $user->data in a hidden field. I included that in the form and didn't check if it mattered or not.