tags:

views:

67

answers:

2

This script crashes apache. I removed url on purpose. Can anyone take a look and offer alternatives? Thanks!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript" language="javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"&gt;&lt;/script&gt;
<script language="javascript">
$(document).ready(function ()
        {
            $("#btn_Start").attr("disabled",false);
            setTimeout(function(){Doextract();},2000);
        });

function Doextract()
{
    if($("#stop").val() == "1")
    {
        $("#btn_Start").attr("disabled",true);
        window.location.reload();
    }
}

function stop()
{
    if($("#stop").val() == "1")
    {
        $("#stop").val("0");
        $("#btn_Start").val("Start");
    }
    else
    {
        $("#stop").val("1");
        $("#btn_Start").val("Stop");
        $("#btn_Start").attr("disabled",true);
        window.location.reload();

    }
}
</script>
</head>
<body>
<input type="hidden" value="1" id="stop" />
<input type="button" value="Get citys"  id="btn_Start" onclick="stop();" /></div>
<div id="showResult"></div>

  <?php 
 set_time_limit(0);
ini_set('memory_limit', '-1');
$url = "";
include_once 'simple_html_dom.php'; 
include_once 'conn.php';
$id = $_GET['id'];
$sql = "select * from page_category where category_tag = 0 and city_id = '".$id."' LIMIT 0 , 1";
$rst = mysql_query("$sql",$link);
if($rst > 0)
{
      $details = mysql_fetch_row($rst);
      $cateid = $details[0];
      $count = mysql_num_rows($rst);
        $html = file_get_html($url.$details[3]);
        $sql = "delete from page_data where category_id =".$cateid;
        $rst = mysql_query("$sql",$link);
        foreach ($html->find('input#search-find') as $e)
        {
            $categoryname = $e->value;
        }
        foreach ($html->find('div#toolbar-top') as $e)
        {
            foreach ($e->find('strong') as $c) 
            {
                $temp = split('b',$c->plaintext);
                $total = $temp[0];
                $pages = $total/25;
                //echo $total."<br>";
                //echo $pages;
            }
        }
        $j = 1;
        //echo $pages;
        for($i=1;$i<=ceil($pages);$i+1)
        {
                $urls = $url.$details[3]."?page=".$i;
                //echo $urls;
                $html = file_get_html($urls);
              foreach ($html->find('div.description') as $e)
            {
                $name = "";
                foreach ($e->find('h2') as $c) 
                {
                    $name = $c->plaintext;
                }
                $address = "";
                foreach ($e->find('span.street-address') as $c) 
                {
                    $address = $c->plaintext;
                }
                $locality ="";
                foreach ($e->find('span.locality') as $c) 
                {
                    $locality = $c->plaintext;
                }
                $region ="";
                foreach ($e->find('span.region') as $c) 
                {
                    $region = $c->plaintext;
                }
                $code ="";
                foreach ($e->find('span.postal-code') as $c) 
                {
                    $code = $c->plaintext;
                }
                $tel ="";
                foreach ($e->find('li.number') as $c) 
                {
                    $tel = str_replace('(','',$c->plaintext);
                    $tel1 = str_replace(')','',$tel);
                    $tel2 = str_replace('-','',$tel1);
                    $tel3 = str_replace(' ','',$tel2);

                }
                $email = "";
                foreach ($e->find('a.email') as $c) 
                {
                    $email = str_replace('mailto:','',$c->href);
                }

                $sql="insert into page_data (category_id,name,category,address,city,state,postalcode,telnumber,email) values ";
                $sql.="(".$cateid.",'".$name."','".$categoryname."','".$address."','".$locality."','".$region."','".$code."','".$tel3."','".$email."')";
                    //echo $sql;
                $res = mysql_query("$sql",$link);

            }
        }
        $sql = "update page_category set category_tag = 1 where id ='".$cateid."'";
        //echo $sql;
        $res = mysql_query("$sql",$link) or die(mysql_error());
        echo "Category: \"".$details[2]."\" is done!";
}
else 
{
    echo "All categories are done!";
    //$sql = "update page_city set city_tag = 2 where id =".$id;
    //$rst = mysql_query("$sql",$link);
}

  ?>
</body>
</html>
+1  A: 

Are you sure it actually crashes apache and isn't just an error in your script? Are you displaying errors?

To show errors, place this at the top of your page:

error_reporting(E_ALL);
ini_set('display_errors', '1');

If that doesn't work, I would keep taking away code until it starts to work and then slowly build it up until it breaks again. Then, you'll know the trouble spot.

easement
+1  A: 

Can you let us know what you mean by crash? Does the script not finish executing? Does Apache return a 500 status?

In general practice, you should simplify/take out where possible areas of code until you can get it to break so you can find the statement(s) that cause PHP to kill itself.

For instance, take out the contents of the if block... if the script works, you know your problem is somewhere there. You can also try some strategically placed die calls, which will halt execution and execute and print the output of the argument in the function call. For instance, place:

die(var_dump($details))

after your call $details = mysql_fetch_row($rst); to see if MySQL isn't acting up.

Steven Xu