Solved

Get outputted url

Posted on 2004-10-25
1,124 Views
Last Modified: 2011-09-20
Hi,
I have a very sticky logic problem, let me explain:

Basically there is a remote site something.com with links on the page like this:
<a href=redir.php?siteid=89 target=_blank>

when you click that link, it connects to the database and gets the url of $siteid then opens it in a new window (via header("location: "); I guess)

My program will connect to the remote scipt like so:
http://something.com/redir.php?siteid=89
and it should get the returned URL (this will be saved in the DB)

Any ideas?

Thanks,
Mag
0
Question by:mag1c1an
    10 Comments
     
    LVL 5

    Expert Comment

    by:basiclife
    You could open a socket to something.com and send it a normal set of HTTP headers requesting the specified URL then look at the headers it returns... That'ss skip PHP being redirected without your noticing it
    0
     
    LVL 2

    Expert Comment

    by:Thesolitas
    What you want to do is open up the URL "http://something.com/redir.php?siteid=89" and look at the headers returned, specifically for a Location header, this will tell you the destination of the redirect (if it is redirected).

    Do you currently have a class/function for opening up a URL in PHP and getting the headers? Check if you have CURL installed, information on CURL can be found here:

    http://ca.php.net/manual/en/ref.curl.php

    If you don't have CURL and can't install it, here are some good HTTP classes:

    http://www.phpclasses.org/browse/class/6.html

    One of the classes there that does what you want is:

    http://www.phpclasses.org/browse/package/803.html

    Download that package then use it as follows:

    <?php

    require "URLHelper.php";
       
    $url = 'http://something.com/redir.php?siteid=89';

    echo "HTTP Status for: &quot;$url&quot;<br>";
    echo "The real URL is: " . URLHelper::getRealURL($url) . "<br>";

    ?>


    For more information you can read about the HTTP Status codes here:

    http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html


    0
     
    LVL 2

    Author Comment

    by:mag1c1an
    Hi,
    I tried it using this:

    <?php

    require "URLHelper.php";
       
    $url = 'http://xxxonice.com/ct/cx.php?i=19385&s=85&g=Row1&t=1';

    echo "HTTP Status for: &quot;$url&quot;<br><br><br><br>";
    echo "The real URL is: " . URLHelper::getRealURL($url) . "<br>";

    ?>

    but its giving me a page full of output.....


    I do have cURL installed but dont know how to use it and I find its quite hard to learn really, can you give me a cURL example instaead?

    Thanks,
    Mag
    0
     
    LVL 49

    Expert Comment

    by:Roonaan
    Wel try:

    echo nl2br(htmlspecialchars(filegetcontents($url));

    This will show the contents of the page.

    Quite often javascript is used to do the redirection. The reason redirection is used is to prevent hacking. Because when a phpsessid or other critical info is in the url, this can be passed to other sites by means of the referer address. Therefor they use this kind of redirection.

    Regards

    -r-
    0
     
    LVL 2

    Author Comment

    by:mag1c1an
    Hi,
    Tried that.....not working.
    JS is not being used in this case....I checked it out.

    Thanks anyway.
    Mag
    0
     
    LVL 2

    Expert Comment

    by:Thesolitas
    Here's a cURL example, this is not tested as I don't currently have cURL installed, if you have problems with this, I'm rebuilding some php boxes right now and I'll be putting cURL on them so I can fully test this later.

    This code is based off some sample code found in the comments here: http://ca.php.net/manual/en/function.curl-getinfo.php

    <?php

    // create curl object
    $ch = curl_init();

    if (!$ch)
    {
        die("Couldn't initialize a cURL handle");
    }

    // set url for curl command
    $url = 'http://xxxonice.com/ct/cx.php?i=19385&s=85&g=Row1&t=1';

    // set some curl options
    $ret = curl_setopt($ch, CURLOPT_URL, $url);
    $ret = curl_setopt($ch, CURLOPT_HEADER, 1);
    $ret = curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    $ret = curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
    $ret = curl_setopt($ch, CURLOPT_TIMEOUT, 30);

    $ret = curl_exec($ch); // execute curl

    if (empty($ret))
    {
        die(curl_error($ch));
        curl_close($ch); // close cURL handler
    }
    else
    {
        // fetch curl info
        $info = curl_getinfo($ch);
        curl_close($ch); // close cURL handler

        if (empty($info['url']))
        {
            die("No effective url was returned");
        }
        else
        {
            print "The final url was: '" . $info['url'] . "'<br>";
        }
    }

    ?>


    Try that, let me know how it works out. I'll test this later when I get curl on my new box.
    0
     
    LVL 2

    Author Comment

    by:mag1c1an
    Hi,
    Works pretty good but its not only getting me the headers and also the final url but also displaying the target page....all i need is th final url.

    Your solution is working too well, too many details:
    first its displaying the headers of the first redirect, then the second redirect then the final page's headers.
    Then its fetching the entire final pages code
    then its displaying the final pages address.....

    I think you are nearly there, but if you want to give up...I will award you the points...


    Thanks,
    Mag
    0
     
    LVL 2

    Accepted Solution

    by:
    I never give up : )  I'll get my box up so I can test this myself. I'm sure its a matter of the cURL options (set using curl_setopt).

    If you'd like to play around with it now (its going to be later today when I get my box up), the docs for curl_setopt are here:

    http://ca.php.net/manual/en/function.curl-setopt.php

    Just glancing at it, one thing I would try is changing:

    $ret = curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);

    TO:

    $ret = curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    This will tell curl to output the contents to the $ret variable in this command:

    $ret = curl_exec($ch); // execute curl

    Instead of outputting it to the screen (this should help).

    Try that, see if it works. *crosses fingers*
    0
     
    LVL 2

    Expert Comment

    by:Thesolitas
    Another possibility is to use the CURLOPT_MUTE option, this mutes all the curl functions.

    Set this using:

    $ret = curl_setopt($ch, CURLOPT_MUTE, 1);
    0
     
    LVL 2

    Author Comment

    by:mag1c1an
    Hey,
    I cant relly remember what i did but I think one of your suggestions helped me a bit in finding the solution, so I'm awarding you the points instead of this thread being deleted.

    Thanks,
    Mag
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Cisco Complete Network Certification Training

    If you’re an IT engineer or technician, it's time you take your career to the next level. This elite training bundle is brimming with all of the information you need to learn to sit for Cisco CNNA, CCNP, and CCENT certification exams.

    SQL injection vulnerabilities have been described as one of the most serious threats for Web applications. Web applications that are vulnerable to SQL injection may allow an attacker to gain complete access to their underlying databases. Because…
    A colleague recently asked me about how to give his client a small part of the web site that could be completely under the client's control.  Since I have done this sort of thing before to add emergency banners to a web site, I decided I would creat…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    884 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now