Enjoy an ad free experience by logging in. Not a member yet?
Register .
06-06-2012, 12:11 PM
PM User |
#1
Regular Coder
Join Date: Sep 2010
Posts: 118
Thanks: 7
Thanked 3 Times in 3 Posts
Use PHP to show page identified by URL parameters?
Hello, I've never really used PHP before, and I'd like to know how to load pages into an iframe depending on a url parameter. (I am doing this now with JS, but it is slower, and creates problems for SEO and those with JS disabled.)
I'd like to have the iframe source and page title set server-side into my template page.
Thanks!
06-06-2012, 02:33 PM
PM User |
#2
Regular Coder
Join Date: Aug 2009
Location: England
Posts: 483
Thanks: 22
Thanked 79 Times in 78 Posts
PHP Code:
<?php $url = isset ( $_GET [ 'url' ]) ? $_GET [ 'url' ] : "" ; // Retrieves $_GET variable from URL named 'url' $title = isset ( $_GET [ 'title' ]) ? $_GET [ 'title' ] : "" ; // For title $url = "http://www.codingforums.com/showthread.php?t=263790" ; // Test value $title = "TEST PAGE" ; // Test value ?> <html> <head> <title><?php echo $title ; ?> </title> <style type="text/css"> iframe { width: 100%; height: 100%; border: none; scroll: auto; } </style> </head> <body> <iframe src="<?php echo $url ; ?> "></iframe> </body> </html>
06-06-2012, 06:10 PM
PM User |
#3
Regular Coder
Join Date: Sep 2010
Posts: 118
Thanks: 7
Thanked 3 Times in 3 Posts
Thank you!
Just a couple more questions:
How do I set a default iframe source if the url specified by the parameter, or the parameter, is missing?
How can I pull the title of the page from the iframe and echo it here?
How do I change any uppercase letters in the parameter to lowercase?
Thanks!
Last edited by Mooseman; 06-06-2012 at 06:27 PM ..
06-06-2012, 06:47 PM
PM User |
#4
Regular Coder
Join Date: Aug 2009
Location: England
Posts: 483
Thanks: 22
Thanked 79 Times in 78 Posts
PHP Code:
<?php $_GET [ 'url' ] = "http://www.codingforums.com" ; // Test URL $url = isset ( $_GET [ 'url' ]) ? strtolower ( $_GET [ 'url' ]) : "someDefaultPage.php" ; $file = file ( $url ); $file = implode ( "" , $file ); if ( preg_match ( "/<title>(.+)<\/title>/i" , $file , $m )) $title = $m [ 1 ]; else $title = "someDefaultTitle" ; ?> <html> <head> <title><?php echo $title ; ?> </title> <style type="text/css"> iframe { width: 100%; height: 100%; border: none; scroll: auto; } </style> </head> <body> <iframe src="<?php echo $url ; ?> "></iframe> </body> </html>
06-06-2012, 07:16 PM
PM User |
#5
Regular Coder
Join Date: Sep 2010
Posts: 118
Thanks: 7
Thanked 3 Times in 3 Posts
Thanks again! However, the parameter default page is not working. I just get a PHP error saying "implode() : Invalid arguments passed" Thank you!
06-06-2012, 07:26 PM
PM User |
#6
Regular Coder
Join Date: Aug 2009
Location: England
Posts: 483
Thanks: 22
Thanked 79 Times in 78 Posts
It works fine. Just comment out the test URL and place it in the default field. I'm guessing you just left it as "someDefaultPage.php".
PHP Code:
// $_GET['url'] = "http://www.codingForums.com"; // Test URL $url = isset ( $_GET [ 'url' ]) ? strtolower ( $_GET [ 'url' ]) : "http://www.codingForums.com" ;
06-06-2012, 07:42 PM
PM User |
#7
Regular Coder
Join Date: Sep 2010
Posts: 118
Thanks: 7
Thanked 3 Times in 3 Posts
Quote:
Originally Posted by
dan-dan
It works fine. Just comment out the test URL and place it in the default field. I'm guessing you just left it as "someDefaultPage.php".
PHP Code:
// $_GET['url'] = "http://www.codingForums.com"; // Test URL
$url = isset ( $_GET [ 'url' ]) ? strtolower ( $_GET [ 'url' ]) : "http://www.codingForums.com" ;
That's exactly what I did. Is it restricted to php files? I made it an html file.
06-06-2012, 07:49 PM
PM User |
#8
Regular Coder
Join Date: Aug 2009
Location: England
Posts: 483
Thanks: 22
Thanked 79 Times in 78 Posts
It will work with HTML files, yep
06-06-2012, 11:11 PM
PM User |
#9
Regular Coder
Join Date: Sep 2010
Posts: 118
Thanks: 7
Thanked 3 Times in 3 Posts
I see now better how echo is essentially a server-side variable. What I have:
<iframe src="http://example.com/A/B/<?php echo $url; ?>.html"></iframe> However, the PHP is passing urls of pages that don't exist. How can I fix this? Also, the only thing $title that ever shows is someDefaultTitle.
Thank you!
Last edited by Mooseman; 06-07-2012 at 12:32 AM ..
06-07-2012, 06:55 AM
PM User |
#10
Regular Coder
Join Date: Aug 2009
Location: England
Posts: 483
Thanks: 22
Thanked 79 Times in 78 Posts
The page will accept 'any' value in the url variable at the moment. You need to validate your data first. For instance, you might use
file_exists() to check whether the page exists in your directory, and if not refer to the default page.
The title works for me. Are you sure that your pages actually contain titles?
PHP Code:
<?php $url = (isset ( $_GET [ 'url' ]) && file_exists ( $_GET [ 'url' ])) ? strtolower ( $_GET [ 'url' ]) : "http://www.codingforums.com/showthread.php?t=263790" ; $file = file ( $url ); $file = implode ( "" , $file ); if ( preg_match ( "/<title>(.+)<\/title>/i" , $file , $m )) $title = $m [ 1 ]; else $title = "someDefaultTitle" ; ?>
Users who have thanked dan-dan for this post:
06-07-2012, 01:24 PM
PM User |
#11
Regular Coder
Join Date: Sep 2010
Posts: 118
Thanks: 7
Thanked 3 Times in 3 Posts
Quote:
Originally Posted by
dan-dan
The title works for me. Are you sure that your pages actually contain titles?
This works great now! However, I am having to set the parameter to
directory/filename.html. I would like to just be able to set the filename in the URL. Thank you!
06-07-2012, 04:07 PM
PM User |
#12
Regular Coder
Join Date: Aug 2009
Location: England
Posts: 483
Thanks: 22
Thanked 79 Times in 78 Posts
PHP Code:
$url = (isset ( $_GET [ 'url' ]) && file_exists ( $_GET [ 'url' ])) ? "someDirectory/" . strtolower ( $_GET [ 'url' ]) : "http://www.codingforums.com/showthread.php?t=263790" ;
06-07-2012, 04:58 PM
PM User |
#13
Regular Coder
Join Date: Sep 2010
Posts: 118
Thanks: 7
Thanked 3 Times in 3 Posts
Still not working.
If I'm not getting a PHP error, the file_exists is returning as negative. Thank you!
06-07-2012, 05:08 PM
PM User |
#14
Regular Coder
Join Date: Aug 2009
Location: England
Posts: 483
Thanks: 22
Thanked 79 Times in 78 Posts
Please don't tell me you've left the directory as someDirectory/???
06-07-2012, 05:13 PM
PM User |
#15
Regular Coder
Join Date: Sep 2010
Posts: 118
Thanks: 7
Thanked 3 Times in 3 Posts
Quote:
Originally Posted by
dan-dan
Please don't tell me you've left the directory as someDirectory/???
I'm not that dumb
I changed it to the correct directory, but I get either a PHP error (function.file and function.implode) or my specified 404 page. Here's my entire code:
PHP Code:
$url = (isset ( $_GET [ 'url' ]) && file_exists ( $_GET [ 'url' ])) ? "files/" . strtolower ( $_GET [ 'track' ]) : "http://example.com/404.html" ; $file = file ( $url ); $file = implode ( "" , $file ); if ( preg_match ( "/<title>(.+)<\/title>/i" , $file , $m )){ $title = $m [ 1 ];} else{ $title = "Error" ;}
Thank you!
Jump To Top of Thread
Thread Tools
Rate This Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT +1. The time now is 11:02 PM .
Advertisement
Log in to turn off these ads.