简体   繁体   English

登录后如何使用户停留在同一页面上

[英]How to make a user stay on same page once they log in

I am having an issue on how to make it where users who are viewing any page can log in on the page they are viewing and it stays on that page. 我在如何使正在查看任何页面的用户可以登录他们正在查看的页面并停留在该页面上的问题上遇到了一个问题。 How would this be accomplished? 这将如何实现?

Below is a single line I am currently using, however, if on any page and a user logs in, they are redirected to their profile. 下面是我当前正在使用的一行,但是,如果在任何页面上并且用户登录,他们将被重定向到其个人资料。 How can I set this line where it logs the user in, and it stays on that same page they are viewing? 如何在用户登录所在的位置设置此行,并使它停留在他们正在查看的同一页面上? So in other words, are not redirected to their profile... 所以换句话说,不会重定向到他们的个人资料...

PHP: PHP:

header("Location: members.php?id=" . $_SESSION['username']);

If more info is needed, let me know and I can make an edit ;) 如果需要更多信息,请告诉我,我可以进行编辑;)

Have the login form submit the address of the current page. 让登录表单提交当前页面的地址。 Then you can simply redirect back to that address when the login succeeds, eg 然后,您可以在登录成功后直接重定向回该地址,例如

<form>
   <input type="hidden" name="curpage" value="<?php echo htmlspecialchars($_SERVER['PHP_SELF']) ?>" />
   <input type="text" name="username" />
   <input type="password" name="password" />
   <input type="submit" />
</form>

if ($login_is_successful) {
   header("Location: {$_POST['curpage']}");
}

You could try using the referer, but since that's not sent by all browser, and is not always accurate, you're better off sing alternate "current location" tracking means, such as the above hidden form field. 您可以尝试使用引荐来源网址,但是由于并非所有浏览器都发送该引荐来源网址,而且并非始终准确,因此最好使用替代的“当前位置”跟踪方式,例如上面的隐藏表单字段。

When they click on the login button you can read the url and save it to a varibale and redirect them to this url. 当他们单击登录按钮时,您可以阅读该URL并将其保存到变量,然后将其重定向到该URL。

So instead of 所以代替

header("Location: members.php?id=" . $_SESSION['username']);

you can use sth. 您可以使用某物。 like: 喜欢:

 header("Location: $last_page);

Try this you can create a php file with this code and include into your code like this session.php 试试这个,您可以使用此代码创建一个php文件,并将其包含在您的代码中,例如session.php

<?php
session_start();
error_reporting(0);
if(isset($_SESSION["usuario"]))
{
$usuario = $_SESSION["usuario"];
else{
header('Location: members.php?id=" . $_SESSION['username']');
}
?>

index.php index.php

<?php
include ('session.php'); 
?>

to avoid using same code in every page 避免在每个页面中使用相同的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM