php crashes apache

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
johnalonso777
Forum Newbie
Posts: 4
Joined: Wed Jan 13, 2010 3:07 pm

php crashes apache

Post by johnalonso777 »

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

Code: Select all

 
<!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>
 
Last edited by johnalonso777 on Wed Jan 13, 2010 8:45 pm, edited 1 time in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: php crashes apache

Post by requinix »

How about you put that all in [syntax=php][/syntax] tags and then we'll talk?
johnalonso777
Forum Newbie
Posts: 4
Joined: Wed Jan 13, 2010 3:07 pm

Re: php crashes apache

Post by johnalonso777 »

what do you mean exactly?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: php crashes apache

Post by requinix »

Edit your post. There's a button in the bottom-right corner.

At the beginning of your code put "[syntax=php]" (without the quotes).
At the end of your code put "[/syntax]" (without the quotes).
Then submit.
johnalonso777
Forum Newbie
Posts: 4
Joined: Wed Jan 13, 2010 3:07 pm

Re: php crashes apache

Post by johnalonso777 »

I have edited my original post, thanks.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: php crashes apache

Post by requinix »

If PHP or Apache crashes, without any warnings or errors, one of the first things to check is for infinite loops. Make sure do/while conditions can be met and that for loops terminate.

Code: Select all

for($i=1;$i<=ceil($pages);$i+1)
That one probably won't. Can you see why?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: php crashes apache

Post by Weirdan »

tasairis wrote:If PHP or Apache crashes, without any warnings or errors, one of the first things to check is for infinite loops.
Usually infinite loop is terminated when max_execution_time has reached. The single most widespread type of crashes for apache / mod_php is a segmentation fault. It's easily identified by 'Segmentation fault' string in main apache error log. Segmentation fault occurs when a program (php or one of its numerous extensions) tries to access a memory area it should not have been accessing (like dereferencing null pointer). First step to debug segmentation fault would be to compile debug build of php:

Code: Select all

./configure --enable-debug `php-config --configure-options` && make && make install
, then configure apache to dump core to file on crash and then wait until crash occurs. Now when you have core file you may use gdb to see where it crashed.

All this assumes you really have a real crash and not some silly error in the script itself.
johnalonso777
Forum Newbie
Posts: 4
Joined: Wed Jan 13, 2010 3:07 pm

Re: php crashes apache

Post by johnalonso777 »

1. for($i=1;$i<=ceil($pages);$i+1)

I'm fairly new to php so I don't see why. I have tried both:

1. for($i=1;$i<=ceil($pages);$i+1)

and

1. for($i=1;$i<=ceil($pages);$i++)

I don't really understand the do and while loops much.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: php crashes apache

Post by requinix »

The third part needs to do something. A verb. $i+1 is just an expression, a noun, same way 1+1 is an expression - it doesn't accomplish anything.
$i++ is like saying "add one": that's a verb because it does something.

But anyways, look at the condition.

Code: Select all

$i<=ceil($pages)
If $i starts at 1 then that condition is (probably) true. What happens to $i next? Well, nothing. $i never changes its value. So the condition remains true. Every time. The loop doesn't quit.

Like Weirdan said, most of the time PHP will terminate an infinite loop (and the entire script) because some amount of time has passed. However sometimes, particularly when the time limit has been disabled with set_time_limit(), the code is just too strenuous on the server and it crashes.

If Apache still crashes after fixing the for loop, Weirdan may be right again in that the problem is a segfault.
johnalonso777 wrote:I don't really understand the do and while loops much.
That's what Google is for. And the PHP manual.
PHP manual
for loops
Post Reply