PHP refresh page if certain slug or ID - php

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>';
}
}

Related

How to reload a PHP page twice before displaying it completely to a user?

When a user opens a php page, can I make the page to reload by itself for two times before showing the contents of it to the user?
I tried to use:
header("Location: http://url");
but it goes on loop and never loads the page.
This is veeeery unusual, what I could think of is:
URL: page.php
if (!isset($_GET["time"]) && !isset($_GET["done"]))
{
header("Location: http://url.com/page.php?time=1");
exit;
}
else if ($_GET["time"] == 1)
{
header("Location: http://url.com/page.php?time=2");
exit;
}
else if ($_GET["time"] == 2)
{
header("Location: http://url.com/page.php?done=1");
exit;
}
Or you could use sessions, but good luck with that.

Php Header redirects, but doesnt echo after that

It redirects them back to the homepage but I want it to also display a box.
if($m->send()){
header('Location:http://blankwebsite.com/');
echo '<script>
alert("Your Quote Request has been submitted!");
</script>';
}
else{
echo $m->ErrorInfo;
}`
You could save the html in a session variable, display it after redirect and empty the session variable like a flash message at script start up.
if($m->send()){
$_SESSION['redirectMessage'] = base64_encode(utf8_encode('<script>alert("Your Quote Request has been submitted!");</script>'));
header('Location:http://blankwebsite.com/');
}
else{
echo $m->ErrorInfo;
}
On new page request or redirect:
if isset($_SESSION['redirectMessage']) {
echo htmlentities(base64_decode($_SESSION['redirectMessage']), ENT_QUOTES, 'utf-8');
$_SESSION['redirectMessage'] = null;
}
This solution uses sessions, so do make sure session_start() is called at the top of the script.

How To Add A Delay To PhP Header?

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.

URL opening in all browsers even when user not logged in?

My url is opening in all browsers even when I am using sessions. Ex abc.com/123.php without users logged in. This opens up in all browsers. I am using this code.All codes are in < php open/close tags ok codes which ia m using are
<?php
session_start();
if (isset($_SESSION['LAST_REQUEST_TIME'])) {
if (time() - $_SESSION['LAST_REQUEST_TIME'] > 600) {
// session timed out, last request is longer than 10 minutes ago
unset($_SESSION);
session_destroy();
header("location:userlogin.php");
}
} else {
$_SESSION['LAST_REQUEST_TIME'] = time();
}
if($_SESSION['username']=="");
?>
I am not sure that this compiles well. Because there are a couple of problems.
Be careful with this line of code:
if($_SESSION['username']=="");
This means that true part of this if statement finishes at semicolon.
Second thing is that your else part is never executed but printed as regular HTML.
I would write it like this:
<?php
session_start();
if (isset($_SESSION['LAST_REQUEST_TIME'])) {
if (time() - $_SESSION['LAST_REQUEST_TIME'] > 600) {
// session timed out, last request is longer than 10 minutes ago
unset($_SESSION);
session_destroy();
header("location:userlogin.php");
}
} else {
$_SESSION['LAST_REQUEST_TIME'] = time();
}
if(isset($_SESSION['username'])) {
?>
///////SOME HTML CODE/////
<?php
} else {
header("location:to_some_login_page.php");
}
?>
And I believe that is what you intended to to with closing <?php tag.
Also for readability I suggest you to do just this:
if ($_SESSION['username']!="") {
header("location:to_some_login_page.php");
}
So you don't even need else part, because as soon as header is set, he will be redirected.
Because your
else {
header("location:to_some_login_page.php");
}
is outside of <?php ?>
Try This:
<?php
session_start();
if (isset($_SESSION['LAST_REQUEST_TIME'])) {
if (time() - $_SESSION['LAST_REQUEST_TIME'] > 600) {
// session timed out, last request is longer than 10 minutes ago
unset($_SESSION);
session_destroy();
header("location:userlogin.php");
}
else {
$_SESSION['LAST_REQUEST_TIME'] = time();
}
if($_SESSION['username']=="");
///////your code/////
}
else {
header("location:to_some_login_page.php");
}
?>

PHP Redirect with Cookies

How do I redirect to a splash page once with cookies?
I'm setting the cookie on my splash.php page to this:
<?php
$expire = time()+60;
setcookie("no_splash", "1", $expire);
?>
On that page there's a link to my index.php with this:
<?php
if($_COOKIE['no_splash']== '1') {
header("Location: index.php");
echo "it works";
} else if($_COOKIE['no_splash']!= '1') {
header("Location: splash.php");
};
?>
I keep getting a redirect loop error but can't figure why.
You are redirecting to index.php from the index.php file, hence the loop.
Change your code to be simply
if($_COOKIE['no_splash'] != '1') {
header("Location: splash.php");
exit;
}
or indeed
if(!$_COOKIE['no_splash']) {
header("Location: splash.php");
exit;
}
which is the same thing.
$expire = time()+60;
header("Set-Cookie: no_splash=1; expires=$expire; path=/");
header("Location: index.php");
Have you tried simply:
<?php
if($_COOKIE['no_splash']== '1') {
echo "it works";
} else {
header("Location: splash.php");
};
?>
Maybe isset($_COOKIE['no_splash']) rather than `$_COOKIE['no_splash']== '1'?
Also, not sure it this is what you want, but you can set simply not set the expiration time (or set it to 0), and it will delete the cookie when the browser is closed, so if they keep it open, they won't have to go back to the splash.

Categories