Jump to content


Photo

Default Case for Dynamic Content


  • Please log in to reply
3 replies to this topic

#1 bachya

bachya
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 13 June 2006 - 04:45 PM

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! :)

#2 nogray

nogray
  • Members
  • PipPipPip
  • Advanced Member
  • 930 posts
  • LocationSan Francisco CA

Posted 13 June 2006 - 04:51 PM

you can use a small if statement like
<?PHP
if (isset($_GET[article])){
     php include("$_GET[article].html");
}
else {
     echo "default content";
}
?>

NoGray.com


#3 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 13 June 2006 - 04:51 PM

You really should be checking the existance of any file before you try to include it anyway, otherwise your current setup has a major 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;
if (isset($_GET[article])) {
  if (file_exists($_GET[article]."html")) {
    include $_GET[article]".html";
  } else {
    include "default.html";
  }
} else {
  include "default.html";
}
PS: All this can easily be done in asp also.

#4 bachya

bachya
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 14 June 2006 - 06:18 PM

[!--quoteo(post=383356:date=Jun 13 2006, 12:51 PM:name=thorpe)--][div class=\'quotetop\']QUOTE(thorpe @ Jun 13 2006, 12:51 PM) View Post[/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 major 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;
if (isset($_GET[article])) {
  if (file_exists($_GET[article]."html")) {
    include $_GET[article]".html";
  } else {
    include "default.html";
  }
} else {
  include "default.html";
}
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...





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users