SMF: 登录后将用户重定向回上一页,而不是重定向到论坛。

huangapple go评论121阅读模式
英文:

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

问题

我正在制作一个包含Simple Machines Forum(2.1.4)的网页,并使用它的SSI。有一些页面,如果你是访客,我会限制内容,但要求你登录才能查看。

所以,一个示例页面的格式如下:

<?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>

如果他们是访客,他们将被重定向到我的LOGIN.PHP页面:

<?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------------------------------->

从上面的LOGIN.PHP中可以看出,我尝试记录先前的页面URL,然后将该URL作为参数传递给ssi_login()。常规页面没有问题,可以将未登录用户重定向到我的登录页面。但我遇到的问题是,当用户登录时,他们被重定向到论坛的主页,而不是他们进入网站的先前页面。

是否有人能为我提供一些关于在登录后将用户重定向回先前页面而不是将他们重定向到论坛的指导?

英文:

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?

答案1

得分: 1

我通过使用在这里找到的资源找到了解决方案:

[https://wiki.simplemachines.org/smf/How_to_use_the_SMF_user_system_outside_of_SMF][1]

[https://wiki.simplemachines.org/smf/Category:Integrating_SMF][2]

这样做的最佳方法是创建一个自定义的ssi_login()并重定向到PHP_SELF:

```php
<?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>';
    }
?>

<details>
<summary>英文:</summary>
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][1]
[https://wiki.simplemachines.org/smf/Category:Integrating_SMF][2]
The best way of doing this was to create a custom ssi_login() &amp; redirect to PHP_SELF:
```php
&lt;?php
$cur_link = (isset($_SERVER[&#39;HTTPS&#39;]) &amp;&amp; $_SERVER[&#39;HTTPS&#39;] === &#39;on&#39; ? &quot;https&quot; : &quot;http&quot;) . &quot;://$_SERVER[HTTP_HOST]$_SERVER[PHP_SELF]&quot;;
global $context, $txt;
if ($context[&#39;user&#39;][&#39;is_logged&#39;]) {
echo &#39;&lt;div style=&quot;text-align: right;&quot;&gt;&#39;;
ssi_welcome();
echo &#39;&lt;br /&gt;&#39;;
ssi_logout();
echo &#39;&lt;/div&gt;&#39;;
} else {
ssi_login($cur_link, &#39;block&#39;);
echo &#39;
&lt;div id=&quot;login_form&quot; class=&quot;login-form&quot;&gt;
&lt;form action=&quot;&#39;, $scripturl, &#39;?action=login2&quot; method=&quot;post&quot; accept-charset=&quot;&#39;, $context[&#39;character_set&#39;], &#39;&quot;&gt;
&lt;label for=&quot;user&quot;&gt;&#39;, $txt[&#39;username&#39;], &#39;:&lt;/label&gt;
&lt;input type=&quot;text&quot; id=&quot;user&quot; name=&quot;user&quot; size=&quot;9&quot; value=&quot;&#39;, $user_info[&#39;username&#39;], &#39;&quot; class=&quot;input_text&quot; /&gt;
&lt;label for=&quot;passwrd&quot;&gt;&#39;, $txt[&#39;password&#39;], &#39;:&lt;/label&gt;
&lt;input type=&quot;password&quot; name=&quot;passwrd&quot; id=&quot;passwrd&quot; size=&quot;9&quot; class=&quot;input_password&quot; /&gt;
&lt;input type=&quot;hidden&quot; name=&quot;cookielength&quot; value=&quot;-1&quot; /&gt;
&lt;input type=&quot;hidden&quot; name=&quot;&#39;, $context[&#39;session_var&#39;], &#39;&quot; value=&quot;&#39;, $context[&#39;session_id&#39;], &#39;&quot; /&gt;
&lt;input type=&quot;hidden&quot; name=&quot;&#39;, $context[&#39;login_token_var&#39;], &#39;&quot; value=&quot;&#39;, $context[&#39;login_token&#39;], &#39;&quot;&gt;
&lt;input type=&quot;submit&quot; value=&quot;&#39;, $txt[&#39;login&#39;], &#39;&quot; class=&quot;button_submit&quot; /&gt;
&lt;/form&gt;
&lt;/div&gt;&#39;;
}
?&gt;

huangapple
  • 本文由 发表于 2023年6月19日 06:43:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76502787.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定