phpsmfsmf-forum

SMF: Redirecting a user back to the previous page after login instead redirects them to the forum


I am making a web page which encompasses Simple Machines Forum (2.1.4), and makes use of its SSI. There are pages that I'm restricting content if you're a guest yet requires that you be logged on to view it.

So, an example page would be in this format:

<?php
require("/var/www/html/community/SSI.php");
?>
<?php
                if ($context['user']['is_logged'])
                {
                        echo'
                                <div>

<--------------------------HTML-------------------------->

</div>';
}
else
 {
 redirectexit('https://mywebsite.com/event/login.php');
}
 ?>
</body>
</html>

And if they are a guest they are redirected to my LOGIN.PHP page:

<?php
include '/var/www/html/community/SSI.php';
?>
<------------------HTML---------------------------->
            <p class="title">
                <?php ssi_welcome(); ?>
            </p>
<-------------------HTML---------------------------->
                <?php
                if ($context['user']['is_guest']) {
                    $_SESSION['login_url'] = $_SERVER['HTTP_REFERER']; 
                    ssi_login($_SESSION['login_url']); 
                } else {
                    $_SESSION['logout_url'] = 'https://mywebsite.com';
                    ssi_logout();
                }
                ?>

<-------------------------HTML------------------------------->

As you can see from the above LOGIN.PHP I attempted to record the previous page URL, then pass that URL as an argument to ssi_login(). The regular pages have no issue redirecting a user not logged in to my login page. However, the problem I'm having is that when the user logs in they are redirected to the home page of the forum as opposed to the previous page where they entered the website.

Can someone provide me some guidance regarding redirecting a user back to the previous page after login instead of redirecting them to the forum?


Solution

  • I figured out a solution by using resources found here:

    https://wiki.simplemachines.org/smf/How_to_use_the_SMF_user_system_outside_of_SMF

    https://wiki.simplemachines.org/smf/Category:Integrating_SMF

    The best way of doing this was to create a custom ssi_login() & redirect to PHP_SELF:

    <?php
        $cur_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[PHP_SELF]";
    
        global $context, $txt;
    
        if ($context['user']['is_logged']) {
            echo '<div style="text-align: right;">';
            ssi_welcome();
            echo '<br />';
            ssi_logout();
            echo '</div>';
        } else {
            ssi_login($cur_link, 'block');
    
            echo '
                <div id="login_form" class="login-form">
                    <form action="', $scripturl, '?action=login2" method="post" accept-charset="', $context['character_set'], '">
                        <label for="user">', $txt['username'], ':</label>
                        <input type="text" id="user" name="user" size="9" value="', $user_info['username'], '" class="input_text" />
                        <label for="passwrd">', $txt['password'], ':</label>
                        <input type="password" name="passwrd" id="passwrd" size="9" class="input_password" />
                        <input type="hidden" name="cookielength" value="-1" />
                        <input type="hidden" name="', $context['session_var'], '" value="', $context['session_id'], '" />
                        <input type="hidden" name="', $context['login_token_var'], '" value="', $context['login_token'], '">
                        <input type="submit" value="', $txt['login'], '" class="button_submit" />
                    </form>
                </div>';
        }
    ?>