How To Add A Delay To PhP Header? - php

How To I Add A Delay To header('Location: '.$_GET['q']); In My Code?
<?php
if (isset($_GET['q']) && !empty($_GET['q'])) {
if(filter_var($_GET['q'], FILTER_VALIDATE_URL)){
header('Location: '.$_GET['q']);
}else{
echo "Invalid URL";
}
}

Sleep function will block code to be executed after.
Maybe what you are looking for is html meta tag refresh
Like this:
<META http-equiv="refresh" content="5;URL=http://www.example.com/mypage">
where 5 is measure in seconds.

Related

PHP refresh page if certain slug or ID

I'm trying to get a page refresh once after a set amount of seconds only on a set page.
So something like
if is_page('test.php') {
refresh page 5000(once);
} else
continue
You can do this by jQuery
like
window.setTimeout(function() {location.href="URL of test.php"}, 5000);
in PHP you can:
if(is_page('test.php') && !isset($_GET['r'])){
header("Refresh: 5;url='http://zasite.com/test.php?r=1'");
}
NOTE: This can only be done if the headers have not already been sent http://php.net/manual/ro/function.headers-sent.php otherwise you will end up with a warning and the refresh will not work. A workaround would be:
if(is_page('test.php') && !isset($_GET['r'])){
if(!headers_sent()){
header("Refresh: 5;url='http://zasite.com/test.php?r=1'");
} else {
echo '<meta http-equiv="refresh" content="5" ; url=http://zasite.com/test.php?r=1>';
}
}
OR - PHP with Gyandeep Sharma's JS answer
if(is_page('test.php') && !isset($_GET['r'])){
if(!headers_sent()){
header("Refresh: 5;url='http://zasite.com/test.php?r=1'");
} else {
echo '<script>window.setTimeout(function () {location.href="http://zasite.com/test.php?r=1";}, 5000);</script>';
}
}

if elseif else preg_match windows location

I am creating a form where i want that if someone answer is a certain number like 2 or 7 the will be directed to diffrent pages.
I searched online and came up making this.
<?php
// define variables
$name "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (!preg_match("/^3/",$name)) {
echo "<meta http-equiv="refresh" content="0; URL=http://google.nl/">";
} elseif (!preg_match("/^9/",$name)) {
echo "<meta http-equiv="refresh" content="0; URL=http://osm.nl/">";
}
else {
echo "<meta http-equiv="refresh" content="0; URL=http://facebook.nl/">";
}
}
?>
First i tested it by creating a validation form, which works fine. Trough research i edited the code. But now when i tried to open the page it is point blank.
How do i fix it that i can redirect the inputs
Personally I would Use Switch Statement and the php header function to redirect
In the file your posting the form to:
$page = $_POST['name'];
switch($page)
{
case 2:
header("Location: http://www.google.com");
break;
case 7:
header("Location: http://www.facebook.com");
break;
}
Should do the trick.
So here are all your errors:
Line 3 :$name "": Where is the "="?
Line 7, 9, 12: echo "<meta http-equiv="refresh": If you haven't figured that youself, consider using header as #NathanLeadill said.

PHP, refresh page on insert

this is the updating function:
public function update($id) {
$id = $_GET['id'];
$stmt9 = $this->conn->prepare("UPDATE users SET `name`= :name, `email`= :email WHERE `id` = :id");
$stmt9->bindParam(':name', $this->name);
$stmt9->bindParam(':email', $this->email);
$stmt9->bindParam(':id' , $id, PDO::PARAM_INT);
$stmt9->execute();
if ($stmt9) {
$message = "User updated Sussesfully!";
header('location:');
}else {
header("location:");
}
}
}
Now on update here i want the page to be refresh so i could see the updated data, but here now if it update it's will keep user in edite page, and will show the data of privous entered if i see in database the data has been updated and if i refresh the page with f5 it will show the on edit page is been update with out that when i submit the form it will get update but on the form it will show the prevouse data,
so how i can make the page to get refresh after submitting. on redirection if if redirect to list page it will show that it's been updated, but here i want on mean time stay on edit page and reaload page so i could see the updated data.
regards
Simple:
header('Refresh: 0'); // 0 = seconds
Even you can specify new location
header("Refresh:2; url=new_page.php");
But when working with header function there should not be anything echoed before calling it,
but if you have already echoed anything, then you can use html or javascript:
HTML
<meta http-equiv="refresh" content="0">
<!--here you can also specify new url location-->
<meta http-equiv="refresh" content="0; url=http://url.com/">
JS
window.location.reload();
Update: because you can't use header do this:
if ($stmt9)
{
$message = "User updated Sussesfully!";
echo '<meta http-equiv="refresh" content="0">';
}
else
{
echo '<meta http-equiv="refresh" content="0">';
}
you should redirect to update url rather than reload
eg.
header("location:updateurl?id=1");
You can do it by using jquery.
location.reload();
If you wish to redirect to the exactly same page, you can use variable $_SERVER['REQUEST_URI'].
header('location:' . $_SERVER['REQUEST_URI'] );
This code is familiar with re-write rules if any.
Warning: Cannot modify header information - headers already sent by (ou
You need to switch on output buffering in PHP. If this option is enabled, then you need check if your code doesn't flush output buffer somewhere earlier.
Its simple, change this code:
if ($stmt9) {
$message = "User updated Sussesfully!";
header('location:');
}else {
header("location:");
}
like this
if ($stmt9) {
$message = "User updated Sussesfully!";
header('location: ?message='.$message);
}else {
header("location: ?message=error");
}

Infinite redirect loop from PHP code in Wordpress

I have a PHP script which I will post below. It is a voting website, and my client only wants one user to be able to vote once based on their cookies and IP address.
After voting once, if the cookie or IP is detected as the same they are redirected to a fake voting pg which allows multiple votes. The browser loops between both the legal and duplicate vote pages.
Here is the code, I only added in the exit and die functions after getting this error and seeing online that might be the cause - however adding those functions made no difference.
$q = mysql_query("SELECT * FROM votelog");
while($row = mysql_fetch_array($q))
{
if(($ip = $_SERVER['REMOTE_ADDR']) == $row['ip'])
{
$duplicateIP = true;
}//end if
if(($row['pollid'] == 8))
{
$duplicatePoll = true;
}//end if
}//end while
//check cookies
if(isset($_COOKIE['poll']))
{
$cookieCheck = true;
}//end if
if((($duplicateIP == true) && ($duplicatePoll == true)) or ($cookieCheck == true))
{
show this pg
}//end if
else
{
echo '<meta http-equiv="refresh" content="0; url=/polls/legit" />'; //redirect to legal pg
exit();
die();
}//end else
Any ideas? The other page is the same except that the if and else are switched, like this:
if((($duplicateIP == true) && ($duplicatePoll == true)) or ($cookieCheck == true))
{
echo '<meta http-equiv="refresh" content="0; url=/polls/dupe" />'; //redirect to duplicate
exit();
die();
}//end if
else
{
show this pg
}//end else
P.S - I'm operating in a Wordpress environment
It is hard to guess whats going on there. But if your code is fine, i would expect that you have an caching issue.
Setting the headers (header()) correct, would help to solve that issue. But be carefull, you can make it more worse with setting wrong headers.
So an easy workaround could be to add an ?time() to your url. So the redirected URL would change each second.
echo '<meta http-equiv="refresh" content="0; url=/polls/dupe?'.time().'" />'; //redirect to duplicate
Just a side note:
exit();
die(); // this will never reached, as exit() and die() is the same
About exit() and die()

PHP re-direct after form submitted

I'm trying to redirect people to a PDF after they've submitted the form correctly, before I do this I do some checking of the form fields to make they've been filled out correctly, now I was trying to use header() to do my re-direct, but because I've echoed a number of times before I get an error. Here's my code below, what can I do?
<?php
if(isset($_POST['submit'])) {
if($valid_fname == "Y") {
if($valid_sname == "Y") {
if($valid_company == "Y") {
if($valid_email == "Y") {
}
else {
echo "<p class=\"secText\">Please enter a valid email address</p>\n";
}
}
else{
echo "<p class=\"secText\">Please enter the company you work for</p>\n";
}
}
else {
echo "<p class=\"secText\">Please enter your surname</p>\n";
}
}
else {
echo "<p class=\"secText\">Please enter your first name</p>\n";
}
if(($valid_fname == "Y")&&($valid_sname == "Y")&&($valid_company == "Y")&&($valid_email == "Y")) {
echo "<p class=\"secText\">Thank you for confirming your details, you will be re-directed to \"The Personal Touch\" Whitepaper shortly.</p>\n";
header('Location: http://www.sefasinnovation.co.uk/personal_touch.pdf');
exit();
}
}
?>
EDIT Ok I got it to work with a bit of javascript in the end
if(($valid_fname == "Y")&&($valid_sname == "Y")&&($valid_company == "Y")&&($valid_email == "Y")) {
echo "<p class=\"secText\">Thank you for confirming your details, you will be re-directed to \"The Personal Touch\" Whitepaper shortly.</p>\n";
echo "<script type=\"text/javascript\">\n";
echo " <!--\n";
echo " setTimeout(\"window.location = 'http://www.sefasinnovation.co.uk/personal_touch.pdf';\",5000);\n";
echo "//-->\n";
echo "</script>\n";
}
You could use output buffering to send content only after the headers have been sent.
if(($valid_fname == "Y")&&($valid_sname == "Y")&&($valid_company == "Y")&&($valid_email == "Y")) {
ob_start();
echo "<p class=\"secText\">Thank you for confirming your details, you will be re-directed to \"The Personal Touch\" Whitepaper shortly.</p>\n";
header('Location: http://www.sefasinnovation.co.uk/personal_touch.pdf');
ob_end_flush();
exit();
}
However since there is practically no time to display the message before the redirection is done this approach would be useless.
Your best approach here would probably be to redirect your page to a small HTML page which will trigger a JavaScript redirect after a certain amount of time has passed. Here is the general idea. You should sort the details out.
PHP
if(($valid_fname == "Y")&&($valid_sname == "Y")&&($valid_company == "Y")&&($valid_email == "Y")) {
header('Location: http://www.sefasinnovation.co.uk/notification.html');
exit();
// You could also avoid redirection to an HTML file and output the code directly
echo <<<HTML
<html>
<head>
<title>Enter desired title</title>
<script type="text/javascript">
setTimeout(function(){
window.location = "http://www.sefasinnovation.co.uk/personal_touch.pdf";
}, 5000);
</script>
</head>
<body>
<p class="secText">Thank you for confirming your details, you will be re-directed to "The Personal Touch" Whitepaper shortly.</p>
<p>If this page isn't redirected in 5 seconds please click here.</p>
</body>
</html>
HTML;
}
notification.html (the PHP code above could also spit this code out but only if there was not output on the page previously)
<html>
<head>
<title>Enter desired title</title>
<script type="text/javascript">
setTimeout(function(){
window.location = "http://www.sefasinnovation.co.uk/personal_touch.pdf";
}, 5000);
</script>
</head>
<body>
<p class="secText">Thank you for confirming your details, you will be re-directed to "The Personal Touch" Whitepaper shortly.</p>
<p>If this page isn't redirected in 5 seconds please click here.</p>
</body>
</html>
The additional link in notification.html should allow users to do a manual redirection in case JavaScript is disabled.
try to make a link to redirect the user to the pdf page so the link contains the pdf page url
Thank you for confirming your details
or you can use the meta tag to redirect the user after 2 second
<meta http-equiv="refresh" content="2; url=http://www.sefasinnovation.co.uk/personal_touch.pdf" />
Your problem is here:
if(($valid_fname == "Y")&&($valid_sname == "Y")&&($valid_company == "Y")&&($valid_email == "Y")) {
echo "<p class=\"secText\">Thank you for confirming your details, you will be re-directed to \"The Personal Touch\" Whitepaper shortly.</p>\n";
header('Location: http://www.sefasinnovation.co.uk/personal_touch.pdf');
exit();
}
You can't output anything to the page if you want to send redirection headers. Remove that echo statement and try the page again.
Alternatively, you can write a HTML page with a redirection <meta> tag (you can make it delay like 5 seconds and then redirect. Your message will then work).
Look it up on Google for a tutorial.

Categories