Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-06-2012, 12:11 PM   PM User | #1
Mooseman
Regular Coder

 
Mooseman's Avatar
 
Join Date: Sep 2010
Posts: 118
Thanks: 7
Thanked 3 Times in 3 Posts
Mooseman is an unknown quantity at this point
Question 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!
Mooseman is offline   Reply With Quote
Old 06-06-2012, 02:33 PM   PM User | #2
dan-dan
Regular Coder

 
dan-dan's Avatar
 
Join Date: Aug 2009
Location: England
Posts: 483
Thanks: 22
Thanked 79 Times in 78 Posts
dan-dan is on a distinguished road
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>
dan-dan is offline   Reply With Quote
Old 06-06-2012, 06:10 PM   PM User | #3
Mooseman
Regular Coder

 
Mooseman's Avatar
 
Join Date: Sep 2010
Posts: 118
Thanks: 7
Thanked 3 Times in 3 Posts
Mooseman is an unknown quantity at this point
Question

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..
Mooseman is offline   Reply With Quote
Old 06-06-2012, 06:47 PM   PM User | #4
dan-dan
Regular Coder

 
dan-dan's Avatar
 
Join Date: Aug 2009
Location: England
Posts: 483
Thanks: 22
Thanked 79 Times in 78 Posts
dan-dan is on a distinguished road
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>
dan-dan is offline   Reply With Quote
Old 06-06-2012, 07:16 PM   PM User | #5
Mooseman
Regular Coder

 
Mooseman's Avatar
 
Join Date: Sep 2010
Posts: 118
Thanks: 7
Thanked 3 Times in 3 Posts
Mooseman is an unknown quantity at this point
Thanks again! However, the parameter default page is not working. I just get a PHP error saying "implode() : Invalid arguments passed" Thank you!
Mooseman is offline   Reply With Quote
Old 06-06-2012, 07:26 PM   PM User | #6
dan-dan
Regular Coder

 
dan-dan's Avatar
 
Join Date: Aug 2009
Location: England
Posts: 483
Thanks: 22
Thanked 79 Times in 78 Posts
dan-dan is on a distinguished road
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"
dan-dan is offline   Reply With Quote
Old 06-06-2012, 07:42 PM   PM User | #7
Mooseman
Regular Coder

 
Mooseman's Avatar
 
Join Date: Sep 2010
Posts: 118
Thanks: 7
Thanked 3 Times in 3 Posts
Mooseman is an unknown quantity at this point
Quote:
Originally Posted by dan-dan View Post
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.
Mooseman is offline   Reply With Quote
Old 06-06-2012, 07:49 PM   PM User | #8
dan-dan
Regular Coder

 
dan-dan's Avatar
 
Join Date: Aug 2009
Location: England
Posts: 483
Thanks: 22
Thanked 79 Times in 78 Posts
dan-dan is on a distinguished road
It will work with HTML files, yep
dan-dan is offline   Reply With Quote
Old 06-06-2012, 11:11 PM   PM User | #9
Mooseman
Regular Coder

 
Mooseman's Avatar
 
Join Date: Sep 2010
Posts: 118
Thanks: 7
Thanked 3 Times in 3 Posts
Mooseman is an unknown quantity at this point
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..
Mooseman is offline   Reply With Quote
Old 06-07-2012, 06:55 AM   PM User | #10
dan-dan
Regular Coder

 
dan-dan's Avatar
 
Join Date: Aug 2009
Location: England
Posts: 483
Thanks: 22
Thanked 79 Times in 78 Posts
dan-dan is on a distinguished road
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";
?>
dan-dan is offline   Reply With Quote
Users who have thanked dan-dan for this post:
Mooseman (06-07-2012)
Old 06-07-2012, 01:24 PM   PM User | #11
Mooseman
Regular Coder

 
Mooseman's Avatar
 
Join Date: Sep 2010
Posts: 118
Thanks: 7
Thanked 3 Times in 3 Posts
Mooseman is an unknown quantity at this point
Quote:
Originally Posted by dan-dan View Post
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!
Mooseman is offline   Reply With Quote
Old 06-07-2012, 04:07 PM   PM User | #12
dan-dan
Regular Coder

 
dan-dan's Avatar
 
Join Date: Aug 2009
Location: England
Posts: 483
Thanks: 22
Thanked 79 Times in 78 Posts
dan-dan is on a distinguished road
PHP Code:
$url = (isset ($_GET['url']) && file_exists($_GET['url'])) ? "someDirectory/" strtolower($_GET['url']) : "http://www.codingforums.com/showthread.php?t=263790"
dan-dan is offline   Reply With Quote
Old 06-07-2012, 04:58 PM   PM User | #13
Mooseman
Regular Coder

 
Mooseman's Avatar
 
Join Date: Sep 2010
Posts: 118
Thanks: 7
Thanked 3 Times in 3 Posts
Mooseman is an unknown quantity at this point
Still not working. If I'm not getting a PHP error, the file_exists is returning as negative. Thank you!
Mooseman is offline   Reply With Quote
Old 06-07-2012, 05:08 PM   PM User | #14
dan-dan
Regular Coder

 
dan-dan's Avatar
 
Join Date: Aug 2009
Location: England
Posts: 483
Thanks: 22
Thanked 79 Times in 78 Posts
dan-dan is on a distinguished road
Please don't tell me you've left the directory as someDirectory/???
dan-dan is offline   Reply With Quote
Old 06-07-2012, 05:13 PM   PM User | #15
Mooseman
Regular Coder

 
Mooseman's Avatar
 
Join Date: Sep 2010
Posts: 118
Thanks: 7
Thanked 3 Times in 3 Posts
Mooseman is an unknown quantity at this point
Quote:
Originally Posted by dan-dan View Post
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!
Mooseman is offline   Reply With Quote
Reply

Bookmarks

Tags
iframe, parameter, php

Jump To Top of Thread


Thread Tools
Rate This Thread
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:02 PM.


Advertisement
Log in to turn off these ads.