Page 1 of 1

Is there anything wrong with this code?

Posted: Wed Sep 08, 2004 9:27 pm
by zas
Hello,

I used this code to navigate in the site I'm working on..

The code is working but now we are informed by the host that our site is using too much resources in the bin/php. we are suspecting the code to be the source of problem but I don't know whats wrong since I'm a programmer but I just started to learn what I need in php..

if you can have a look at this code and guide me if there is a proplem with it or if it's ok .. I'll appreciate that

Code: Select all

<?php
if(! $_GET['subject']){ 
$subject='main';} 
if ($_GET['section']){ 
if ($section=='news'){ 
$file=$section."/".$subject;} 
else{ 
if ($_GET['subsection']){ 
$file=$section."/".$subsection."/".$subject.$_GET['page'].".php";} 
else{ 
$file=$section."/".$subject.$_GET['page'].".php";} 
} } 
else{ 
$file=$subject.$_GET['page'].".php";} 
if (file_exists($file)){ 
include($file);} 
else{ 
echo "Unable to find the requested page." ;}
?>
the variables:

subject: the name of the page without the extension.. except for the news.

section: the name of the folder

subsection: the name of the subfolder

file: to store the relative path of the file

and I use main.php as the main page in each folder..

and to sum the code in readable way:

If the (subject) is not passed through the url then
---- (subject) = main

if (section) then
---- if it is the news section then
--------- file= news/(subject)
---- else
--------- if (subsection) then
-------------- file= (section)/(subsection)/(supject).php
--------- else
-------------- file= (section)/(supject).php
else
---- file=(subject).php

if the file exist then
---- include the file
else
---- print the error message


I may mention also that the site is popular that it get 1000+ click/day. but we are told by the host that the site is getting 100 hit/second that it made the load in the server and they are also gave a hint that this code maybe the source of the problem..


Thanks,
zas

Posted: Wed Sep 08, 2004 9:58 pm
by redmonkey
In terms of system resource usage, I can see no reason why this code snippet would give you any problems.

Posted: Wed Sep 08, 2004 10:22 pm
by d3ad1ysp0rk

Code: Select all

<?php
if(empty($_GET['subject'])){ $subject = 'main'; }
if(!empty($_GET['section'])){
   $file = $section . "/";
   if ($section == 'news'){
      $file .= $subject;
   }
   else{
      if ($_GET['subsection']){
         $file .= $subsection . "/" . $subject.$_GET['page'];
      }
      else{
         $file .= $subject.$_GET['page'];
      }
   }
}
else{
   $file = $subject.$_GET['page'];
}
$file .= ".php";

if (file_exists($file)){
   include($file);
}
else{
   echo "Unable to find the requested page." ;
}
?>
Added a bit of formatting, empty() calls, and changed the way $file is set a bit

Posted: Wed Sep 08, 2004 10:51 pm
by zas
Thanks a lot!

Posted: Thu Sep 09, 2004 12:16 am
by zas
but can you please tell me.. I'm still somehow confused..

The host is saying that our site is experiencing "burst traffic".. as they mentioned the 100 hit/second..

and asked us to be sure that our code is not open to burst connection

I can't fully understand this.. so I can't judge.

They hinted that the cause of the problem maybe the "presistent request" such as:

"if page is here, display, if not here, display other page."
if that was stated repeatedly, the script would look back on its self, and
attempt to load the same variables over and over again. This would be part of
the reason.
Well.. we were using this code in the index page then we altered to use include header and footer for all the site pages and kept using the code for just the news.

So, I really don't know..

My questions now are:

Is this code open to burst connection?

what are usually the causes of this "burst traffic" problem? some kind of attack for example?


sorry for not mentioning this in the first post.