Attaching a link to back button in php - 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.

Related

How to disable browser back history for a specific route in php? [duplicate]

I am working on a my site to allow users to take test so they can see how much they know of a particular subject. I am running into a little problem though. Once a user submits the test for grading, how do I prevent them from going back to the test page? I am on a Mac with Safari running and when I click the back button in my web browser after I submit the test it leaves all of the answers I answered filled out. I want it do this: When a user submits a test and they click the back button in their web browser it redirects them to the main test page.
I am using PHP and MYSQL. I even have the test pages setup so that the user must come from a certain url (I am using HTTP_REFERER) and I have tried other stuff such as sessions but I cannot seem to figure this out. Any help is greatly appreciated.
You don't stop them.
Instead change your application so that it still works even if they go back. You can embed a unique number in a hidden field on the page and if they resubmit the same test twice you can detect it and display an appropriate error message. You should also think about what should happen if they modify the unique number.
If you don't want people to post different answers once they have already answered, all you have to do is check, in the script that accepts the test for grading, that the user has never submitted the test before. If you don't, a clever student will always be able to to circumvent your protection by sending an appropriate request directly to that script.
If you don't want people to see previous answers (for instance, if you have two people grade their tests on the same computer), consider using AJAX on the test page to submit the answers and then erase them from the fields. This way, most browsers will not remember the answers and the back button will not un-erase data that was erased by JavaScript.
At the top of the grade page, put the following:
session_start();
$_SESSION['testcomplete'] = 'yes';
Then at the top of each page of the test, put this:
session_start()
if ($_SESSION['testcomplete'] == 'yes') {
header("Location:cheater.php");
}
You could simulate there being no page to go back to. From one page, generate each test page using jQuery, and provide no way to go back, only forward. The back button would take them to the page before they ever launched the test, and you could allow them to launch the test again and generate the right part where they should be. This would be pretty easy, if you haven't gone too far in development the current way.
You could run javascript that clears out all the answers. You might also just allow one submission so that subsequent submissions don't get processed. HTTP_REFERER is usually sent, but can be spoofed and forged by an altered browser.
On the top of the script POST-ing the answers, do a check whether you have the test results in the database for the current user for this test. If you do, redirect to results.
if(get_test_results($user)){
$test_url = get_test_url($user);
header( "Location: $test_url" ) ;
}
Disabling the back button is not a good idea.
I was facing a similar problem making an online examination myself
what I did is
I provided a session variable such that if the user pastes the previous page's URL in the address bar then on loading the page the page is automatically forwards to the next desired page. Whether the page whose URL was mentioned is the being visited the first time or being revisited is determined by the value of the session variable
If the user instead of loading the page does a go back via the browser button the it automatically redirects to the next page in history as :
javascript:window.history.forward(1);
Hope this helps :)
http://www.htmlgoodies.com/tutorials/buttons/article.php/3478911/Disabling-the-Back-Button.htm you should be able to do it in javascript.

How can I prevent the user to go back to my shop checkout with the "back" button of the browser? [duplicate]

I am working on a my site to allow users to take test so they can see how much they know of a particular subject. I am running into a little problem though. Once a user submits the test for grading, how do I prevent them from going back to the test page? I am on a Mac with Safari running and when I click the back button in my web browser after I submit the test it leaves all of the answers I answered filled out. I want it do this: When a user submits a test and they click the back button in their web browser it redirects them to the main test page.
I am using PHP and MYSQL. I even have the test pages setup so that the user must come from a certain url (I am using HTTP_REFERER) and I have tried other stuff such as sessions but I cannot seem to figure this out. Any help is greatly appreciated.
You don't stop them.
Instead change your application so that it still works even if they go back. You can embed a unique number in a hidden field on the page and if they resubmit the same test twice you can detect it and display an appropriate error message. You should also think about what should happen if they modify the unique number.
If you don't want people to post different answers once they have already answered, all you have to do is check, in the script that accepts the test for grading, that the user has never submitted the test before. If you don't, a clever student will always be able to to circumvent your protection by sending an appropriate request directly to that script.
If you don't want people to see previous answers (for instance, if you have two people grade their tests on the same computer), consider using AJAX on the test page to submit the answers and then erase them from the fields. This way, most browsers will not remember the answers and the back button will not un-erase data that was erased by JavaScript.
At the top of the grade page, put the following:
session_start();
$_SESSION['testcomplete'] = 'yes';
Then at the top of each page of the test, put this:
session_start()
if ($_SESSION['testcomplete'] == 'yes') {
header("Location:cheater.php");
}
You could simulate there being no page to go back to. From one page, generate each test page using jQuery, and provide no way to go back, only forward. The back button would take them to the page before they ever launched the test, and you could allow them to launch the test again and generate the right part where they should be. This would be pretty easy, if you haven't gone too far in development the current way.
You could run javascript that clears out all the answers. You might also just allow one submission so that subsequent submissions don't get processed. HTTP_REFERER is usually sent, but can be spoofed and forged by an altered browser.
On the top of the script POST-ing the answers, do a check whether you have the test results in the database for the current user for this test. If you do, redirect to results.
if(get_test_results($user)){
$test_url = get_test_url($user);
header( "Location: $test_url" ) ;
}
Disabling the back button is not a good idea.
I was facing a similar problem making an online examination myself
what I did is
I provided a session variable such that if the user pastes the previous page's URL in the address bar then on loading the page the page is automatically forwards to the next desired page. Whether the page whose URL was mentioned is the being visited the first time or being revisited is determined by the value of the session variable
If the user instead of loading the page does a go back via the browser button the it automatically redirects to the next page in history as :
javascript:window.history.forward(1);
Hope this helps :)
http://www.htmlgoodies.com/tutorials/buttons/article.php/3478911/Disabling-the-Back-Button.htm you should be able to do it in javascript.

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.

Triggering a jquery slidedown message on redirect

I have a list of links of twitter accounts (on people.php) a user can follow by pressing the persons name. When they press the link the follow.php script is run and the user is following the person. At the end of this script there is a header("Location:people.php") redirecting the user back to the people page.
What I want to do is give the user a message when they are redirected to the people.php page in a slide down message saying they are now following whatever person they clicked on.
I understand how to make this (this is what I am going to make http://www.youtube.com/watch?v=7wNz5T8SepQ), I just don't know how to connect it so the slide down message is triggered only on this redirect, not when the user first visits the people.php page, and how to make it so the message say which person you just followed. Thanks a lot for the help!
Since you are controlling the redirect, can you append a variable on the query string that will force a notification, ala:
http://www.yoursite.com/people.php?notify=true
You might also use a session cookie. Set the cookie when the user click the link and remove it when the message displays. Since you'll be using jQuery, you can use the jQ cookie plugin for simplicity:
https://github.com/carhartl/jquery-cookie
change your redirect header to include an extra flag - header("Location:people.php?following=who") and then you can look for that param when they return to the people.php page after following someone

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.

Categories