Redirect without changing the browser url - php

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.

Related

Best way to redirect user upon form submit to URL using input as directory

Hello! I am trying to put together a landing page that will allow individuals to visit, enter an access code, and be redirected to a directory that corresponds to the access code. For example, access code is 12345, user is redirected to example.com/12345 upon submit. We will be using direct links for the most part, but in the event that someone hits a 404 or try to visit the root directory, we want to have an interface for returning to the project / an alternative way for people to access the page.
What might be the best way to redirect a user after they enter the access code in the form?
Thanks for your advice!
It's really hard to say what the 'best' solution would be as it's open to interpretation. Here's what I would do.
Instead of routing to a specific page, I would route them to a controller that includes the code and/or content from the user directory. This will allow you to secure any contents of the user directories through server configurations, and give you better programmatic control of what happens when something goes wrong.
The user key should be set to a session key but if you don't want to do that, you could set it to a POST or GET parameter just as easily.
if(array_key_exists("user",$_SESSION)){
include_once("/".$_SESSION['user'].".php");
//use the included file if it won't automatically run itself
}
else{
echo "error - missing user key";
}

How to include a login form in the header part of the html page

I am trying to implement a log-in form in the header part of my html page.
Log-in form in the sense, i just want to show a pop-up text box , when a url is called.
Example : when a user try to call "www.myserver.com/lodapages.html" i want the user to enter a password before the content of the page loads.
if the password matches , then the user can see the page contents else no.
This is what happens when we generally tries to access mysql server.
So, Can anybody assist me to work on this. or is there any other way of simple implementation ?
I am using php codeigniter.
You won't easily prevent a page to load with a popup like that.
Either you go and code a standard three steps authentication :
the user requests the page
if he is athenticated, he gets it, else he is redirected to a login page
if the authentication scucceds, then, he is redirected to the requested page
Or you simply use htaccess protection (wich will actually popu-up an authentication dialog...) :
on load call javascript prompt method and get password from user after that redirect user to next page.
In codeigniter, maybe you can use a login.php view as the default view page, when a user try to call your pages, login.php will be displayed and the user is required to enter password.After the user enters the right password, you can use the controller login to redirect the page to lodapages.html.

Display login success without adding to every page

So I want to add like a box that says that you have been logged in successfully, but I don't want to add the code to every page. How can I make it so that the code is added to the login page (which redirects you to the home page, or whatever)?
Use the $_POST global on the page what is the submit page of the login form. The most useful is when that is the index.php, which includes all the other active pages.
if( isset($_POST['loginname']) )
{
>>write out the welcome message<<
}
it means, that immediatelly before that was submitted the form with the loginname input field.
After that save the login event into a session (ex. $_SESSION=$_POST['loginname']), only this will accesible on all further pages. But on logout do not forget to clear that session.
That depends on your login system structure but if you have a login box at the top of everypage for example there should be a script to process those logins without adding code to everypage other than a header include.
Again, depends on how your site is setup
If your question is how to send that information and store it from page to page, you would use _POST to send to login page, store the information in a session/cookie. Etc
Edit: if i am misunderstanding the question, let me know.
The question is not really clear. Looks like you need to think of a include file which can have your success message and related processing.

login page and home page with same url in facebook

How did facebook manage to have login page and home page of a user with same domain name?
When the user click on login button, he should be redirected to another page.It seems like this is not happening in case of facebook,as there is not apparent chane in the URL
Facebook (like many other websites) keeps track of whether a user is logged in.
It does this by starting a 'session' on the server side, and having the web browser send a cookie when a page is requested.
What happens is:
You request www.facebook.com, the server sees that you are not logged in so it shows a page to login. When you login it redirects to the same page, but this time the server gets a cookie that shows you are logged in. And based on this it shows you your homepage.
When I go to login I am on $DOMAIN/login.php and went I hit login I go to $DOMAIN/home.php
You could if you wanted have the whole site on ONE page by just controlling what page to actually include() via the $_POST vars or even $_GET vars so what you're saying is very possible.
i.e.
if(login_vars_are_set){
try{
login();
}
}
if(login_successful){
include(homepage);
}
Does that make sense?
Let's say you load index.php.
In it, before anything is displayed on the screen (for a visitor to see) the server asks:
if ($visitor_logged_in == 'NO') {
echo 'Facebook Homepage blah blah blah';
}
if ($visitor_logged_in == 'YES') {
echo 'Thank you for logging into Facebook!';
}
Now, above all of that, is a $_GET/$_POST request that sees if any form data was submitted. If that is your username/password and it is verified to be correct, the index.php page assigns a value to the $visitor_logged_in variable. If the form data was blank or invalid, it would get a value of 'NO' or, if it was a real user, it would get 'YES'.
It's a pretty simple thing and lots and lots of webpages behave like this. For one thing it's sometimes easier to have a handful of PHP pages than a separate page for every type of action (instead of using multiple pages, just use multiple IF statements). Another thing is just simpler/easier to learn/manage code. If you know all of a page's "guts" are on a handful of pages, it makes updating them far easier to work with and troubleshoot.
I'm a big fan of self referencing pages, particularly when it is involved with log-in/log-out type behavior.

Attaching a link to back button in php

Is there any way by which I can attach the back button of my browser to any particular link?
Actually I am designing a login page in PHP. After login I want the back button of my browser to open any particular page that I want, how can I do that in PHP?
Actually I am not a PHP guy, so my question might sound silly to some. :P
I suspect you want to redirect the user to a particular page after he logs in, you can simply use the header function for that:
header("LOCATION: user-panel.php");
That will redirect home to user-panel.php page.
The browser's back button goes back until there is history found, just to add that you can use javascript for that although this might not be required in your case:
Go Back
More info here
Update Based On Comment:
Basically you set a session when the user is authenticated for the first time, here is an example:
session_start();
// check if the user is already logged in: if yes redirect him even if the back button is clicked
if (isset($_SESSION['logged']))
{
header("LOCATION: user-panel.php");
}
// below is your own normal code
// your db query if the user specified criteria was met
if (user found)
{
$_SESSION['logged'] = true; // you should add this line if not already there
// redirect the user
}
As #Sarfraz correctly says header is the way to go. The back button is on the browser. PHP runs on the server, it does know anything about what happens in the client's browser. The page may as well have been accessed from a shell, for instance, where you have no back button.
Also, that would not be good page design, as people expect the page to rediret automagically after a login, not to have to push the back button.

Categories