登录PHP后如何进入同一页面 [英] how to go to the same page after login in PHP
问题描述
例如
步骤1:
我正在浏览的页面没有登录,登录前的最后一页是beforeLogin.php
I am browsing a page without logging in and my last page before logging in is beforeLogin.php
步骤2:
现在我的问题是当我登录时将重定向到index.php页面.相反,我应该重定向到我浏览过的最后一页.
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.
这是从上面的示例应重定向到beforeLogin.php
That is from the above example is should redirected to beforeLogin.php
这应该怎么做.
任何帮助都是有意义的.
Any help will be appreciable.
预先感谢
推荐答案
您将需要一种跟踪所访问网页的方法,以便可以向后引用用户浏览过的最后一页.
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.
当我想到跨多个页面跟踪用户的 session 时,我像其他所有PHP程序员一样,想到使用
When I think of tracking a user's session across multiple-pages I, like every other PHP programmer, think of using sessions.
一种可能的方法是将访问的链接存储到会话变量中,然后当用户到达login.php
页面(要登录的页面)时,提供
A possible way could be to store the link visited into a session variable and then when the user reaches the login.php
page (the page to login into) provide a header
redirect to $url
given by the session variable.
您可以将此代码粘贴到网站上的所有页面中:
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"
这利用 $_SERVER
变量返回使用$_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.
这篇关于登录PHP后如何进入同一页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!