Displaying a requested URL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

prejudged_fire
Forum Newbie
Posts: 15
Joined: Sun Dec 18, 2005 6:28 pm

Displaying a requested URL

Post by prejudged_fire »

I want to have an input box where a user can type in a URL and then display that website's content in a designated area of my website but still stay on MY domain name. For example:

http://www.mysite.com

Welcome to My Website

enter URL: http://www.somesite.com/

Then, when they click the "submit" button,

Welcome to My Website
-------------------------------------
Contents of http://www.somesite.com/ displayed here.




But I want the user to stay on the mysite.com url. I guess this is kinda hard to explain but if any of you know what Im talking about, please help me ok? thanks.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

So let me get this straight: you want to input a url via a form and grab the contents of that webpage to display on a section of your webpage?

If thats the case it is very simple. I am assuming you understand the use of $_POST and such...

Code: Select all

if (!empty($_POST['url'])) {
   //grab contents of page
   $contents = @file_get_contents($_POST['url']);

   //if file_getg_contents() fails echo error instead of result   
   echo (!$contents ? 'Could not find the requested page '. $_POST['url'] : $contents);
}
for more information read file_get_contents()
prejudged_fire
Forum Newbie
Posts: 15
Joined: Sun Dec 18, 2005 6:28 pm

Post by prejudged_fire »

well yes you understand what I mean and yes I understand $_POST. However that code simply displays Could not find the requested page no matter what url you input. I tested it with http://www.yahoo.com/ and it could not find it.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Are you sure you are including http:// in the url?

Code: Select all

$_POST['url'] = 'http://www.yahoo.com';
	
	if (!empty($_POST['url'])) {
		//grab contents of page
		$contents = @file_get_contents($_POST['url']);
	
		//if file_getg_contents() fails echo error instead of result   
		echo (!$contents ? 'Could not find the requested page '. $_POST['url'] : $contents);
	}
works fine for me :?
prejudged_fire
Forum Newbie
Posts: 15
Joined: Sun Dec 18, 2005 6:28 pm

Post by prejudged_fire »

yes im sure
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Temporarly remove the @ before file_get_contents to see what the actual error is then
prejudged_fire
Forum Newbie
Posts: 15
Joined: Sun Dec 18, 2005 6:28 pm

Post by prejudged_fire »

Last edited by prejudged_fire on Mon Dec 19, 2005 10:08 am, edited 1 time in total.
prejudged_fire
Forum Newbie
Posts: 15
Joined: Sun Dec 18, 2005 6:28 pm

Post by prejudged_fire »

Parse error: parse error, unexpected '<' in /home/freehost/t35.com/y/o/youcantseeme/index.php on line 21
prejudged_fire
Forum Newbie
Posts: 15
Joined: Sun Dec 18, 2005 6:28 pm

Post by prejudged_fire »

wait actually this is the error

Warning: file_get_contents(): URL file-access is disabled in the server configuration in /home/freehost/t35.com/y/o/youcantseeme/index.php on line 15

Warning: file_get_contents(http://www.yahoo.com): failed to open stream: no suitable wrapper could be found in /home/freehost/t35.com/y/o/youcantseeme/index.php on line 15
Could not find the requested page http://www.yahoo.com
prejudged_fire
Forum Newbie
Posts: 15
Joined: Sun Dec 18, 2005 6:28 pm

Post by prejudged_fire »

this is the coding used for index.html

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
</head>
<body>
<form action="index.php" method="POST">
<input type='text' name='url' value="http://">
<input type="submit" value="Go">
</body>
</html>
this is the code for index.php

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
</head>
<body>
Welcome to Youcantseeme.com!<br>
<hr>
<?php
$_POST['url'] = 'http://www.yahoo.com';
    
    if (!empty($_POST['url'])) {
        //grab contents of page
        $contents = file_get_contents($_POST['url']);
    
        //if file_getg_contents() fails echo error instead of result   
        echo (!$contents ? 'Could not find the requested page '. $_POST['url'] : $contents);
    }
?>
</body>
</html>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

The message is exactly what it sais.. the host has disabled the use of grabbing content from other sites via file_get_contents();

Try doing this

Code: Select all

$url= $_POST['url'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec ($ch); # This returns HTML
curl_close ($ch); 

echo $content;


For the love of god use the edit button..
prejudged_fire
Forum Newbie
Posts: 15
Joined: Sun Dec 18, 2005 6:28 pm

Post by prejudged_fire »

ok sorry about the bumping. That time it worked. But is there a way to remain on my website even when the user clicks on a link? If you havent already guessed, I am trying to make a site similar to Sneakysurf.com where you will remain on the sneakysurf server while viewing other pages.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

iframe? Can't think of anything else outside file_get_contents() and that your host seems to have disabled. An iframe may just do the trick - but the user will be connecting directly to that site for request - so what you would have is basically your site acting as a wrapper around its content.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I've done this before without an iframe using curl, but you require some clever regex to modify the links and develop a way of passing the requested page around.
prejudged_fire
Forum Newbie
Posts: 15
Joined: Sun Dec 18, 2005 6:28 pm

Post by prejudged_fire »

ok so how do i go about doing that?

BTW i am using a different host now that doesnt disable the use of grabbing contents of other urls
Post Reply