Changing browser history in php - php

i have a webpage, done with php (start.php&number=1)
On this webpage, there is a link to increase the number everytime i click on it.
This works perfectly fine. If I click it 99 times, the url is start.php&number=100
Now when i click the back button in my browser or swipe left on my phone, it just goes back to start.php&number=99. Although this is really reasonable, i just want to go back to the page before I visited start.php.
For example facebook if I was on facebook before I visited start.php
Is there a way to do this

You can't change the browser history. But if you don't want the browser to regard the new number as a different page then you need to
Use javascript to increment the number client side only.
Or use an ajax call to increment it at the server and display it at the client by updating a portion of the web page.

Related

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.

How to display message if user clicks on browser button?

My question is it possible to display a message if browser buttons have been pressed in order to be able to display a message stating if user should leave the page or stay on a page? Also can a message be displayed if user tries to change a url if they are on a certain page?
Thanks
Server side is needed for foolproofing this. On the server, when assessment is started, you should first have start screen URL. You must program it so that once assessment is started (after clicking start), it goes to the exam URL, and your assessment is always saved - when user goes back, using server side tricks have it so that instead of showing the start screen, it redirects back to the correct page (in a way that still maintains history), shows the exam with the data still in it, and pops up the warning not to click back button. Then, if user does it again, it does the same thing, again.
You have to have it so the exam is saved all the time. unload javascript event can be used to notify via alert() that they shouldn't be leaving the exam, and thats about all. onbeforeunload event can be used to try to give them the choice to stay, but shouldn't be depended on as it doesn't work on every browser.
Use onbeforeunload as suggested in a comment. There are various example, like this one: https://web.archive.org/web/20211028110528/http://www.4guysfromrolla.com/demos/OnBeforeUnloadDemo1.htm
If you want to call out the buttons that the user must click, you will need to do some browser detection, as most browsers have OK/Cancel buttons on the window that they display, but Chrome has "Leave this page" and "Stay on this page".

Need help redirecting and firing a pixel

I'm an affiliate marketer with essentially no programming experience, but I need a little help and I'm hoping you guys can help me out.
Currently I use a simple PHP redirect so that my advertiser can't see the ref URL of the site I have my ads on. (I assume this accomplishes that?)
<?php
header("Location: DESINATION URL");
exit;
?>
The ad server that I use allows me to target or exclude my ad campaigns to/from any user that has fired a particular pixel (iframe or script). The idea is that with this re-targeting capability, you can target your ad campaigns to people who visited a particular website in the past that you have a pixel on.
So, what I need to do is not only redirect users, but I want to figure out how to simultaneously fire either the iframe or script pixel. Here's the idea. Someone that clicks on one of my ads but doesn't end up making a purchase isn't someone that I want to serve ads to over and over again. Once they click, I want them to no longer be eligible to see one of my ads. The only way to do this is to fire a pixel for that user when they click, so that I can then use this retargeting feature of the ad server I use to exclude those users from my campaigns. Make sense?
User lands on the page
pixel fires
page redirects to destination URL
Is this possible?
No, a header-based redirect will ALWAYS show the url to the client. It's essentially telling the browser "hey, go over here and fetch this address". Your server isn't fetching the contents of that address, it's just telling the browser where to go to fetch it itself.
What you want is a proxy - your server fetches the URL and sends the results back to the client.
And what do you mean, "fired a pixel"? Loading a web bug?

make webpage counters work in redirects

I'm writing a php program that redirects user to a page.(something like link shortener)
I want count pages visits without using mysql or etc. so I chose to work with a page counter service like histats but to make these services to work webpage has to be opened by user and a jsp or embeded flash has to be runned in user browser. but my program redirects user to another page that doesn't belongs to me and page on my domain wont be opened!
is there any way to make these counters to work?
You cannot normally force the user to call the counter URL. Especially when you redirect the client via http. You may save the hits in a plain file or any other database.
You should also know that the user maybe blocks the counter with an adblocker when you use a please wait for redirection message. Instat of redirect directly.

executing tracking script on external link out

I am curious on the best way to execute a database query and set a cookie when a user clicks an external link, without having that script passed to the browser history so when the user goes back from the external site they will land on the same page they clicked the link and not the page that executes the tracking script.
I noticed google tracks on an external click, not sure how but I see there is a mousedown event that possibly changes the href location of the link to their tracking script?
How can I achieve a similar function?
Here is a solution for tracking clicks I posted earlier today:
counter for number of clicks on links in php

Categories