My site keeps overloading, please look at the code!

Hey everyone. My site keeps overloading and getting errors. Heres the code:

<?php
@mysql_close();
session_start(); # Create a new session
ob_start(); // Start output buffer
error_reporting(0);

function getmicrotime(){
list($msec, $sec) = explode(" ",microtime());
return ((float)$msec + (float)$sec);
}

define('TIME_START', getmicrotime());
$HTTP_REFERER = $_SERVER['HTTP_REFERER'];

$_LOGIN = (!isset($_SESSION['USERNAME']) || !isset($_SESSION['USERID']) || !isset($_SESSION['PASSWORD'])) ? $_COOKIE : $_SESSION;
define('DOC_ROOT', $HTTP_SERVER_VARS['DOCUMENT_ROOT']);
define('SELF', $HTTP_SERVER_VARS['PHP_SELF']);
define('CUR_TIME', time()+10800);
define('USERID', $_LOGIN['USERID']);

class Setup {

  function inc($php_incs, $js_incs) {
   ?>
   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   <html xmlns="http://www.w3.org/1999/xhtml"><head>        
        
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <meta name="description" content="">
        <meta name="keywords" content="">
        <meta name="robots" content="index, follow, noarchive">
        
        <link rel="stylesheet" href="/css/template_css.css" type="text/css">
        <link rel="stylesheet" href="/css/dropdown.htm" type="text/css" media="screen, projection">
        <link rel="shortcut icon" href="http://www.thecarshowcase.com/favicon.ico">
        
        <script type="text/javascript" src="index_files/javascript.htm"></script>
        <script type="text/javascript" src="index_files/external.htm"></script>
   <?php
   
   // Check for neccessary PHP functions
    //if ( !array_search('valid_user', $php_incs) ) {
         str_replace('get_user_info', '', $php_incs);
     array_push($php_incs, 'valid_user');
       array_push($php_incs, 'user_panel');
         array_push($php_incs, 'get_user_info');
         array_push($php_incs, 'resize_image');
         array_push($php_incs, 'showroom');
   // } # The get_advertisements() func is included on do_header.php
   // End checking for neccessary PHP functions
   // Check for neccessary Javascript functions
    if ( !array_search('Login', $js_incs) ) {
     array_push($js_incs, 'Login');
    } if ( !array_search('AdvertiseSolutions', $js_incs) ) {
       array_push($js_incs, 'AdvertiseSolutions');
      } if ( !array_search('Signup', $js_incs) ) {
         array_push($js_incs, 'Signup');
        } if ( !array_search('Search', $js_incs) ) {
           array_push($js_incs, 'Search');
          }
    // Finish checking for neccessary Javascript functions
   
   foreach($php_incs as $src) {
     if ($src != '' && !is_callable($src)) {
     require($_SERVER['DOCUMENT_ROOT'] . '/tcsrebuilt/funcs/' . $src . '.php');
     }
    }
      echo "<script language='javascript'>
";
      echo "function isset( variable) {
return (typeof(window[variable]) == \"undefined\") ? false : true;
}

";
    foreach($js_incs as $src) {
       if ( !file_exists($_SERVER['DOCUMENT_ROOT'] . '/tcsrebuilt/js/' . $src . '.js') ) {
        $src = ucwords($src);
       } // Check for capitol letters
     require($_SERVER['DOCUMENT_ROOT'] . '/tcsrebuilt/js/' . $src . '.js'); echo "
";
    }
     
      echo "
</script>
";
  } // End __construct
  
    function connect($server='localhost', $user='*****', $pass='*****', $db='tcs_main') {
    @mysql_close();
      $mysql = mysql_connect($server, $user, $pass);
      $db = mysql_select_db($db);
        if (!$mysql) { echo 'Could not connect to MySQL server. MySQL: ' . mysql_error(); }
        if (!$db   ) { echo 'Could not select MySQL database. MySQL: '   . mysql_error(); }
        
         # Get today's timestamp
         $month = date('n');
         $day = date('j');
         $year = date('Y');
          $today = mktime(0, 0, 0, $month, $day, $year);
           unset($month, $day, $year);
         # Insert visit
         $get_visits = mysql_query("SELECT * FROM visits WHERE date = '" . $today . "'");
          if ( @mysql_num_rows($get_visits) == 0 ) {
           # Insert visit for today
            $create = mysql_query("INSERT INTO visits (date,hits,recent) VALUES (" . $today . ", 1, " . time() . ")");
          } else {
             $add_visit = mysql_query("UPDATE visits SET hits = hits+1, recent = " . time() . " WHERE date = " . $today);
          }
            $yesterday = $today - (60*60*24);
             $month = date('n', $yesterday);
             $day = date('j', $yesterday);
             $year = date('Y', $yesterday);
             $yesterday_date = mktime(0, 0, 0, $month, $day, $year);
            $yesterday_visits = @mysql_query("SELECT hits FROM visits WHERE date = " . $yesterday_date);
            if (@mysql_num_rows($yesterday_visits) == 0 || !@mysql_num_rows($yesterday_visits)) {
             define('HITS_YESTERDAY', 0);
            } else {
             define('HITS_YESTERDAY', @mysql_result($yesterday_visits, 0, 'hits'));
            }
             unset($yesterday, $month, $day, $year, $yesterday_date);
    } // End connect
    
    
     function forum_log($user, $forum=0) {
      if ( !is_callable('valid_user') ) {
       // Retrieve valid_user function
         require('funcs/valid_user.php');
      } // End if ( !is_callable(valid user) )
       if ( valid_user() )       {
        $log = "UPDATE users SET forumrefresh = " . CUR_TIME . ", cur_forum = " . $forum . " WHERE id = '" . $user . "'";
         # Update forum online time
          $update_forums_log = mysql_query($log) or die (mysql_error());
           
           return true;
        } // End if (valid_user())
      return false;
     } // End function forum_log
     
     
       function mark_read($topic_id, $forum_id) {
       if ( !is_callable('valid_user') ) {
        // Retrieve valid_user function
         require('funcs/valid_user.php');
       } // End if ( !is_callable(valid user) )
       
         if (valid_user() === true)    { # Insert into read_topics table
             $get_threads = mysql_query("SELECT * FROM read_topics WHERE topic_id = " . $topic_id . " AND user = '" . $_LOGIN['USERID'] . "' LIMIT 1");
               if ( mysql_num_rows($get_threads) <= 0 ) {
                 $insert = mysql_query("INSERT INTO read_topics (status,user,topic_id,forum_id) VALUES ('1', '" . $_LOGIN['USERID'] . "', '" . $topic_id . "', '" . $forum_id . "')");
               }
         } // End if (valid_user() === true)
       } // end function mark_read
    
    
        function process_id($id) {
         (int)$id; // Cat to INT type
         
         if ( preg_match('#[^0-9]#i', $id) ) {
          return false;
         }
         
         return strip_tags( addslashes($id) );
        } // end process id
        
    
    function forum_jumper($current_id, $user=1) {
            $forums = array(
            '&nbsp;&nbsp;&nbsp;-- All Access -- ', 'general', 'programming', 'flash', 'site help', 'gaming', 'politics', 'clubs/cults', 'promotion', 'art', 'love/hate', '&nbsp;&nbsp;&nbsp;-- reply only --  ', 'news'
                      );
            $ids = array(0, 1, 2, 3, 4, 5, 6, 8, 12, 13, 14, 0, 7);
            
            if ($user > 2) {
            $forums[] = '&nbsp;&nbsp;&nbsp;-- special access --  ';
            $ids[] = 0;
             $forums[] = 'mod\'s lounge';
             $ids[] = 11;
            } if ($user > 3) {
             $forums[] = 'admin\'s lounge';
             $ids[] = 9;
            }
            
              for($i = 0; $i < count($forums); $i++) {
               if ($ids[$i] == $current_id) { 
                $options[] = '<option selected class="submission_form" value="' . 
                              $ids[$i] . '">' . ucwords($forums[$i]) . '</option>';
               } else {
                $options[] = '<option class="submission_form" value="' . $ids[$i] . '">' . ucwords($forums[$i]) . '</option>';
               }
              }
              
                    return '<form name="forum_jumper" method="get" action="/mb/forum.php">
                             <select class="submission_form" name="id" onChange="JumpTo(\'forum_jumper\', \'id\');">
                               <option class="submission_form" value="0">Forum Jumper</option>
                               ' . join($options, "
") . '
                             </select>
                            </form>';
    } // end forum_jumper method
    

          function set_permissions($perms) {
           $_LOGIN['PERMISSIONS'] = $perms;
             return true;               
          } // End function set_permissions
          
          function get_permissions($user) {
           
           if ($user == '') {
            return 1;
           }
          
            if (!is_callable('valid_user')) {
             require('funcs/valid_user.php');
            }
            
              if (!valid_user()) {
               //$_LOGIN['PERMISSIONS'] = 1; global $_LOGIN;
               return 1;
              }
              
             if (!is_callable('get_user_info')) {
              require('funcs/get_user_info.php');
             }
             
               //$_LOGIN['PERMISSIONS'] = get_user_info($user, 'permissions'); global $_LOGIN;
                return get_user_info($user, 'permissions');
            
          } // End function get_permissions


  function headers($headers) {
   echo $headers;
  } // end function headers

} // End class Setup
########################################################################################


#########################################################################################
class LargeTemplate {
 function title($title) {
   echo "<title>TheCarShowcase.com! - " . $title . "</title>
";
   echo '<style type="text/css">
          .content {
           position: absolute;
           left: 32px;
           top: 294px;
          }
         </style>';
   echo "
</head>
";
 }
 
  function Build($body, $align="center", $blockquote=false, $ad=true) {
  $_LOGIN = (!isset($_SESSION['USERNAME']) || !isset($_SESSION['USERID']) || !isset($_SESSION['PASSWORD'])) ? $_COOKIE : $_SESSION;
  if ( valid_user() ) {
   $login_info = '<b>Logged in as <a href="panel.php">' . $_LOGIN['USERNAME'] . '</a> &nbsp; &nbsp; 
                    [ <a href="logout.php">Logout</a> ]</b> &nbsp; &nbsp; &nbsp; ';
  } else {
   $login_info = '<!-- <form action="login.php" method="post">                                
                                <table border="0" cellpadding="0" cellspacing="3">
                                    <tbody><tr>
                                        <td class="smallfont">Username:</td>
                                        <td><input name="username" type="text" class="bginput" size="15"></td>
                                        <td class="smallfont" colspan="2" nowrap="nowrap"><label for="cb_cookieuser_navbar">
                                        <input name="cookie" value="1" checked="checked" type="checkbox">Remember me</label>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="smallfont">Password</td>
                                        <td><input name="password" class="bginput" size="15" type="password"></td>
                                        <td align="left"><input name="Submit" class="button" value="Log In" type="submit"></td>
                                    </tr>
                                    <tr>
                                        <td colspan="3" style="text-align: center;">Not a member yet? Click here to 
                                        <a href="signup.php">Register</a></td>
                                    </tr>
                                </tbody></table>
                                
                            </form> -->
                            <!-- / login form -->';
  }
 
  $rand = mt_rand(1, 3);
   $banner = 'logo' . $rand . '.png';
?>

    <body bgcolor="6f6f71">
<div align="center">

    <!-- Set up Page table -->
    <table width="974" cellpadding="0" cellspacing="0" border="0" bgcolor="#FFFFFF">
        <tr>
            <td width="15" height="100%" rowspan="7"></td>
            <td width="944" height="7" bgcolor="#FFA11B"></td>
            <td width="15" height="100%" rowspan="7"></td>
        </tr>

        <!-- Section above header start -->

        <tr>
            <td width="944">
                <table width="944" cellpadding="0" cellspacing="0" border="0">
                    <tr>
                        <td width="203"><a href="http://thecarshowcase.com"><img src="/images/logo.jpg" /></a></td>
                        <td width="741" align="right"><? echo $login_info; ?></td>
                    </tr>
                </table></td>        
        </tr>

        <!-- Section above header End -->

        <!-- Header start -->
        <tr>
            <td width="944"><a href="http://thecarshowcase.com"><img src="http://thecarshowcase.com/images/logo.png" alt="The Car Showcase" /></a></td>
        </tr>

        <!-- Menu Start -->

        <tr>
            <td width="944">
                <div id="nav">
                    <a href="index.php"><img src="http://thecarshowcase.com/images/link_home.gif" alt="HOME" /></a>
                    <a href="contests.php"><img src="http://thecarshowcase.com/images/link_contests.gif" alt="CONTESTS" /></a>
                    <a href="browse.php"><img src="http://thecarshowcase.com/images/link_rides.gif" alt="RIDES" border="0" /></a>
                    <a href="/forums/"><img src="http://thecarshowcase.com/images/link_forums.gif" alt="FORUMS" border="0" /></a>
                    <a href="shop.php"><img src="http://thecarshowcase.com/images/link_shop.gif" alt="SHOP" /></a>
                    <a href="signup.php"><img src="http://thecarshowcase.com/images/link_register.gif" alt="REGISTER" /></a>    
                    <a href="http://tcsbiz.blogspot.com/"><img src="http://thecarshowcase.com/images/link_blog.gif" alt="BLOG" border="0" /></a>                </div></td>
        </tr>

        <!-- Menu End -->
        <!-- Header End -->

        <!-- Start Content -->
        <tr>
            <td valign="top" width="944" align="left">
<?php
 if ($ad === true) {
  echo '<div id="ad"><img src="/images/ad.jpg" width="160"></div>';
 } else {
  echo $ad;
 }
?>
            
            <? echo $body; ?>
            
            </td>
        </tr>
        <!-- End Content -->

        <!-- Start Footer -->
        <tr>
            <td width="944" align="center"><p>&nbsp;</p>
              <p><a href="http://thecarshowcase.com/about.php" class="links">About</a> | <a href="http://thecarshowcase.com/privacy.php" class="links">Privacy Policy</a> | <a href="http://thecarshowcase.com/advertise.php" class="links">Advertise</a> | <a href="http://thecarshowcase.com/forums/sendmessage.php" class="links">Contact Us</a></p>
              <p>© Copyright 2006 <a href="http://www.sicret.net/">Sicret</a> All Rights Reserved.<br />
               Coding/Programming Completed by <a target="_blank" href="http://www.pilot-doofy.com">Dan Lindsey</a>
            
        </p>    </td>
        </tr>

        <tr>
            <td width="944" height="7" bgcolor="#FFA11B"></td>
        </tr>
    </table>
</div></body></html>
  <?php /*
$_LOGIN = (!isset($_SESSION['USERNAME']) || !isset($_SESSION['USERID']) || !isset($_SESSION['PASSWORD'])) ? $_COOKIE : $_SESSION;
    # Check for unread Private Messages
     if ( $_SESSION['pms_read'] != 1 ) {
      $get = mysql_query("SELECT * FROM `pms` WHERE `to` = '" . $_LOGIN['USERID'] . "' AND `read` != 1");
       if ( mysql_num_rows($get) > 0 && valid_user() ) {
        echo '<script language="javascript" type="text/javascript">
var is_read = confirm("You have ' . mysql_num_rows($get) . ' unread private messages.

If you would like to view your message please click OK, otherwise, click cancel.");
                if (is_read == true) {
                 window.location.href="/login/verify/messages.php";
                }
</script>';
          $_SESSION['pms_read'] = 1; 
       } // end if ( rows > 0 )
     } // end if valid_user
     */
  ?>
</body>
</html> 
<?php
  } // End function Build
  
    function close() {     
        @mysql_close(); // Close MySQL connection
        @ob_end_flush(); // End output buffer
    }
} // End class LargeTemplate
?>

I know the code looks a little bloated at first, and it is. But that shouldn’t cause a problem with looping connections and getting 8 million hits in a day for no apparent reason.

Thanks, and sorry if it’s too much code. If you’d like to see some of the functions that require an active MySQL connection and establish one if one isn’t already, I can attach those as well.

Please help me get to the bottom of this, as my site keeps going down because of the overload :frowning:

please guys?

Could you provide whether the php messes up placing it in the mysql or messes up pulling it out,

pretty much, it would help to have some sql database :stuck_out_tongue:

I’m not quite sure of what you mean. However, if this answers your question, I think the stats are getting overloaded with a call to a header() somewhere. Maybe a never ending loop due to an invalid Location?

I ment when you run the script just once how many hits does it add :stuck_out_tongue:

basically asking for more info