open new php window

Asked

Viewed 15,380 times

5

On the site I have several advertising banners. When I click on a banner, I am redirected to a page that counts clicks on the banner. After this count I am redirected to the page before the banner click and a new tab opens with the banner link. For that I have the following:

// $go_to_url é o link do banner
echo "<script>window.open('".$go_to_url."', '_blank');</script>";

// redirecciona para a página anterior ao clique no banner
$last_url = $_SERVER['HTTP_REFERER'];
echo "<script>window.location.replace('".$last_url."');</script>";

So far so good, but there is a problem that can happen, the browser ask permission to open pop-up windows, and in this case the client does not have time to accept because it is soon redirected to the previous page. Is there any way around this problem? For example when using php’s header() function, but this function does not allow you to open another tab.

  • If the banner link directs to a new one and then uses the header solves your problem?

  • if I use the header I am redirected outside the site and this is not what I want. I want to stay on the site and in another tab the banner page is opened

  • That’s exactly what I said in the reply below.

2 answers

7


Good morning, I suggest that the new page opens in another window and your page stays in the browser. You can do it this way: assuming your page is index.php

on your page:

<a href="index.php?redir_to=<?php echo $go_to_url; ?> target="_blank">">Banner</a>

in php do the following

<?php
    $redir_to = $_GET['redir_to'];
    //GRAVAR O CONTADOR PARA $redir_to EM BD OU txt conforme seu caso

    header("Location: $redir_to");
?>

3

Another alternative would be to do click by ajax accounting:

HTML:

<a href="<?php echo $go_to_url; ?>" class="anuncio" target="_blank">Anúncio</a>

jquery:

$('.anuncio').click(function() {
    $.get('contabiliza.php', { url: $(this).attr('href') });
});

PHP: accounting.php

<?php
if (isset($_GET['url'])) {
// proceder com a contabilidade.
}
?>

The advantage of this method is that the link is completely transparent.

  • exactly, it would also be a viable alternative! Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.