Jump to content

Archived

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

bachya

Default Case for Dynamic Content

Recommended Posts

Hi all,

I'm fairly new to PHP, and I have a question about dynamic content that's loaded through a URL variable. My site uses a line like this:

<?php include("$_GET[article].html") ?>

...to dynamically include text into a laid out page (in a URL like www.mysite.com/articles.php?article=34).

I like this, because it's simple (whereas, in ASP, I would have to have a large case statement with every possible article). However, I'm wondering if it's possible to have "default content" should someone enter something like: www.mysite.com/articles.php (no variable on the end).

Is this possible, while STILL keeping intact my one-line inclusion from above? Or is something like a case statement the only way to have "default content?"

Thanks! :)

Share this post


Link to post
Share on other sites
you can use a small if statement like
[code]
<?PHP
if (isset($_GET[article])){
     php include("$_GET[article].html");
}
else {
     echo "default content";
}
?>
[/code]

Share this post


Link to post
Share on other sites
You really should be checking the existance of any file before you try to include it anyway, otherwise your current setup has a [b]major[/b] security hole in your site.

All you need do is check the file and querystring exist, if it does, include it, otherwise load a default. eg;
[code]
if (isset($_GET[article])) {
  if (file_exists($_GET[article]."html")) {
    include $_GET[article]".html";
  } else {
    include "default.html";
  }
} else {
  include "default.html";
}
[/code]
PS: All this can easily be done in asp also.

Share this post


Link to post
Share on other sites
[!--quoteo(post=383356:date=Jun 13 2006, 12:51 PM:name=thorpe)--][div class=\'quotetop\']QUOTE(thorpe @ Jun 13 2006, 12:51 PM) [snapback]383356[/snapback][/div][div class=\'quotemain\'][!--quotec--]
You really should be checking the existance of any file before you try to include it anyway, otherwise your current setup has a [b]major[/b] security hole in your site.

All you need do is check the file and querystring exist, if it does, include it, otherwise load a default. eg;
[code]
if (isset($_GET[article])) {
  if (file_exists($_GET[article]."html")) {
    include $_GET[article]".html";
  } else {
    include "default.html";
  }
} else {
  include "default.html";
}
[/code]
PS: All this can easily be done in asp also.
[/quote]

How would I accomplish the above in ASP? For whatever reason, ASP is slightly more confusing to me...

Share this post


Link to post
Share on other sites

×

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.