0

I currently have around 34k pins/auths generated in my database, and when I run this it just says This webpage is not available and says that apache crashed. I'm not sure what's causing it to crash because it generated 34k so far without problems. If I could get pointers or anything on what's causing my script to crash that would be great. Thanks. Also I'm running this on a local wamp server. So it gave me an apache crash popup, but I can't seem to get it to show again.

EDIT Here's the crash pop up info:

C:\ProgramData\Microsoft\Windows\WER\ReportQueue\AppCrash_httpd.exe_7bb4b47e7db260ba266dfdf76b7ba24f8a5e099_cab_175bb644\WERB55A.tmp.appcompat.txt C:\ProgramData\Microsoft\Windows\WER\ReportQueue\AppCrash_httpd.exe_7bb4b47e7db260ba266dfdf76b7ba24f8a5e099_cab_175bb644\WERB56A.tmp.WERInternalMetadata.xml C:\ProgramData\Microsoft\Windows\WER\ReportQueue\AppCrash_httpd.exe_7bb4b47e7db260ba266dfdf76b7ba24f8a5e099_cab_175bb644\WERB5E9.tmp.mdmp

error log:

[Sat Apr 12 19:17:01.362346 2014] [core:notice] [pid 3124:tid 412] AH00094: Command line: 'c:\\wamp\\bin\\apache\\apache2.4.4\\bin\\httpd.exe -d C:/wamp/bin/apache/Apache2.4.4' [Sat Apr 12 19:17:01.363346 2014] [mpm_winnt:notice] [pid 3124:tid 412] AH00418: Parent: Created child process 940 [Sat Apr 12 19:17:01.599360 2014] [mpm_winnt:notice] [pid 940:tid 308] AH00354: Child: Starting 150 worker threads. [Sat Apr 12 19:17:04.302514 2014] [mpm_winnt:notice] [pid 3124:tid 412] AH00428: Parent: child process 940 exited with status 255 -- Restarting. [Sat Apr 12 19:17:04.389519 2014] [mpm_winnt:notice] [pid 3124:tid 412] AH00455: Apache/2.4.4 (Win64) PHP/5.4.12 configured -- resuming normal operations [Sat Apr 12 19:17:04.389519 2014] [mpm_winnt:notice] [pid 3124:tid 412] AH00456: Server built: Feb 22 2013 22:08:37 [Sat Apr 12 19:17:04.389519 2014] [core:notice] [pid 3124:tid 412] AH00094: Command line: 'c:\\wamp\\bin\\apache\\apache2.4.4\\bin\\httpd.exe -d C:/wamp/bin/apache/Apache2.4.4' [Sat Apr 12 19:17:04.391519 2014] [mpm_winnt:notice] [pid 3124:tid 412] AH00418: Parent: Created child process 2996 [Sat Apr 12 19:17:04.647534 2014] [mpm_winnt:notice] [pid 2996:tid 308] AH00354: Child: Starting 150 worker threads.

code:

<?php
    ini_set('xdebug.max_nesting_level', 1000000); //this is to fix the recursion?
    ini_set('max_execution_time', 3000000); //300 seconds = 5 minutes
    ini_set('memory_limit', -1);
    ini_set('error_reporting', E_ALL);
    //Enter your database connection details here.
    $host = 'localhost';
    $db_name = 'pins'; 
    $db_username = 'root';
    $db_password = ''; 
    date_default_timezone_set('America/Los_Angeles');
    try
    {
        $pdo = new PDO('mysql:host='. $host .';dbname='.$db_name, $db_username, $db_password);
    }
    catch (PDOException $e)
    {
        exit('Error Connecting To DataBase');
    }
    $starttime = microtime(true);
    class lists
    {
        public $pdo;
        function __construct($pdo)
        {
            $this->pdo = $pdo;
        }
        function duplicateCode($code)
        {
            $query = $this->pdo->prepare("SELECT 1 FROM pins WHERE pins_pin = ?");
            $query->bindValue(1, $code);
            $query->execute();
            return $query->fetch();
        }
        function generateCode()
        {
            $code = "AA" . substr(str_shuffle('0123456789'), 0, 8);
            if($this->duplicateCode($code))
            {
                return $this->generateCode();
            }
            else
            {
                return $code;
            }
        }   
        function generatePin($code)
        {
            $query = $this->pdo->prepare("INSERT INTO pins (pins_pin) VALUES (?)");
            $query->bindValue(1, $code);
            $query->execute();
            return;
        }
    }
    function post_to_url($url, $data) {
        $fields = '';
        foreach ($data as $key => $value) {
            $fields .= $key . '=' . $value . '&';
        }
        rtrim($fields, '&');

        $post = curl_init();

        curl_setopt($post, CURLOPT_URL, $url);
        curl_setopt($post, CURLOPT_POST, count($data));
        curl_setopt($post, CURLOPT_POSTFIELDS, $fields);
        curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);

        $result = curl_exec($post);

        curl_close($post);
        return $result;
    }
    $list = new lists($pdo);
    for($i = 0; $i < 100000; $i++)
    {
        $code = $list->generateCode();
        $pin = $list->generatePin($code);
    }
    $endtime = microtime(true);
    $duration = $endtime - $starttime; //calculates total time taken
    echo $duration;
?>
1
  • 1
    Check your error log (tail -f /var/log/apache2/error.log if you're *nix). Commented Apr 13, 2014 at 2:19

1 Answer 1

1

Chances are that you're exceeding an internal recursion limit by calling lists::generateCode() repeatedly.

An easy fix for this will be to eliminate the recursion:

function generateCode()
{
    do {
        $code = "AA" . substr(str_shuffle('0123456789'), 0, 8);
    } while ($this->duplicateCode($code));
    return $code;
}

However, there's a more fundamental issue at hand here. Your code is generating codes which begin with two As, then contain 6 digits, all of which must be different. There are only 10! ÷ (10 - 6)! = 151,200 possible codes which fit this pattern - once you have generated all of them, no more will be available, and even this corrected code will loop endlessly. (Additionally, if str_shuffle() does not visit all of the possible permutations — which is entirely possible — the set of possible outputs will be even smaller than this.)

Unless you have a hard requirement that all six digits be different, I would strongly recommend that you instead use any sequence of six digits, e.g.:

  $code = sprintf("AA%06d", rand(0, 999999));
Sign up to request clarification or add additional context in comments.

1 Comment

there needs to be 8 digits. so for example AA00000000 which should be 1 million possible codes.

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.