php PHP登录后如何跳转到同一个页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2532141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to go to the same page after login in PHP
提问by Fero
For example,
例如,
STEP 1:
第1步:
I am browsing a page without logging in and my last page before logging in is beforeLogin.php
我在没有登录的情况下浏览页面,登录前的最后一页是 beforeLogin.php
STEP 2:
第2步:
Now my prob is when i logged in i am redirecting to the index.php page. Instead i should be redirected to the last page what i had browsed.
现在我的问题是当我登录时我重定向到 index.php 页面。相反,我应该被重定向到我浏览过的最后一页。
That is from the above example is should redirected to beforeLogin.php
那就是从上面的例子应该重定向到 beforeLogin.php
How should this can be done.
这应该怎么做。
Any help will be appreciable.
任何帮助将是可观的。
thanks in advance
提前致谢
回答by Anthony Forloney
You would need a way to keep track of the web pages visited, so you can back-reference to the last page the user has browsed to.
您需要一种方法来跟踪访问过的网页,以便您可以反向引用用户浏览的最后一个页面。
When I think of tracking a user's sessionacross multiple-pages I, like every other PHP programmer, think of using sessions.
当我想到跨多个页面跟踪用户的会话时,我和其他 PHP 程序员一样,会想到使用session。
A possible way could be to store the link visited into a session variable and then when the user reaches the login.phppage (the page to login into) provide a headerredirect to $urlgiven by the session variable.
一种可能的方法是将访问的链接存储到会话变量中,然后当用户到达login.php页面(要登录的页面)时提供会话变量给定的header重定向$url。
NOTE: Below code snippets, have not been testedor compiled.
注意:以下代码片段尚未经过测试或编译。
You can paste this code into all your pages on your website:
您可以将此代码粘贴到您网站上的所有页面中:
<?php
session_start(); // starts the session
$_SESSION['url'] = $_SERVER['REQUEST_URI']; // i.e. "about.php"
This utilizes the $_SERVERvariables to return the URI of the current page visited using $_SERVER['REQUEST_URI']
这利用$_SERVER变量返回当前访问的页面的 URI 使用$_SERVER['REQUEST_URI']
And then for the login page to help further demonstrate:
然后对于登录页面帮助进一步演示:
<?php
session_start(); // needed for sessions.
if(isset($_SESSION['url']))
$url = $_SESSION['url']; // holds url for last page visited.
else
$url = "index.php"; // default page for
header("Location: http://example.com/$url"); // perform correct redirect.
回答by Lotus Notes
The simplest solution by far is simply to have:
到目前为止,最简单的解决方案就是拥有:
<hidden name="redirurl" value="<? echo $_SERVER['HTTP_REFERER']; ?>" />
Then redirect to that address once they log in.
然后在他们登录后重定向到该地址。
However, this is only good if you have a login box on every page. I personally do and I find it works quite well.
但是,这只有在每个页面上都有登录框时才有用。我个人是这样做的,我发现它效果很好。
回答by shashikant pandit
<?php
session_start(); // starts the session
$_SESSION['url'] = $_SERVER['REQUEST_URI']; // i.e. "about.php"
This utilizes the $_SERVERvariables to return the URI of the current page visited using $_SERVER['REQUEST_URI']
这利用$_SERVER变量返回当前访问的页面的 URI 使用$_SERVER['REQUEST_URI']
And then for the login page to help further demonstrate:
然后对于登录页面帮助进一步演示:
<?php
session_start(); // needed for sessions.
if(isset($_SESSION['url']))
$url = $_SESSION['url']; // holds url for last page visited.
else
$url = "index.php"; // default page for
header("Location: http://example.com$url"); // perform correct redirect.
回答by Your Common Sense
you can make login form action lead to the current page. that's plain and simple
您可以使登录表单操作引导到当前页面。简单明了
Say, your page looks like
说,你的页面看起来像
<?
include "auth.php"; // you need it anyway, as you want to control user's auth
//the rest of the page;
?>
<form method="POST">
login form
</form>
<?
//the rest of the page;
if (!empty($_SESSION['user_id'])) echo "Welcome registered user!";
?>
and auth.php would take care of login check and then redirect to a current page is not a big deal
和 auth.php 会负责登录检查,然后重定向到当前页面没什么大不了的
<?
if (isset($_REQUEST[session_name()])) session_start();
if (isset($_POST['auth_name'])) {
//password checking
if (pass ok) {
session_start();
$_SESSION['user_id'] = $row['id'];
}
header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
exit;
}
something like this no extra session required.
像这样不需要额外的会话。

