0

This is not a duplicate of Maximum execution time in phpMyadmin; this has nothing to do with phpmyadmin.

On my site, there is a form which sends POST data to a PHP script and loads a new page. When you submit the form, the page loads forever and then results in:

The XXX.XXX page isn’t working XXX.XXX is currently unable to handle this request. HTTP ERROR 500

I thought it may be a traffic issue so I upgraded my hosting SSD and made the website only available to me to test it, but the problem still persisted. Here is my script:

<?php

$used_file  = 'used.txt';
$codes_file = 'codes.txt';

# Generates a random unused code from the code file 
function genCode() {
    global $used_file, $codes_file;
    $codes = file_get_contents($codes_file);
    $codes = explode("\n", $codes);
    $used  = file_get_contents($used_file);
    $used  = explode("\n", $used);
    foreach($codes as $code) {
        if(!in_array($code, $used))
            return $code;
    }
}

# Generate error string from error code
function getError($err) {
    switch($err) {
        case 1: return 'No submit';
        case 2: return 'Wrong password';
        case 3: return 'No password';
    }
}

# Adds generated code to the 'used' codes file
function append_used($code) {
    global $used_file, $codes_file;
    $str = $code . '\n';
    file_put_contents($used_file, $str, FILE_APPEND);
}

# Get user's IP (for cookie handling)
function getIP() {
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
        $ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}

# Cookie handling
# Set a cookie for how many times the form has been submitted by user

$used = 0;

if(isset($_COOKIE['used_twice'])) {
    $used = 3;
}

if(isset($_COOKIE['used_once']) && !isset($_COOKIE['used_twice'])) {
    setcookie('used_twice', getIP(), time()+18000);
    $used = 2;
}

if(!isset($_COOKIE['used_once'])) {
    setcookie('used_once', getIP());
    $used = 1;
}

# Check if all the POST data is correct

$password = $_POST['inputPassword'];
$submit   = $_POST['submit'];

if(isset($password)) {

    if($password == 'test123') {

        if(isset($submit)) {

            $code = genCode();

        }

        else
            $err = 1;

    } else 
          $err = 2;

} else
    $err = 3;



# Now generate the new page
include 'web_functions.php';
getHead('Generated', 'Generated code');
getBody('Generated code');

?>



            <a href="../"><img src="goback.png"></img></a>
            <h2>Code Generated</h2>
            <br/>

            <?php
            if(!isset($err) && isset($thecode) && $used == 1) {
            ?>
            <center>
                <div class="bs-component">
                <div class="alert alert-dismissible alert-success">
                <p>Your code is: </p> <strong> <?php echo $thecode; append_used($thecode); ?> </strong>
                </div>
                </div>
            </div>
            </center>
            <?php
            } elseif(isset($err)) {
            ?>
            <center>
            <div class="bs-component">
            <div class="alert alert-dismissible alert-danger">
            <p>There was an error:</p> <strong><?php echo getError($err); ?></strong>
            </div>
            </div>
            </div>
            </center>
            <?php
                } elseif(!isset($err) && $used != 1){
                ?>
                <center>
                <div class="bs-component">
                <div class="alert alert-dismissable alert-danger">
                <p>You can only use the code generator once every 5 hours. Please try again later.</p>
                </div>
                </div>
                </div>
                </center>
                <?php } else { ?>
                <div class="bs-component">
                <div class="alert alert-dismissable alert-danger">
                <p>There was an unexpected error. Please try again.</p>
                </div>
                </div>
                </div>
                <?php } ?>

                <br/>
                <br/>
                <br/>


                <?php closeTags();
                getFooter(); ?>
      </div>


 </div>


 </div>
</div>

What could be causing this issue?

Edit: The error log says:

PHP Fatal error:  Maximum execution time of 60 seconds exceeded on line 13

Line 13 is within the foreach loop.

Community
  • 1
  • 1
Gerard Way
  • 91
  • 9

0 Answers0