Forcing the Browser Back a Page - php

I was wondering if there was any way through php or javascript I could tell the browser to go back to the page it came from, or even better not load the page at all (the later being probably impossible).
The reason for this is that I have written a small php script that will take parameters from the url and post a tweet for me discreetly while I am at work.
ex.
tweet.php?user=myname&pass=mypass&message=My message goes here
Though it works, I get stuck with a white page. It would be nice if I could have the browser go back to the page it was just on, so the pause between work would be minimal.
Thank you for the help!

javascript: history.go(-1);

The JavaScript function for this is window.back(). Have your PHP script produce something like the following to have browsers automatically "bounced back" to the submitting page:
<html>
<head>
<title>Success</title>
</head>
<body onload="window.back()">
<h1>Success</h1>
</body>
</html>
Non-JS browsers will see a "success" message, JS browsers will get bounced back.

You could do the following in PHP to redirect back to the previous page:
<?php
$ref = $_SERVER['HTTP_REFERER'];
header('refresh: 10; url='.$ref);
?>

Depending on the browser, either an HTTP response code of 204 or 205 might cause it to not leave the current page.

If use use a PHP HEADER, you can redirect to another point on the site. Minimal Pause work (as long as the process isn't very long).

In tweet.php, use the header function to redirect back to the referer

Sending people back to the page without any success message would be very confusing. I would make the call with AJAX and provide some feedback to the user that the action was performed successfully, very much like the voting system here on SO works.

Related

PHP header function does not work perfectly on IE?

I'm new at HTML/PHP and a doubt just came to my mind while developing a simple system to the company I work for.
After the user fill an HTML form and save it, I have a php page that saves the previous form information in my MySQL database. This PHP page displays a message if tha data has been saved correctly and after 5 seconds it redirects the user to my index.php. I used php header function to countdown the 5 seconds and redirect the user to index.php:
header("refresh:5,url= index.php"); /* Redirect browser */
That works perfectly on Google Chrome, but on IE it seems like it just understand the "refresh" part and completely ignore the "url" redirect, because it just refresh the current page, which gives me a few errors, of course.
My doubt is: php header function has compatibility problems with IE11 (or vice versa)? I know that I can use HTML to do the same thing, but is there a way to make the "url" from php header function works with IE11?
Thanks.
you can use it to refresh page
<meta http-equiv="refresh" content="30; ,URL=http://www.metatags.info/login">
please use like this as suggested below
echo '<meta http-equiv="refresh" content="30; ,URL=http://www.metatags.info/login">'
That's not how you redirect in PHP (it's actually how you redirect in HTML, which isn't the same thing). Try;
header("Location: index.php");

PHP Referrer of Referrer (Recursively)

I am looking into sending the user back two (or maybe more) pages from where they came. What works so far is
<strong>
<a href="javascript:history.go(-3)">
Click here to go back to the view
</a>
</strong>
However, the [history] page being called does not refresh to show the changes.
So I have the idea of referring the referrer. PHP gives me $_SERVER['REFERER'] to use, which is OK but only up to one level. How do I get the referrer (of referrer...) of $_SERVER['REFERER'] ?
As far as I know, you cannot do that in PHP. The simple answer is no, because PHP is server-side, and gets just the current referrer, while Javascript is client-size, running on the browser, which actually does have 2 or more history steps.
Consider re-thinking why you want this to happen. You can never guarantee that the 2-step back referrer (even the last one) is still in your site.
Pure JS. No PHP.
Modify your existing code like this.
<strong>
<a href="javascript:window.name='autoLoad';history.go(-3)">
Click here to go back to the view
</a>
</code>
Now add this JS to the page, where you are being redirected. say test.html when you click history.go(-3)
function autoLoad(){
if (window.name=='autoLoad') {
location.reload();
window.name='';
}
}
and in the body tag add this
<body onload="autoLoad()">
In server variable $_SERVER['REFERER'] is only the last referrer. If you want to use previous you have to save them to session. The problem is that you can't find out which browser tab initialized the request - so your history would be mixed for all tabs.
Solution would be using JS to force the browser to reload page.
You could do something like this, or use cookies if you like them more, but you don`t need PHP for this.
<input type="hidden" id="reload" value="0">
<script type="text/javascript">
window.onload = function(){
var el = document.getElementById("reload");
if(el.value != "0")
{
window.location.reload();
}
el.value="1";
}
</script>

How to Redirect Page in PHP after a few seconds without meta http-equiv=REFRESH CONTENT=time

It seems that it is not advisable to use
<meta http-equiv=REFRESH CONTENT=3;url=url>
for redirects but instead use
header('Location: url')
However, I would like to show the user some message and allow them some time to read it before redirecting. Is there a way to do it without meta?
Try use "refresh" header:
header('Refresh: 3;url=page.php');
Also, you can look at this Question Refresh HTTP Header.
There is nothing wrong with using the meta refresh tag.
<meta http-equiv="refresh" content="5;URL='http://example.com/'" />
That tag says wait 5 seconds and redirect to example.com. This tag isn't a problem unless users are on IE6 and it still works, just breaks the history buttons.
Using JavaScript is an option, but make sure you include a link saying "If you are not automatically redirected, please click here". You should actually include that link either way.
php way to set header, will redirect you to test.php in 5 seconds:
header( "refresh:5;url=test.php" );
call before any actual output is sent.
And in javascript:
setTimeout(function () {
window.location.href= url; // the redirect goes here
},5000); // 5 seconds
Header tags are sent on page load, to the browser, so that it can quickly redirect the user to the desired page without bothering to render it or even load it into the history. As such, you can't call a redirect once the page has already loaded, as the headers have already been dealt with.
You can instead perform this with:
header( "refresh:5;url=wherever.php" );
Which basically sets the <meta> tag in the headers of the page itself, meaning you don't need to write the tag out.
By what you guys are saying, theoretically this should work then:
URL: http://www.example.com/ticketgen/index.php?success=1&redir=1
<?php
$myredir = ($_GET['redir']);
if ($myredir == 1)
{
header( "refresh:5;url=http://www.example.com/ticketgen/" );
}
?>
But it does nothing. I also have it at the VERY TOP of the page so it can send the headers.
it doesn't work in Firefox i just found out.
You can do it with a small piece of javascript:
<script type="text/javascript" language="JavaScript">location.href = 'otherpage.php';</script>
Of course, this will depend on the person having JavaScript enabled.
Obviously, to set the delay you can use something like setTimeout:
<script type="text/javascript" language="JavaScript">
setTimeout(function () {
location.href = 'stackoverflowhelp.php';
}, 5000);
</script>
I think really the best way is header("Refresh: 10;url=../index.php");
Like what i've done with my work.
https://codingislove.com/redirect-pages-php/
check out the above article, where they clearly explained about how to redirect the pages in PHP by setting time.
Redirecting code without time set:
header('location:URL ADDRESS');
Redirecting code with three seconds time set:
header('refresh:3; url=URL ADDRESS');

Check whether Javascript is enabled

Is there a way to check whether Javascript is enabled or supported by the browser? If it's not supported, I would like to redirect the user to a user-friendly error page.
I am using jQuery and the PHP Zend Framework.
<noscript><meta http-equiv="refresh" content="1;url=error.html"></noscript>
This will redirect to an error page if script is disabled. Just replace error.html with the URL of your error page.
As yet another option, you can (though it requires a second page visit) use javascript to set a cookie.
If the cookie exists server-side (they have javascript) render the page as normal. During the absense of the cookie, you can either use a Location redirect, or render the appropriate [stripped-down] template to accommodate their lack of javascript support.
page html
<script type="text/javascript">
document.cookie = 'hasJS=true';
</script>
page php
if (isset($_COOKIE['hasJS'])){
// normal page render
}else{
header('Location: http://mysite.com/index-nojs.php');
}
Whether or not javascript is enabled is only known from within the context of a browser, so the best you can do is write some browser-side code to set a flag based on if javascript is enabled or not. One possibility is do something like
<noscript><img src="/javascript_disabled.php"></noscript>
and
// contents of javascript_disabled.php
$_SESSION['javascript_disabled'] = 1;
As the default, send out the version without javascript. There you include a little piece of javascript that redirects to the dynamic version. This will only get executed when js is enabled.
You can make a simple "landing page" for users without javascript AND add a javascript redirection to the javascript-enabled version of site.
Something like this:
...
<html>
...
<script type="text/javascript">
window.location.replace("/hasjs");
</script>
Piece of cake!
Encompass your entire Javascript page in one DIV.
Overlay a second DIV of equal size that contains your non-Javascript page. Give that DIV an id and a high z-index:
div id="hidejs" style="z-index: 200"
In this second non-JS DIV, have your very first code be Javascript that sets this DIV's visibility to hidden:
document.getElementById.("hidejs").style.visibility = "hidden";
No Javascript enabled? Nothing will happen and they'll see the overlying non-Javascript page.
Javascript enabled? The overlying non-Javascript page will be made invisible and they'll see the underlying Javascript page.
This also lets you keep the whole page in one file, making modifications easier, rather than trying to manage two files for the same page.
Use <noscript> tags to include a meta-redirect if the page does not have JavaScript.
<noscript> tags are called when the browser connecting to the page does not have JavaScript.
Bring the user to the error page by default, and have a javascript redirect execute in the section of the page. Have the redirect push the user to the real page.
If they don't have javascript enabled, they won't get redirected and the rest of the page (error page) will load.
Yes. Make you have the latest jQuery.
Javascript:
$(function(){ $('#jsEnabled2').html('Yes it is') })
PHP:
$js - 'No';
$jscheck = 'Javascript Enabled: ';
$jscheck .= '<span id="jsEnabled">'.$js.'</span>';
print $jscheck;

HTML Redirect To Original Page

I have a one page site that has PHP code in it. Once the user presses 'Send', this sends the information to my email, then displays a messagebox saying that the action was a success to the user - great.
After the messagebox is closed, the website stays at website.com/report.php. Is there a way to redirect it back to the original page.
Also, any way to change the icon in the messagebox that pops up? Here is the code that I have:
<script language="JavaScript">alert("Your request has been sent. I will contact you soon!");</script>
Thanks.
Look into window.open and window.location
Place it after your alert()
http://www.tizag.com/javascriptT/javascriptredirect.php
Also, to answer your messagebox icon question: No, it is browser-dependent and not modifiable.
If you want to do that, your are going to need to fake it with html/css and javascript.
<script language="JavaScript">
alert("Your request has been sent. I will contact you soon!");
window.location.assign("http://website.com");
</script>
If you want to change the icon in alert box or make it look a little fancy, you could try YUI dialog
Use this code to display the alert:
function displayAlert(message, redirect) {
alert(message);
window.location.href = redirect;
}
Then, you can use code like:
displayAlert("This is the message", "http://redirect.the/user/here");
Use the php header command
<?php
header("Location: http://www.example.com/");
exit;
?>
To do a redirect in PHP, use header("Location: page.php"); for this. Before and after this your code shouldn't be sending any other output to the response. Eventually use exit(); to terminate the script afterwards.
If you need the page which was requested right before this page, then best what you can do is to include its URL as request parameter of the link to report.php and use it as redirect destination. E.g.
report
and in the report.php pass it as hidden input field:
<input type="hidden" name="referrer" value="<?php echo getParam("referrer"); ?>">
And after submitting the report do:
header("Location: " . getParam("referrer") . ")"; // getParam() returns sanitized GET parameter.
An alternative is to use the $_SERVER['HTTP_REFERER'] header (yes, including the typo) for this, but this is just not that reliable as it may be disabled or spoofed by the client.
I believe a more elegant solution is to simply present the user with a confirmation page (instead of an alert box), and place a link to the previous page there.
That would at lease work for all users.
For those users with javascript a little Ajax (jQuery) could submit the form for you, and display the confirmation nessage. All without leaving the page the user is on (negating the need for any fancy redirects).
Towards the end of your php, use this:
header('location: home.php');
This will cause the browser to load the original page.
I don't believe that the standard alert box can be altered, aside from the message. You can't change the title or the buttons, either.
Frank

Categories