Go back to previous page if same domain - javascript

I need to create a button to go to the previous page if the last page visited was in the same domain and I have to check what page was. For example if the user come from google, the back button have to redirect to the homepage, if he come from www.domain.com/product the back button have to redirect to that page, if he come from www.domain.com/static the back button have to redirect to the homepage.
I tried with PHP and the $_SERVER HTTP REFERER and if the protocol is http woks fine, but it doesn't work with https, it redirects always to the homepage. It looks like that variable doesn't exists!
How can I solve that problem? JavaScript and history obj can help?
Thanks

Use a session variable to track the current page and check it at the next page, i.e.:
if (isset($_SESSION['page_url'])) {
// visitor has seen another page in this domain
$previous_page = $_SESSION['page_url'];
}
else {
// visitor comes from outside, use home page
$previous_page = 'http://domain.com';
}
// keep the URL of the current page
$_SESSION['current_url'] = $_SERVER['REQUEST_URI'];

Related

How to skip external redirection page using history back function?

Let's say an external site has a redirection page before actually moving to the internal site when user clicks the link.
This internal site only has a button that will let you go back using
History.back() or .go(-x). I can only do changes on the internal site.
What's happening here is that we now have a "loop" that redirects to the external redirection page using history.back(), and then the user will be redirected once again to the internal site.
Flow:
External site -> user clicks on internal site link -> external site sends user to external redirection page -> user redirects to visit the internal site -> user click history.back button -> user get redirected to external redirection page -> user redirects to visit the internal site again
I basically do not want the user to end up at the internal site after users clicks the history.back button.
I thought about saving page visit in localstorage, so that if it gets redirected back to the internal site again it will instead do history.go(-2).
I am also aware of https://developer.mozilla.org/en-US/docs/Web/API/Document/referrer document.referrer, but that will also degrade the UX since the user gets redirected back and forth at least once.
The only solution I could think of is having a query parameter upon visiting the internal site someurl.com/?redirects=2, so that we could do history.go(-redirects). However, that requires the external site to decide how many redirects it has.
Is it possible to know if user comes from a redirection page from the external site? Is there a way so that we can skip the external redirection page and just go straight to the external page?
window.location.replace is there for exactly the same requirement, please refer https://developer.mozilla.org/en-US/docs/Web/API/Location/replace
Assuming that the external rediection page contains only the logic for rediection and no content, they should have used location.replace for redirection to have a clean state of history.
I know that you do not have control over external site but I just wanted to help in case you can ask someone to make changes there :)
I came up with a solution to the problem, but my approach does use document.referrer.
Regarding UX degradation when using document.referrer, it should be barely noticeable as long as your clientside code is optimized.
Also, you might be interested to read this MDN Article.
My solution approach is attached here:
const isExternalURL = (url) => new URL(url).origin !== location.origin;
function checkExternalURL() {
const referrerPage = document.referrer;
if (referrerPage == undefined) return false;
return isExternalURL(referrerPage);
}
if (checkExternalURL()) {
console.log("External URL Detected");
} else {
console.log("Internal URL Detected");
}

Javascript get the full URL that redirected to a page

Hi there, as you can see on the image, in my webpage I have several pages that can redirect to the same page.
On the example, both pages:
example.com/content.html -> example.com/news.html
example.com/files/actual.html -> example.com/news.html
I want to enable a button on the page example.com/news.html which goes back to the full source refereer url
So for example, if user A got redirected to example.com/news.html through example.com/content.html his Go back button should point to the source URL -> example.com/content.html
I have tried the JS property
var referrer = document.referrer;
console.log(referrer);
But It only returns the domain name example.com and not the full URL example.com/content.html
Any thank is appreciated.
It depends on what you need. If you always have a redirection you can use :
window.history.go(-2);
Otherwise, you may have to manipulate the parameter using history informations.
Full documentation : https://developer.mozilla.org/en-US/docs/Web/API/History_API
What you are asking for is not possible. In modern browsers, a redirect does not replace the referrer, nor does the redirect appear in window.history. Using client side JavaScript, there is no way to tell that the user came through a redirect as opposed to clicking on a link that brought them to the page directly.
As a workaround you could change the redirect to add a parameter. example.com/content.html could redirect to example.com/news.html?from=content.html Then the JavaScript on news.html could look at the URL parameter to determine which page redirected.
Alternately, you could use server side solutions. Your server's access log would have a record of redirects. You could examine this log file to determine which page redirected.

How can i get source page url

Now i need to get source page url when i navigate any page under specific domain i tried this jquery code
$(document).ready(function() {
var referrer = document.referrer;
});
but i get the previous url page but i want to get the main link that open my domain for example i searched about my website from google then i open my website from google then i navigate any page under my domain ..... i want to get in any page that i come from google ..by the way my website is PHP.... can i make some thing like that ?!
On the server side, you can use $_SERVER['HTTP_REFERER'] to get the referrer.
Now when the user links (or submits) from one page to the next in your website, but you still want the website they originally came from instead of the page they just were on, you should remember the original referrer in some way, for instance by storing it in a session variable. Something like this:
$ref = $_SERVER['HTTP_REFERER']; // Get referrer
if (!$ref.strpos($_SERVER['HTTP_HOST'])) // It's not from the same domain?
$_SESSION['originalreferrer'] = $ref; // Nope, store in session
Then you will have $_SESSION['originalreferrer'] as the original referrer, as long as you include this code in each of your pages that may serve as a landing page from outside.

Preventing unwanted redirects from browser to app store

I've been working on a redirect page that sits between an ad and the app store. The ad exists as a static URL that directs to the redirect page. The redirect page sends an ajax request to a third party, sets a cookie, then redirects to the AppStore. All well and good and not uncommon.
The redirect page cannot close itself so it remains as a tab in Safari. The issue I'm having is that when the user returns to Safari if the page is been purged from the cache, Safari will reload it triggering the redirect. I do not want the users getting thrown into the AppStore unexpectedly.
One solution would be to check for the presence of a cookie and not redirect if it's presence, but this leaves the edge case of the user clicking on another banner ad and not getting the appropriate redirect. I've tried appending an anchor to the URL which prevents user initiated refreshes, but the auto-refresh mechanism of Safari does not respect the programmatically added hash.
If I could use a dynamic source to generate the URL that directs the user to the page I could generate a timestamp, but right now the origin URL is static. Does anyone have a solution for this using client side code? Or is this really only solvable using a server-side solution?
I ended up choosing to do a two stage redirect. User clicks a banner and gets directed to:
http://myserver.example.com?someKey=someValue
I have a function that does this:
// Do I have a visited param?
if ($.url(window.location.href).param('visited') === '1') {
// Do I have a visited cookie?
if (helper.retrieveCookie('VISITED') == undefined) {
console.log('Setting visited cookie');
helper.storeCookie('VISITED', '1');
return 1; // Redirect to AppStore.
}
// Have param and cookie
console.log('VISITED cookie set');
console.log('Refreshed');
return 2; // Don't redirect.
} else {
// No param
helper.removeCookie('VISITED');
return 0; // Redirect to self with &visited=1
}
So basically we will pass through the code twice meaningfully and N times subsequently due to refreshes. During the first pass we do our AJAX request, then redirect to ourself with an added param. During the second pass we set a cookie and redirect to the AppStore. Any subsequent load of the page will have both a cookie and a param and won't redirect. New banner clicks will not have the param so they will perform normally.
This isn't the most beautiful solution since we have to reload our redirect page, but since its contents should be cached, the hit should be minimal.
How about adding a javascript on the landing page?
if (history.length < 2)
history.back();
else
location.href = '/thankyoufordownloading.htm';
I haven't tested it myself but it could work.

Document.referrer wrong when pressing the back button

I have a PHP / Javascript page that automatically logs a user into different systems from one log on. These are external sites and all works good except when the user hits the back button. It then redirects them right back where they came from.
I'm looking to have it redirect back to my main website and avoid getting stuck in this redirect nightmare. So I tried document.referrer, but that only seems to grab the current page I'm on and not the referred site. Am I wrong or can this not be done like this?
function myfunc () {
var frm = document.getElementById("loggedin1");
if(document.referrer != 'https://someurl') {
alert(document.referrer);//Just used to see output
frm.submit();
}
}
window.onload = myfunc;
If I could get it to function I would add an else in there and have it going back to my website.
Thanks!
It sounds like you are trying to go back to a previous page, that is not within the website of the page you're on?
A few points:
1) document.referrer will only work if the person got to the current page through a link, or clicking something.... not if they were redirected through other means.
2) Due to browser security implementations, you will not be able to access the javascript history for other sites. So if you go from your site, site A, to site B for a login, you will not be able to access the site A history from site B.
If you need to take them back to the previous page they were on on your site, can you use an iframe to load the external page? that way they'll never leave your site? Or maybe a window popup?
If what you are trying to accomplish is site logins, have you looked into the available apis? Sites like facebooks have apis for allowing logging in on your site through theirs.
Cheers!

Categories