Javascript 如何创建带有自动重定向的登录页面?

Javascript 如何创建带有自动重定向的登录页面?,javascript,php,html,redirect,Javascript,Php,Html,Redirect,例如: 当我键入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&ltmpl=default&ltmplcache=2&emr=1&osid=

例如:

当我键入
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&ltmpl=default&ltmplcache=2&emr=1&osid=1&flowName=GlifWebSignIn&flowEntry=ServiceLogin
https://mail.google.com/mail/u/0/#inbox
当我完成登录后,它会将我重定向到下面的链接

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&ltmpl=default&ltmplcache=2&emr=1&osid=1&flowName=GlifWebSignIn&flowEntry=ServiceLogin
https://mail.google.com/mail/u/0/#inbox
我的问题是如何在我的网站上做到这一点。我在用户需要登录的任何页面上都有以下内容

 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;
 }
那么,如何更改此代码以让登录页面知道在成功登录后重定向到哪里呢

我的登录页面上有以下内容

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;
 }

如何将其更改为重定向到页面?

第一步使GET url成为当前页面

$actual_link = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
如果您使用了链接,请确保链接是

echo '<a href="login.php?q='.$actual_link.'">Login</a>';

我发现使用饼干更容易。在发送到登录页面之前,我创建了一个带有用户地址的cookie,然后在登录页面完成后将其重定向回来。

关于gmail如何做到这一点的答案在您的问题中是正确的:它将下一个要跳转到的URL编码为HTTP GET参数。这是登录URL的
&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F
部分。@PMV所以如果我将其添加到标题位置的末尾,那么在我登录后它会去哪里?所以我的问题是,是否有一个通用的解决方案可以跨多个页面工作?第55行的标题参数不带任何变量,它忽略了变量,我不知道为什么,你的问题是,是否有任何方法可以在javascript中重定向,对吗?请尝试以下
window.location.replace(“http://yoururl.com/login.php");答案中的内容正是我想要的,但我不知道为什么会忽略标题位置中的变量,并且它只重定向到引号中的内容。尝试将此
更改为
}否则{?>window.location.replace(“http://yoururl.com/home.php“”
我使用javascript重定向。
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;
 }