CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   Use PHP to show page identified by URL parameters? (http://www.codingforums.com/showthread.php?t=263790)

Mooseman 06-06-2012 12:11 PM

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! :D

dan-dan 06-06-2012 02:33 PM

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>


Mooseman 06-06-2012 06:10 PM

Thank you! :thumbsup: 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!

dan-dan 06-06-2012 06:47 PM

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>


Mooseman 06-06-2012 07:16 PM

Thanks again! However, the parameter default page is not working. I just get a PHP error saying "implode() : Invalid arguments passed" Thank you!

dan-dan 06-06-2012 07:26 PM

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"


Mooseman 06-06-2012 07:42 PM

Quote:

Originally Posted by dan-dan (Post 1237414)
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.

dan-dan 06-06-2012 07:49 PM

It will work with HTML files, yep :thumbsup:

Mooseman 06-06-2012 11:11 PM

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! :D

dan-dan 06-07-2012 06:55 AM

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";
?>


Mooseman 06-07-2012 01:24 PM

Quote:

Originally Posted by dan-dan (Post 1237574)
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! :D

dan-dan 06-07-2012 04:07 PM

PHP Code:

$url = (isset ($_GET['url']) && file_exists($_GET['url'])) ? "someDirectory/" strtolower($_GET['url']) : "http://www.codingforums.com/showthread.php?t=263790"


Mooseman 06-07-2012 04:58 PM

Still not working. :( If I'm not getting a PHP error, the file_exists is returning as negative. Thank you!

dan-dan 06-07-2012 05:08 PM

Please don't tell me you've left the directory as someDirectory/???

Mooseman 06-07-2012 05:13 PM

Quote:

Originally Posted by dan-dan (Post 1237752)
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!


All times are GMT +1. The time now is 09:03 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.