且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

重定向而不更改浏览器 url

更新时间:2022-05-31 22:12:11

如果您想在登录后保留页面,在用户未登录时保持相同的 URL(我们假设为 www.example.com),这很简单:

If you want to keep the page after login, to the same URL when the user was not login (we assume www.example.com), that's easy:

在 www.example.com 中它加载了 index.php,因此我们应该更改 index.php 的内容.

in www.example.com it loads the index.php, so we should change index.php's content.

没有登录的用户,第一次访问时有这个内容:

It has this content when the user who is not logged in, first visit it:

include("htmls/index.html"); // index.html could contain whatever you like

但是你加了这个条件,上面这行是在一个条件里面:

But you add this condition, the above line is inside a condition:

if(isset($_POST['form-submit']))
{
   // do the login process and if success
   include("htmls/index_loggedin.html");
}
else if(is_user_logged_in()===true)
{
  include("htmls/index_loggedin.html"); // 
}
else
{
  include("htmls/index.html"); // means the user has not submitted anything and is also not logged in

}

上面的代码说如果用户已经登录,处理他/她的登录然后包括登录用户的视图,如果用户已经登录,也显示他/她登录用户的视图,否则,如果用户未登录,也没有发送登录请求,则向他展示未登录用户的基本视图.我假设您表单的提交按钮名为表单提交".

The above code says that if the user has logged in, process his/her login and then include a view which is for logged in users, also if the user is already logged, also shows him/her the view which is for logged in users, otherwise, if the user is not logged, nor has sent no request of loggin in, then show him a basic view for unlogged users. I have assumed the submit button of your form is named "form-submit".

然而,以上命名只是为了清楚起见.例如,您可以将 index.html 和 index_loggedin.html 合并为一个 view_index.php,然后将主 index.php 的条件也复制到 view_index.php.

However the above naming is just for sake of clarity. For instance, you can combine index.html and index_loggedin.html into one view_index.php and then also duplicate the conditions of the main index.php to also the view_index.php.

最后一点是,您应该尽可能将代码和视图分开.

A last note is that, you should separate the code and the view as fully as possible.