开发者

Same page over and over, different content, same URL

I have 7 different pages. All pages are the same considering the layout, but are different considering the content. The content is provided by a database, depending on开发者_运维问答 what the users click on.

I figured out you have to pass the variable using the URL, and by so defining which content to load, right?

So, this is my menu-item:

<a id="menupag" class="item1" href="index.php?page=groups?group1">

This is my index:

<div class="content">
    <?php 
        include_once ('content/'.$_GET['page'].'.php'); 
    ?>
</div>

But for some reason, when I click on the menu-item, this message appears:

Warning: include_once(content/groups?group1.php): failed to open stream: No such file or directory in httpd.www/new/index.php on line 32 

What do I have to do to let php ignore the last part of the URL (this part is only used to define which data the database has to return) and just listen to the index.php?page=groups?


You pass parameters in URL in a bad way:

You should change it into:

<a id="menupag" class="item1" href="index.php?page=groups&group=group1">

so you will have in $_GET superglobal two values:

$_GET['page'];  // value: groups
$_GET['group']; // value: group1

However using:

include_once ('content/'.$_GET['page'].'.php');

is totally unsafe. The better solution is:

$allow = array('groups', 'about', 'users');
$page = in_array($_GET['page'], $allow) ? $_GET['page'] : 'default';

include_once ('content/'.$page.'.php');


For security reasons you should have a whitelist so people can't include files you don't want them to include. You could use an array for this.

$validPages = array("groups", "home");
if (in_array($_GET['page'], $validPages)
{
    $include = $_GET['page'];
}
else
{
    $include = "home"; //Your default
}

<div class="content">
    <?php 
        include_once ('content/'.$include.'.php'); 
    ?>
</div>

In answer to your question, your querystring looks invalid. Shouldn't it be index.php?page=groups&group=group1?

If not then you need to check for a question mark by doing:

$include = explode('?', $include);
$include = $include[0];


Not good way to use

<a id="menupag" class="item1" href="index.php?page=groups?group1">

Use like

<a id="menupag" class="item1" href="index.php?page=groups&varame=group1">

Otherwise

$page = array_shift(explode('?' ,$_GET['page']));

Or

$page = explode('?' ,$_GET['page']);
// use $page[0];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜