简体   繁体   中英

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.

I have tried,but I could not do anything due to lack of knowledge in 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:

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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