Im tring to get php to load a webpage, wait for a minute, and load another page..
Problem is, php does not output the first page before sleeping and outputting the second page to the screen...
Here is my code:
<?php
header("Location: http://www.paylesstitleloans.com/");
sleep(60);
header("Location: http://www.paylesstitleloans.com/faq.html");
?>
Would using function like these help me at all:
ob_start();
ob_flush();
I need the first page to display on the browser before sleeping.. Any help would be great! Thank you. Jason
You need to use something on the client side (Javascript, meta refresh, iFrames) to accomplish this.
Relevant topics:
jquery function that load html code into a div and then load other content into div inside html loaded
You cannot user headers from php after the page has already loaded. But, you can use a meta tag to redirect after a certain amount of time. Please refer to the meta example below.
<meta http-equiv="refresh" content="60;url=http://www.paylesstitleloans.com/faq.html/" />
You can't do that with location in http headers because you only can send it once.
You'll have to use javascript for that with a timeout function.
including this script in first page would do the trick :
<script type="text/JavaScript">
<!--
redirectTime = "60";
redirectURL = "http://www.paylesstitleloans.com/faq.html";
function timedRedirect() {
setTimeout("location.href = redirectURL;",redirectTime);
}
// -->
</script>
Related
I have a php file that I load after a user has successfully purchased a subscription to keep track of a purchase conversion using adwords conversion tracking. At which after it needs to redirect the user to the next appropriate page /account/subscription/
Originally I placed the header("Location: /account/subscription/"); below the conversion tracking code which produced the headers already sent error. So now I placed it above the tracking code.
Since I know its good practice to send a die() after the header redirect in my thinking was to place it below the tracking code so that it does not kill the page before the page loads the html.
Even though the header redirect is at the top of the page will the HTML below it render before redirecting?
<?
header("Location: /account/subscription/");
?>
<!-- Event snippet for Example Purchase conversion page -->
<script>
gtag('event', 'conversion', {
'send_to': 'AW-************/**************',
'transaction_id': ''
});
</script>
<?
die();
?>
No.
The body of a redirect response will only be used if the redirect isn't followed silently.
I'm not aware of any common web browser which doesn't silently follow redirects. I think lynx can be configured to do otherwise.
The general practice to die() is for cases where you are conditionally redirecting and want to avoid doing additional processing or including any HTML that is not appropriate for a redirect message.
Although this post is old I thought this might be helpful. The below work just fine as html will still get rendered
<?php
header("Refresh: 0.5; url=http://example.com");
?>
<!DOCTYPE html>
<html>
you tracking code here in body
In order to redirect, before you add the header part, you must include another part ob_start();, which enabled object buffering so it does not send a header until the page is fully loaded. To complete:
EDIT: I realize now that this is not what you want to achieve. I do not believe it can be done in pure PHP
<?
ob_start();
?>
<!-- Event snippet for Example Purchase conversion page -->
<script>
gtag('event', 'conversion', {
'send_to': 'AW-************/**************',
'transaction_id': ''
});
</script>
<?php
header("Location: /account/subscription/");
?>
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');
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;
Is there a way to make page display few seconds in php and redirect to another page ?
The meta redirect is probably what you want, but you CAN do this in PHP too, like this:
<?php header("Refresh: 10;url=http://www.yourdestination.com/"); ?>
Where 10 is the number of seconds to wait.
EDIT Ok, I stand corrected. Corrected answer below.
You can either use PHP's header function as shown elsewhere on this page.
If you want to do a refresh after the page is rendered, you can do so with JavaScript or a Meta Refresh. For users that block meta refreshs and have JavaScript disabled it is good practise to provide a link that can be clicked manually to get to the new target.
Example:
<?php header("Refresh: 2;url=http://www.example.com/"); ?>
<html>
<head>
<title>Redirects</title>
<meta http-equiv="refresh" content="2; URL=http://example.com" />
<script type="text/javascript">
window.setTimeout(function() {
location.href = 'http://example.com';
}, 2000);
</script>
</head>
<body>
<p>Click here if you are not redirected automatically in 2 seconds<br />
Example.com.
</p>
</body>
</html>
Please also see the WCAG suggestions about Automatic Page Refreshes.
However, you're probably best off doing this in JavaScript
setTimeout(function()
{
window.location = "http://www.somedomain.com/somepage.php";
}, 5000); // 5 seconds
See #Gordon's answer a above for a more user-friendly and complete example, this is merely one method.
With META redirect you can:
<meta http-equiv="refresh" content="2;url=http://example.com/">
Where 2 is the delay in seconds.
Use the following code in PHP, but only after understanding this manual page fully (this is the main important part when using the following code):-
$redirectionTime = 5;
$newPageUrl = "wherever_page.php";
header( "Refresh: $redirectionTime; url=$newPageUrl" );
echo "You will now be redirected to a new page, after $redirectionTime seconds. Please be patient...";
exit();
The above code will redirect the user to the "wherever_page.php" page from the existing page after exactly 5 seconds. But you need to do another important thing.
You need to start the Output Buffer first, so that in case you output any HTML before calling the "header()" function, no warning or fatal error will be given. In order to do this, you need to call the following function at the very first line of your web page, whether you include anything or not:-
<?php
ob_start();
// Rest of the web page logic comes after this
The main advantage of the above sets of code is that even if the JavaScript is disabled for that browser, the redirection will still occur.
Hope it helps.
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.