0

I want to temporarily redirect users to an ad page and then return them to their desired page again after 10 seconds.

I don't know how to do this because I don't have much knowledge of PHP or Java. So please provide me complete redirect code and guide me where to put that code.

I am using $_GET parameters on first page so let say my urls are as follow:

mydomain.net/games/?game=PUBG+Mobile&Rating=5
mydomain.net/games/?game=Apex+Legends&Rating=4
mydomain.net/games/?game=GTA+5&Rating=4.5

I want every url to redirect to ads.php page and then redirect back to original url after 10 seconds and never redirect again.

Edward Minnix
  • 2,889
  • 1
  • 13
  • 26
DuckyGray
  • 1
  • 2
  • 4
    java != javascript – Oleksandr Pobuta Jul 11 '19 at 12:41
  • 1
    It doesn't really work like that. When you redirect from one page to another the Document Object Model reloads and any scripts on the new page execute. If the new page redirects back to the original, you'll be stuck in a redirect loop unless you provide some sort of cookie or parameter that tells the first page not to redirect again. I would consider using a modal, popup, or iframe within the original page and remove it from that page after 10 seconds. – h0r53 Jul 11 '19 at 12:44
  • Thankyou @CaitLAN Jenner for reply but I cannot show Google Ads in an iframe or modal it's against google rules so i was looking for a method redirect my traffic from website 1 to website 2. Can you please tell me how can i use parameter or cookies to tells first page not to redirect again? – DuckyGray Jul 11 '19 at 12:53

2 Answers2

2

First of all: Java !== Javascript

Java and Javascript are similar like Car and Carpet are similar.

Source: https://stackoverflow.com/a/245068/3119231

-

To redirect an user to another location you may use:

// Simulate a mouse click:
setTimeout(function() { // timer
    window.location.href = url;
}, 10000); // 10000 ms = 10 seconds

// Simulate an HTTP redirect:
setTimeout(function() { // timer
    window.location.replace(url);
}, 10000); // 10000 ms = 10 seconds

Place it in the documents you like.

Source: https://www.w3schools.com/howto/howto_js_redirect_webpage.asp

Bonus (CaitLAN Jenner): You need to prevent your documents from redirecting in an infinite loop. Your visitors would get pushed from one site to another every 10 seconds.

  • This is true, but it doesn't address the concern of redirecting backwards. As I mentioned in a different comment putting this code on both pages results in an infinite redirect loop unless a cookie or parameter is set to disable this code after the original redirection. Since the OP is using PHP the easiest solution is likely a ?redirect=False parameter in the query string. – h0r53 Jul 11 '19 at 12:49
0

@Maurice mentioned how to perform a HTTP redirect in JavaScript. If you do this on both pages, however, then you will find yourself in an infinite redirect loop, which is very bad. To expand on that answer here is some PHP to dynamically disable the second redirect from the original page through the use of a query string parameter (see https://en.wikipedia.org/wiki/Query_string).

First, you need to include the following PHP code at the top of your document. If you have other PHP code, just put the inside of this code below it. This will accept the redirect query string.

<?php

$redirect = 1

if (isset($_GET['redirect'])) {
    $redirect = htmlspecialchars($_GET["redirect"]);
}

?>

Now, later in the document (preferably at the end of body or within head) you need to dynamically generate the JavaScript using PHP as such.

<?php
if ($redirect == 1)
{
    echo "<script>";
    echo "// Simulate an HTTP redirect:";
    echo "setTimeout(function() { // timer";
    echo "window.location.replace(url);";
    echo "}, 10000); // 10000 ms = 10 seconds";
    echo "</script";
}
?>

Note that you will need to replace "url" here with the appropriate url, which may require properly escaping quotations (see https://www.php.net/manual/en/function.addslashes.php)

As a final note, you need to set the "redirect" query string appropriately on the page that redirects back to the original. You can do so with something like this:

mydomain.net/games/?game=PUBG+Mobile&Rating=5&redirect=0

UPDATE PER REQUEST On the second page you don't need the PHP logic mentioned above. You simply need a JavaScript 10 second redirect. Something like this should work.

<script>
url = "mydomain.net/games/?game=PUBG+Mobile&Rating=5&redirect=0";

// Simulate an HTTP redirect:
setTimeout(function() { // timer
window.location.replace(url);
}, 10000); // 10000 ms = 10 seconds
</script>

Note that here I've used a static URL. If you want this URL to be dynamic you can use the exact same approach I've mentioned for solving the infinite redirect. In other words, pass the original URL as a query string parameter to the ad page, parse it on the ad page through PHP, and use PHP to dynamically create the url in the code above.

h0r53
  • 3,034
  • 2
  • 16
  • 25
  • Can you please give me code for second page (redirecting one). I mean how to set redirect=0 on second page for all the URLs redirecting from 1st page? – DuckyGray Jul 11 '19 at 13:18
  • Try this first (with a static URL) and see if it works for you. Then think about how you can use the same approach to dynamically redirect away from the ad page by sending it a returnURL parameter that you can parse and dynamically generate using the same method I mentioned for solving the infinite redirect problem. Good luck! – h0r53 Jul 11 '19 at 13:28