Redirect without changing the browser url

2019-06-09 23:16发布

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.

I have tried,but I could not do anything due to lack of knowledge in mod_rewite.

Please help, thanks in advance

3条回答
beautiful°
2楼-- · 2019-06-09 23:57

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:

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".

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.

查看更多
叼着烟拽天下
3楼-- · 2019-06-10 00:03

You can't do that with mod_rewrite. How should Apache know if you want home.php or login.php? You probably want to use AJAX to load the files in the background and then display the content.

查看更多
forever°为你锁心
4楼-- · 2019-06-10 00:17

You should use iframe html tag in page layout for the url of the browser remain www.example.com, when user navigates between pages. not redirect nor rewrite are not suitable for these purposes.

查看更多
登录 后发表回答