Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Get outputted url

Posted on 2004-10-25
11
Medium Priority
?
1,244 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
Comment
Question by:mag1c1an
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 5

Expert Comment

by:basiclife
ID: 12406397
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
ID: 12406499
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
ID: 12406875
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 49

Expert Comment

by:Roonaan
ID: 12409347
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
ID: 12422924
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
ID: 12423301
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
ID: 12424111
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:
Thesolitas earned 1000 total points
ID: 12424210
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
ID: 12424231
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
ID: 12616842
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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.
Suggested Courses

636 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