Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Regular Coder Mooseman's Avatar
    Join Date
    Sep 2010
    Posts
    123
    Thanks
    7
    Thanked 3 Times in 3 Posts

    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!

  • #2
    Regular Coder dan-dan's Avatar
    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>

  • #3
    Regular Coder Mooseman's Avatar
    Join Date
    Sep 2010
    Posts
    123
    Thanks
    7
    Thanked 3 Times in 3 Posts

    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 07:27 PM.

  • #4
    Regular Coder dan-dan's Avatar
    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>

  • #5
    Regular Coder Mooseman's Avatar
    Join Date
    Sep 2010
    Posts
    123
    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!

  • #6
    Regular Coder dan-dan's Avatar
    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"

  • #7
    Regular Coder Mooseman's Avatar
    Join Date
    Sep 2010
    Posts
    123
    Thanks
    7
    Thanked 3 Times in 3 Posts
    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.

  • #8
    Regular Coder dan-dan's Avatar
    Join Date
    Aug 2009
    Location
    England
    Posts
    483
    Thanks
    22
    Thanked 79 Times in 78 Posts
    It will work with HTML files, yep

  • #9
    Regular Coder Mooseman's Avatar
    Join Date
    Sep 2010
    Posts
    123
    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 01:32 AM.

  • #10
    Regular Coder dan-dan's Avatar
    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:

    Mooseman (06-07-2012)

  • #11
    Regular Coder Mooseman's Avatar
    Join Date
    Sep 2010
    Posts
    123
    Thanks
    7
    Thanked 3 Times in 3 Posts
    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!

  • #12
    Regular Coder dan-dan's Avatar
    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"

  • #13
    Regular Coder Mooseman's Avatar
    Join Date
    Sep 2010
    Posts
    123
    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!

  • #14
    Regular Coder dan-dan's Avatar
    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/???

  • #15
    Regular Coder Mooseman's Avatar
    Join Date
    Sep 2010
    Posts
    123
    Thanks
    7
    Thanked 3 Times in 3 Posts
    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!


  •  
    Page 1 of 2 12 LastLast

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •