Get outputted url

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
LVL 2
mag1c1anAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
ThesolitasConnect With a Mentor Commented:
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
 
basiclifeCommented:
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
 
ThesolitasCommented:
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
mag1c1anAuthor Commented:
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
 
RoonaanCommented:
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
 
mag1c1anAuthor Commented:
Hi,
Tried that.....not working.
JS is not being used in this case....I checked it out.

Thanks anyway.
Mag
0
 
ThesolitasCommented:
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
 
mag1c1anAuthor Commented:
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
 
ThesolitasCommented:
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
 
mag1c1anAuthor Commented:
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
All Courses

From novice to tech pro — start learning today.