[英]How do you make a login page with automatic redirects?
For example:例如:
When I type gmail.com
it redirects me to following link当我输入
gmail.com
它会将我重定向到以下链接
https://accounts.google.com/signin/v2/identifier?service=mail&passive=true&rm=false&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&ss=1&scc=1<mpl=default<mplcache=2&emr=1&osid=1&flowName=GlifWebSignIn&flowEntry=ServiceLogin
Then when I am done logging in, it redirects me to following link然后当我完成登录后,它会将我重定向到以下链接
https://mail.google.com/mail/u/0/#inbox
My question is how can I do this on my website.我的问题是如何在我的网站上执行此操作。 I have the following on any page where the user needs to be logged in.
我在用户需要登录的任何页面上都有以下内容。
session_start();
require_once 'dbconnect.php';
// if session is not set this will redirect to login page
if( !isset($_SESSION['user']) ) {
header("Location: index.php");
exit;
}
So how can I change this code to let the login page know where to redirect after a successful log in?那么如何更改此代码以让登录页面知道成功登录后重定向的位置?
I have the following on my login page.我的登录页面上有以下内容。
ob_start();
session_start();
require_once 'dbconnect.php';
// it will never let you open index(login) page if session is set
if ( isset($_SESSION['user'])!="" ) {
header("Location: home.php");
exit;
}
How can I change this to redirect to the page?如何更改它以重定向到页面?
First step make GET url the current page第一步使 GET url 成为当前页面
$actual_link = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if you used link make sure the link is如果您使用链接,请确保该链接是
echo '<a href="login.php?q='.$actual_link.'">Login</a>';
Make file login.php for check login制作文件 login.php 以检查登录
session_start();
require_once 'dbconnect.php';
$gotopage = $GET_["q"];
if(!isset($_SESSION['user'])) //Check if error or not isset login and redirects to index.php
{
header("Location: index.php");
}
else if(isset($_SESSION['user'])) //Check if success login and redirects to home.php
{
header("Location:".$gotopage);
}
I have found using cookies is easier.我发现使用 cookie 更容易。 I created a cookie with user address before sent to login page, then redirected them back after the completion of login page.
我在发送到登录页面之前创建了一个带有用户地址的 cookie,然后在登录页面完成后将它们重定向回来。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.