Question about header( "Location:http://

by 11 replies
14
Hi,

Actually, I have a slightly different question.
If I use header( "Location:, it will redirect the browser to the URL specified.

I only want to call the URL, not redirect the browser. How can I accomplish that?

Thanks in advance for your help!
#programming
  • if you are using PHP, have a look at cURL library.
    • [ 1 ] Thanks
  • this will just call the url instead of going to it!

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://www.google.com");
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    $result = curl_exec ($ch);
    curl_close ($ch);
    • [ 1 ] Thanks
  • ...and in case you don't have cURL installed, you can use:

    <?php
    $handle = fopen("http://www.google.com/", "rb");
    $contents = stream_get_contents($handle);
    fclose($handle);
    ?>
    • [ 1 ] Thanks
    • [1] reply
  • Not really, I was thinking that perhaps you couldn't POST data using fopen, but then I remembered this: HTTP POST from PHP, without cURL - Evil, as in Dr.

    Bonus: It mentions an even simpler solution for most cases!

    $page = file_get_contents('http://www.google.com/');

    nice!
    • [ 1 ] Thanks
    • [1] reply
    • Well, I'm trying to get filegetcontents to work... with a twist. Here is the HTML

      <a href=http://www.anyurl.com/ target="_blank" onclick="filegetcontents('http://www.specificurl.com/filename.php');">Anchor Text</a>

      To my understanding, this should first call 'specificurl', and then open 'anyurl' in the browser. But I can't seem to get it to work. Can anybody see an obvious syntax error?

      Thanks for your help!
  • Ah...

    Well, that won't work, simply because you are mixing php, html and javascript in a creative but incorrect way.

    What you need to do is to create a php-file where you call the filegetcontents('http://www.specificurl.com/filename.php');

    Then skip the whole "onclick" thing and just point the URL to that php-file.

    Outline:

    1. Create a php file with filegetcontents('http://www.specificurl.com/filename.php'); on your server and name it getpage.php:

    <?php

    echo filegetcontents('http://www.specificurl.com/filename.php');

    ?>

    2. Make the link on your site look like this, without the onclick:

    <a href="http://www.anyurl.com/getpage.php" target="_blank">Anchor Text</a>

    -------

    Now, when the visitor clicks on the link, the getpage.php is called and that one simply reads from the other page and shows the result to the visitor.

    I hope that helps
    • [ 1 ] Thanks
    • [1] reply
    • Thanks for responding again Manfred! i appreciate your help.

      Maybe it would help if I explained my problem.

      I want to 'track clicks'. In other words, if somebody clicks an outbound link on my site, I want to not only open a new browser with the target URL, but I also want to call another URL which will increment a counter for the link and update the DB.

      In this way, I can track not only traffic for my site, but also track clickthrough ratio for different advertisers. At the same time, the <a tag is still readable by the search engines for SEO... right?

      Probably an easier way to get it done?

      Thanks again Manfred!

      Ken
  • Oh... I see

    Yes, that's achievable but there are easier ways to do it than the one's suggessted above.

    I'll just get some sleep first

    Back in some hours...

    PS. If you add me on Skype we can make progress a lot faster, because then we can sort out any questions directly.

    Skype: manfred.ekblad
    • [ 1 ] Thanks
    • [1] reply
    • Hi Manfred! I'm glad to hear there's something better! I don't have a Skype account... and can't really deal with another learning curve 'right now'. Plus, I don't want to monopolise your time. There's no rush on this particular project... just when you have a chance. I do have a telephone if that would be a better use of your time.

      I appreciate your help!

Next Topics on Trending Feed