Jump to content

Dynamic Includes


-Karl-

Recommended Posts

I have my code:

        $url = $_GET['page'];
        $Pages = explode('/', $url);
        include('pages/'.$Pages[3]);

 

This works fine and it includes the correct file, however, is it possible to do something like this?

 

        $url = $_GET['page'];
        $Pages = explode('/', $url);
        include('pages/'.$Pages[3].'?flibble=questview&id='.$Pages[4]);

Link to comment
https://forums.phpfreaks.com/topic/207357-dynamic-includes/
Share on other sites

You cannot pass url parameters as part of the include path. However you can access url parameters from within include files, eg

 

file1.php

<?php
include 'file2.php';
?>

 

file2.php

<?php
echo $_GET['var1'] . ' - ' . $_GET['var2'];
?>

 

Now go to file.php?var1=hello&var2=world

Link to comment
https://forums.phpfreaks.com/topic/207357-dynamic-includes/#findComment-1084086
Share on other sites

Yes, I know that. But I'm facing an issue.

 

What I'm trying to do is using mod rewrite

Options +FollowSymLinks
RewriteEngine on
RewriteRule \.(gif|png|jpg|css|js)$|^index\.php$ - [L]
RewriteRule ^(.*)$ index.php?page=$1 [NC]

 

this will make www.mysite.com/thepage work.

 

I'm now trying to get

 

www.mysite.com/thispage/1

 

to work, where 1 is the number of the page ID. But I just can't find any work around to this.

Link to comment
https://forums.phpfreaks.com/topic/207357-dynamic-includes/#findComment-1084088
Share on other sites

If your urls are setup as site.com/pagename/pageid

 

Thn you can setup a second rule which passes the page id to index.php too

Options +FollowSymLinks
RewriteEngine on
RewriteRule \.(gif|png|jpg|css|js)$|^index\.php$ - [L]

RewriteRule ^(.*)/([0-9]+)$ index.php?page=$1&id=$2 [NC,L]
RewriteRule ^(.*)$ index.php?page=$1 [NC,L]

 

Now you can access the page id with $_GET['id']

Link to comment
https://forums.phpfreaks.com/topic/207357-dynamic-includes/#findComment-1084089
Share on other sites

That's the whole problem, I've tried that and it hasn't worked.

 

This is my index.php:

<?php
function HtmlOut($Str) {
    return htmlentities($Str, ENT_QUOTES);
}
include ('tpl/header.tpl');

if (isset($_GET['page'])) {
    if (preg_match('/^[a-zA-Z0-9-\/]+$/D', $_GET['page'])) {
        if (is_file('pages/'.$_GET['page'].'.php')) {
            $Page=$_GET['page'];
        } else {
            echo '<div id="contenttitle">File Not Found</div>
                        <div class="news">
                        <h1>File not Found</h1>
                              <p>Unfortunately but the file "<span>'.HtmlOut($_GET['page']).'</span>" could not be found.</p>
                              <p>If you believe this is an error, please contact an administrator immediately.</p>
                        </div>';
        }
    } else {
        $Flash='File &quote;'.HtmlOut($_GET['page']).'&quote; not found.';
    }
}
if ((empty($_GET['page'])) && (!file_exists('pages/'.$Page.'.php'))) {
    $Page = 'index';
}
if (is_file('pages/'.$Page.'.php') && file_exists('pages/'.$Page.'.php')) {
include('pages/'.$Page.'.php');
}

include ('tpl/sidebar.tpl');
include ('tpl/footer.tpl');

?>

 

Then I have pages/quests.php which is what displays a table, or the data to an appropriate ID, this is done via two functions.

Link to comment
https://forums.phpfreaks.com/topic/207357-dynamic-includes/#findComment-1084092
Share on other sites


Can you post some example urls your have tested. As I have tested  your code and its working fine for me, although I made a small change to your rewriterule's

RewriteRule ^([a-z0-9-]+)/([0-9]+)/?$ index.php?page=$1&id=$2 [NC,L]
RewriteRule ^([a-z0-9-]+)/?$ index.php?page=$1 [NC,L]

Going to site.com/test/123 yielded:

Array
(
    

 => test
    [id] => 1243
)

And site.com/test yielded:

Array
(
    

 => test
)

This is what I had in pages/test.php

<?php
echo '<pre>'.print_r($_GET,true).'</pre>';
?>

Link to comment
https://forums.phpfreaks.com/topic/207357-dynamic-includes/#findComment-1084098
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.