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.
Results 1 to 15 of 15
  1. #1
    Regular Coder
    Join Date
    Jul 2004
    Posts
    201
    Thanks
    8
    Thanked 1 Time in 1 Post

    useing ?name=blah - Some help

    Ok, I am not great with PHP and just started learning not long ago. i was wondering how I can use this type of method and what its called.

    Code:
    index.php?name=something
    I have a template and I was wondering if this is possible.

    Have a table in my templat that holds the content. But I want the content to load from a php file.

    So for example, if the address is index.php?name=shared

    Then I want the contents of shared.php to be loaded inside this table. I think it is possibl and I should e able to mang on my own I just need to Know how to use the above statments:

    Code:
    index.php?name=something
    Thanks for any time and effort

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If you just want to change the included file, sure, that's straight forward enough. Just do something like:

    PHP Code:
    if(isset($_GET['name']) {
        include(
    $_GET['name'] . '.php');


  • #3
    Regular Coder
    Join Date
    Mar 2004
    Location
    Australia
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post
    PHP Code:
    <? 
    function validate_page($name

        
    preg_match("/(http(s)?:\/\/)?((www\.)?([^\/]+)\.([\w+]{0,4}))(\/(.*))?/"$name$check); 
        
    /* Here you can decide if you want the format in url to exist. 
        If you do, add it to your $allowed sites var. */ 
        
    $allowedsites = array("urlone.com""urltwo.com"); 
        if (!empty(
    $check[3])) 
        { 
             
    // $check comes back as valid, matches something... 
             
    if ($allowedsites
             { 
                  if (
    in_array($check[3], $allowedsites)) 
                  { 
                       return 
    true
                  } 
                  else 
                  { 
                       return 
    false
                  } 
             } 
             else 
             { 
                  
    // Nothing in allowed sites, though there is a domain found, false it. 
                  
    return false
             } 
        } 
        else 
        { 
             return 
    true
        } 
        } 
        
        
    // Then call it on page: 
        
    if (validate_page($_GET['name'])) 
        { 
             include(
    $_GET['name'] . ".php"); 
        }  
        else
        {
             
    // include the error file
             
    include('error.php');
        }
    ?>
    I had a few problems with people remotely running scripts through my URL and it was considered a security risk so my site got shut down. So id consider taking a bit of caution when using something like this. Above is a work around that checks to see if the page your including is located on your domain. Not really a need for regex but why not

    edit: changed var name in preg_match
    Last edited by Serex; 06-28-2005 at 03:26 AM.

  • #4
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Simple way.
    PHP Code:
    $pages = array("index","contents","news","etc");

    if(
    in_array($_GET['name'],$pages)){
        include(
    $_GET['name'].'.php');
    }else{
        include(
    "error.php");


  • #5
    Regular Coder
    Join Date
    Jul 2004
    Posts
    201
    Thanks
    8
    Thanked 1 Time in 1 Post
    Thanks guys for all replies, I couldnt get first on to work. Second on from serex worked fine, but gixes me an error on line 4 all the time:

    Code:
    Notice: Undefined variable: page in c:\program files\easyphp1-8\www\test.php on line 4
    Nightfire: your code looks good too, but I dont wanna have to list all the pages
    Thanks all of you.

  • #6
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts
    on line 4, change page to name

  • #7
    Regular Coder
    Join Date
    Jul 2004
    Posts
    201
    Thanks
    8
    Thanked 1 Time in 1 Post
    OK, guys, I think am gonna go with serex's solution

    But I have one more proble. SeeIT seemed to fix the above problem, but when no page is called, for example if index.php is just called, it dosn't seem to call error.php

    Does anyone know why?

    Also, what is this called, useing the index.php?name=blah

    thanks again guys, great job

  • #8
    Regular Coder
    Join Date
    Mar 2004
    Location
    Australia
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post
    PHP Code:
    if (validate_page($_GET['name'])) 

         include(
    $_GET['name'] . ".php"); 
    }  
    else
    {
         
    // include the error file
         
    include('error.php');

    it checks to see if the page is valid. and if not it includes an error page that you could create. so it would only include the error page if say the file trying to be included was located somewere else i.e www.domain.com?name=www.domain2.com/script

    we can add a check to see if the page is set...
    What you could also do is add

    PHP Code:
    if (validate_page($_GET['name'])) 

        if (isset(
    $name))
        {
            include(
    $_GET['name'] . ".php"); 
        }
        else
        {
            
    // this is the main page thats loaded up when the site 
            // is visited
            
    include("home.php"); 
        }
    }  
    else
    {
        
    // error page displayed when a page that you dont allow
        // is trying to be included
        
    include("error.php");

    that checks to see if you have set a value for name. If not it includes the "home.php" page.

    hope it helps
    Last edited by Serex; 06-28-2005 at 03:24 AM.

  • #9
    Regular Coder
    Join Date
    Jul 2004
    Posts
    201
    Thanks
    8
    Thanked 1 Time in 1 Post
    Thanks serex, that worked but still gabe me errors,

    I tried to do my own, lol, and here is what I got.

    PHP Code:
    if ($_GET['name'] == "") {
        include (
    "Main.php");
    } else {
        if (
    validate_page($_GET['name'])) {
        include(
    $_GET['name'] . ".php");
        } else {
        include 
    "error.php";
        }

    If i just call index.php then it shows me Main.php but gives this line above it:

    Code:
    Notice: Undefined index: name in c:\program files\easyphp1-8\www\platrip\pages\page.php on line 34
    Line 34 =
    Code:
    if ($_GET['name'] == "") {
    If I call a page such as index.php?name=hello then it shows me the hello page with no problems. If i call a page that dosnt exsist, then it gives me errors saying that can't find file on line woteva.

    Can anyone fix these problems?

    Thanks again for your time

  • #10
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Change the first line to

    PHP Code:
    if (!isset($_GET['name']) || $_GET['name'] == "") { 

  • #11
    Regular Coder
    Join Date
    Jul 2004
    Posts
    201
    Thanks
    8
    Thanked 1 Time in 1 Post
    Yep, that worked SeeIT, thanks

    Ok, now am gonna go and play around with this, my next task is to make the right blocks disappear if r=n lol.

    I might be back

    Thanks all

  • #12
    Regular Coder
    Join Date
    Jul 2004
    Posts
    201
    Thanks
    8
    Thanked 1 Time in 1 Post
    OK, am back again. Now am trying to make the right blocks disssapear only if r=n, if it dosnt = n and its not even set, then it should just show the blocks. Here is what i have:

    PHP Code:
            <?
            
    //See if right blocks is needed
            
    if (!isset($_GET['r']) || $_GET['r'] == "n") {  
                echo 
    "<table width=\"0\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
            } else {
                echo 
    "<table width=\"160\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
            }
            
    ?>
                  <tr>
                    <?
                    
    if (!isset($_GET['r']) || $_GET['r'] == "n") { 
                        echo 
    "";
                    }else{
                        include (
    "includes/right.php");
                    }
                    
    ?>
    This works without any php errors, but the only problem is, if r is not set, ie it isnt called, then it seems to hide the blocks, if i do index.php?r=f then it shows the blocks. If i do r = n then it hides them, so it works, except for the fact that if its not called, it hides then anyway.

    How can I fix this?

  • #13
    Regular Coder
    Join Date
    Jul 2004
    Posts
    201
    Thanks
    8
    Thanked 1 Time in 1 Post
    Ah, I've just seen that the code had a "!" in it, which I think means, does not or is not, so I took them out on both if statments, and now the blocks show, only I get these errors:

    Code:
    Notice: Undefined index: r in c:\program files\easyphp1-8\www\platrip\index2.php on line 219
    
    Notice: Undefined index: r in c:\program files\easyphp1-8\www\platrip\index2.php on line 227
    both lines are the begining of the if statments. If I type r=n then the errors dont show.

    Help

  • #14
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Try this

    PHP Code:
            <?
            
    //See if right blocks is needed
            
    if (isset($_GET['r']) && $_GET['r'] == "n") {  
                echo 
    "<table width=\"0\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
            } else {
                echo 
    "<table width=\"160\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
            }
            
    ?>
                  <tr>
                    <?
                    
    if (isset($_GET['r']) && $_GET['r'] == "n") { 
                        echo 
    "";
                    }else{
                        include (
    "includes/right.php");
                    }
                    
    ?>

  • #15
    Regular Coder
    Join Date
    Jul 2004
    Posts
    201
    Thanks
    8
    Thanked 1 Time in 1 Post
    Yep, that worked great, THANK YOU !!!!!


  •  

    Posting Permissions

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