简体   繁体   English

重定向而不更改浏览器网址

[英]Redirect without changing the browser url

I have the following url, 我有以下网址,

www.example.com/home.php and
 www.example.com/login.php

When ever I redirect to anyof these pages from php , the url of the browser should remain www.example.com. 每当我从php重定向到这些页面中的任何一个时,浏览器的URL都应该保持www.example.com。

I have tried,but I could not do anything due to lack of knowledge in mod_rewite. 我已经尝试过,但是由于对mod_rewite的了解不足,我无法做任何事情。

Please help, thanks in advance 请帮助,在此先感谢

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: 如果要在登录后将页面保留为用户未登录时的相同URL(我们假设为www.example.com),这很容易:

in www.example.com it loads the index.php, so we should change index.php's content. 在www.example.com中,它会加载index.php,因此我们应该更改index.php的内容。

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". 我假设您表单的提交按钮名为“表单提交”。

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. 例如,您可以将index.html和index_loggedin.html合并为一个view_index.php,然后将主index.php的条件也复制到view_index.php。

A last note is that, you should separate the code and the view as fully as possible. 最后要注意的是,您应该将代码和视图尽可能地分开。

You can't do that with mod_rewrite. 您无法使用mod_rewrite做到这一点。 How should Apache know if you want home.php or login.php? Apache应该如何知道您要home.php还是login.php? You probably want to use AJAX to load the files in the background and then display the content. 您可能要使用AJAX在后台加载文件,然后显示内容。

You should use iframe html tag in page layout for the url of the browser remain www.example.com, when user navigates between pages. 当用户在页面之间导航时,应在页面布局中使用iframe html标记,以使浏览器的网址仍为www.example.com。 not redirect nor rewrite are not suitable for these purposes. 不重定向也不重写不适合这些目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM