Javascript/php Back Button with if/else function - javascript

Is it possible to have a back button feature that goes back from the previous page if it was coming from the website itself (i.e. page 1 to page 2). But when user come from other site (i.e. google search result) it links to a general page from my website.
I am new to javascript and the only way I know is the href="javascript:history.go(-1)" which does not do what I want. Can someone provide an example or link to a site that can help me? thx.

You can't change the browsers back button, but you can create a regular button on the website that does this, which is what I'm guessing you want.
Something like this, checking the referrer should work
<?php
$referrer = $_SERVER['HTTP_REFERER'];
if (strpos($referrer, 'example.com') !== FALSE)) {
echo 'Go Back';
} else {
echo 'Go Back';
}
?>

If you are talking about making your own back button, then absolutely. You would need to track the referring page in php using $_SERVER['HTTP_REFERER'], and if that page is not on your site, fudge the link so it goes to your general page.
If you want the same behaviour with the browser built in back button, I think you are out of luck, you can't access the history directly for security reasons, so you have no way of knowing if the last link is on your domain or not.

Related

How Facebook and Google handles the back button after logout?

I am working on a php project in codeigniter. When a user logs out, he/she is directed to login page. But when he/she clicks the back button of browser, he/she gets to see the home page just before the log out, which definitely not a good thing.
I have added session check on every page so that when a user reloads any page after logout he/she will be directed to login page.
As far as i think, this happens due to web browser using cache for the back button.So one solution as given in many stack overflow forums and others is to clear the cache, which i too did. But another problem came that the document expires after back button, which is not good from the UX point of view.
Another solution is to add javascript code to restrict the page to itself when pressing back button, which surely works. But since the javascript is client side, this won't work if you disable it from browser.
If you check the Gmail or facebook or any good site ,they handle it really well and no ,it's not by just javascript code , as suggested in this forum How Google deals with the Back Button after logout? ,
So my question is how do they handle it in real?
On home page, you'll need to check if SESSION is active.
When user is logging out, you'll need to destroy SESSION.
For example:
-On Home Page
session_start();
if(!isset($_SESSION['variableName']))
{
header("location:login.php");
}
On logout page:
session_start();
session_destroy();
Hope this helps.
One time I got that problem and finally I got many tiny solutions for that.
Put noscript tag in common header file. I return a text when the JS turn off.
<noscript>Your browser does not support JavaScript!</noscript>
Another Method to check the user logged or not at every intervel
setInterval(function(){
$("#autorefresh").load("checklogin.php?screenName=autorefresh");
}, 5000);
And finally put this back prevent js
history.pushState(null, null, 'loginController.php?time=
$currentDateTime; ?>');
window.addEventListener('popstate', function(event) {
history.pushState(null, null, 'loginController.php?time=<?php echo $currentDateTime; ?>');
});
This is my experience knowledge... Thank You

HTML5/Javascript: Content changing without adress changing

I have a question which I haven't been able to find the answer for. I hope you can help me.
I am about to build a simple website, containing text and hyperlinks. I want the site to have the same adress no matter which hyperlink is clicked. For example, if my website is www.website.com - when one clicks a hyperlink, the content of the whole page should change, but the adress should still be www.website.com, instead of www.website.com/hyperlink.html for example. In other words, I want to disable people to use the "back" button to return to an earlier page, and prevent them from navigating the page by writing in the adress bar. They should experience a single page, but still be able to navigate through a lot of changing content through links - which means that if they click the "back"-button, they will be navigated away from the website, and if they refresh the page, it will go back to 'index'. Can you point me in the right direction to which methods might be useful here? Earlier, I would have done it in Flash, and embedded the flash-construction into the website, but as far as I have heard, Flash is not the best solution anymore?
Thanks in advance.
First of all, that is not the best idea for SEO.
But that puts aside, you should use javascript to make AJAX call and alter the partial part of your page with the response.
So basically, what you will do is from your home page, capture all link clicked event, and process the request through an AJAX call, and display the result of that call on the same page.
That allow you to refresh a list of item, or a menu, or the entire page if you want.
Since it will be AJAX call, the user won't see any difference in the URL.

Ways to prevent user from reloading/refreshing page in quiz taking application

I am currently trying to figure out a strategy for not allowing a user to refresh/reload a page during the quiz. I know it's not a good idea, or even possible, to completely disable the reload feature in the browser, but are there any other ways to keep the user from being able to restart the exam by reloading the page?
I currently have the browser window not displaying the back button, and typing in the url is not allowed. I know I can disable the context menu from the right-click on the mouse, but keyboard shortcuts for reloading the page are still allowed. Any ideas on how I should approach this?
EDIT
I should change this to mean that I am not actually attempting to disable the browser reload feature, but instead, if the user does attempt to reload, I want to redirect them so that the test ends, or some other event takes place, instead of the test restarting.
Currently, I am using a localStorage variable to indicate if it's a reload and going from there. Is this the best solution?
Thanks!
You are fighting an uphill, unwinnable battle if you are attempting to secure a web application using only client-side technology (javascript). There are always ways to get around the measures you're attempting.
A workable solution would be track the quiz progress server-side after every user decision. This way, if the user tries to reload (or otherwise start over, re-answer, or skip around) you can simply return them to where they belong.
I can think of a solution that detects if the page is refreshed.
If you're using PHP, you can set a SESSION variable on loading and then check to see if that variable is set yet
<?php
if(!isset$_SESSION['loaded'])
{
$_SESSION['loaded'] = 1;
}
else
{
echo 'You refreshed, your quiz is over ;-) ';
}
?>
As for disabling refreshing, you'd be bordering on malware to achieve it, ie. filtering all keyboard input for 'F5' key or 'Ctrl + R' from JavaScript, and disregarding keyboard input when they enter those shortcuts. I'm not sure if that's possible/ethical

How do you break out of frames without breaking the browser's back button?

A site that links to mine keeps my site in a frame, so I added the following JavaScript to my page:
if (window.top.location != window.location) {
window.top.location = window.location
}
Now if I get to my site via the offending site, my site successfully breaks out of the frame. But the back button breaks! The back button sends the user to the framed version of my site, which immediately breaks out again, returning him to where he was trying to leave! Is there a simple way to fix this?
window.top.location.replace(window.location);
The replace method is specifically for this purpose. It replaces the current item in the history state with the new destination so that the back button won't go through the destination you don't want.
jfriend00's answer is indeed correct. Using the window.location.replace method will work without affecting the back button.
However, I'd just like to note that whenever you want to stop a page from being framed, you should do more than just that! There are a couple methods of preventing a simple script like that from breaking out of the frame, which work in many modern browsers. Perhaps you can disable the page, display a message with a link to the full page, something like that. You could also use the X-Frame-Options response header that tells the browser not to display the page in a frame. If you don't take some of these measures, your site could be clickjacked.
Another solution is to open your site in a new window leaving a friendly message in the iframed site:
if (parent.frames.length)
{ window.open("mySite.htm", "MySite");
location.href= "framedMessage.htm";
}
Where framedMessage.htm contains some friendly/warning message.

What to do when browser back button doesn't have the intended effect

I have a page where navigation is handled by hiding and showing preloaded divs when users click on links. But, the users think they've actually changed pages, so they click on their browser's "back" button trying to go back to the div that was previously hidden. But of course, they go back to the page from which they came.
What's the best way to handle this? 90% of the traffic is from a login page. Should I just sandwich a redirect page in between the two? How is this done? Can I just change the browser's back button behavior?
If you are already using jQuery, why not simply add a history manager like jq-bbq or the hashchange or history manager? (Or, if you want to really go all out, switch to a MVC JavaScript framework like Sammy.) That way, the back button will work as the user expects, rather than hacking around their expectations by blocking the back button or throwing in redirects. (Unless you have a good reason to, of course :-) )
If you use a browser history plugin like the jQuery UI one you end up changing the history so that the back button doesn't actually unload the page.
http://yoursite.com
-> User clicks something
-> new address bar reads http://yoursite.com/#/something
because of the hash mark when user goes back it goes back to http://yoursite.com which should inturn fire your show previous div function
read more about the available history manager plugins available for jQuery. There are quite a few. Most if not all provide available callback functions that you can specify.
On change of the state of your page, write a unique set of parameters to the hash of your URL. You can change this via JS without causing the page to reload.
Set a timer on the page that checks the current location hash repeatedly, and if it changes (i.e. the user presses the Back button) then update the state of your page to match the URL.
I have this scheme working to great effect in a local application.
The jQuery Address library is another great alternative.
http://www.asual.com/jquery/address/
You can set the URL for different application states, and get the URL 'parameters' when the page reloads.
Two ideas:
1) onbeforeunload. Ask the user if they want to really go back.
2) Sandwidch a redirect page. Login -> redirect -> your page. A single back click would take the user to your redirect page.
The second is kind of a pain in the neck for people who know what they're doing though. I think the Back button (and all standard navigational elements) should be messed with as little as possible.
I would go with onbeforeunload:
function sure()
{
event.returnValue = "sure?";
}
...
<BODY onbeforeunload="sure()">

Categories