0

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">
<html xmlns="http://www.w3.org/1999/xhtml">
<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"></script>
<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>
5
  • Check the formatting of your post. It's a bit hard to read, especially with the single-line comments. Commented Jan 13, 2010 at 20:45
  • Explain what you mean by "crashes". And fix the formatting. Commented Jan 13, 2010 at 20:45
  • If I were you, I'd start by fixing the markup, and providing some more info about the error. Some error logs if you can would help too. Commented Jan 13, 2010 at 20:45
  • Are you sure if it really "crashes" the Apache? I think it will work a long time because it's downloading pages from URLs. You also can run it from command line php script.php and see if it'll crash. Commented Jan 13, 2010 at 20:48
  • What is the error message and what does it say in Apache's error log? Commented Jan 13, 2010 at 20:50

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.