英文:
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() & redirect to 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>';
}
?>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论