【问题标题】:Bad Gateway 502 Error after waiting ~2min for the page to load等待页面加载约 2 分钟后出现错误网关 502 错误
【发布时间】:2016-08-19 20:44:23
【问题描述】:

嗨, 我尝试为学校制作一个小表格,自从我创建了函数“reorganizeVector()”以来,一切都运行良好。现在,当我想在浏览器上尝试我的表单(仅此网页)时,它会加载 2~3 分钟,然后我收到 Bad Gateway 502 错误。我几乎可以肯定我的问题出在“reorganizeVector()”函数中。我试图找出我的功能有什么问题,但我找不到我做错了什么。

明天我将添加一些其他文件,例如“index.php”,因为现在是凌晨 3 点 18 分,我必须在早上 5 点起床上学,所以...

如果您能帮助我,将不胜感激。
请帮帮我!

顺便提一句。抱歉我的英语不好,我是法裔加拿大人,我已尽力做到最好。

<?php
if(version_compare(PHP_VERSION, '5.4.0') >= 0)
{
    if(session_status() == PHP_SESSION_NONE)
        session_start();
}
elseif(session_id() == '')
    session_start();

define('MIN_CAR', 3);
define('MAX_CAR', 30);
define('MIN_QTY', 1);
define('MAX_QTY', 1000);

if(isset($_POST['name']) AND isset($_POST['qty']) AND isset($_POST['category']))
{
    $nameValide = (strlen($_POST['name'])>=MIN_CAR AND strlen($_POST['name'])<=MAX_CAR);
    $qtyValide = ($_POST['qty']>=MIN_QTY AND $_POST['qty']<=MAX_QTY);
    if($nameValide AND $qtyValide)
    {
        filter_var($_POST['qty'], FILTER_SANITIZE_NUMBER_INT);
        if(!isset($_SESSION['vector']))
        {
            $_SESSION['vector'] = array(
                array('name'=>$_POST['name'],
                'qty'=>$_POST['qty'],
                'cat'=>$_POST['category']));
        }
        else
            array_push($_SESSION['vector'], array('name'=>$_POST['name'], 'qty'=>$_POST['qty'], 'cat'=>$_POST['category']));
    }
    else
    {
        if(!$nameValide)
            error('invalid name (between '.MIN_CAR.' and '.MAX_CAR.' caracters)');
        if(!$qtyValide)
            error('invalid quantity (between '.MIN_QTY.' and '.MAX_QTY.')');
    }
}

function error($text)
{
    if(isset($error))
        array_push($error, '<p class="error">'.$text.'</p>');
    else
        $error = array('<p class="error">'.$text.'</p>');
}


//THIS IS THE FUNCTION THAT CAUSE THIS ISSUE BUT I DONT KNOW WHAT IN THE FUNCTION CAUSE IT
function reorganizeVector()
{
    foreach($_SESSION['vector'] as $value)
    {
        if(isset($array))
            array_push($array, array($value['name'], $value['cat']));
        else
            $array = array(array($value['name'], $value['cat']));
    }
    
    $backup = $array;
    $x = 1;
    
    for($i=0; $i<sizeof($array); $i++)
    {
        //name qty cat
        $int = null;
        $index = null;
        do
        {
            $shifted = array_shift($array);
            $index = array_search($shifted, $array)+$x;
            if(!empty($index))
            {
                if(isset($temp))
                    array_push($int, $index);
                else
                    $int = array($index);
            }
            $x++;
        }
        while(!empty($index));
        
        if(isset($temp))
            array_push($temp[$x], $int);
        else
            $temp = array($x=>$int);
        
    }
    
    foreach($temp as $key => $secondary)
    {
        $primary = $_SESSION['vector'];
        $newQty = $primary[$key]['qty'];
        foreach($secondary as $index)
        {
            $newQty += $primary[$index]['qty'];
        }
        $primary[$key]['qty'] = $newQty;
        if(isset($newVec))
            array_push($newVec, $primary[$key]);
        else
            $newVec = array($primary[$key]);
    }
    
    $_SESSION['vector'] = $newVec;
}

//--------------------------------------------------------------------HTML--------------------------------------------------------------------
?>
<h2>Add an article</h2>
<?php 
if(isset($error))
    foreach($error as $value)
        echo $value;
?>
<form method="post" action="">
    <p>
        <label name="name">Name :</label>
        <input type="text" name="name" pattern="^.{3,30}$" title="Between <?php echo MIN_CAR.' and '.MAX_CAR; ?> caracters" required>
        <br>
        <label name="qty">Quantity : </label>
        <input type="text" name="qty" size="4" pattern="(^(0*[1-9]{1}[0-9]{0,2})$)|(^1000$)" title="Value between <?php echo MIN_QTY.' and '.MAX_QTY; ?>" required>
    </p>
    <p>
        <label name="category">Category :</label><br>
        <input name="category" type="radio" value="kitchen" checked> Kitchen<br>
        <input name="category" type="radio" value="electronic"> Electronic<br>
        <input name="category" type="radio" value="other"> Other<br>
        <input name="category" type="radio" value="hardware"> Hardware<br>
        <input name="category" type="radio" value="sport"> Sport<br>
        <br>
        <button type="submit">Validate</button>
    </p>
</form>

<?php
if(isset($_SESSION['vector']))
{
    reorganizeVector();//<---------------------------------------FUNCTION CALLED HERE
?>
<hr>
<table border="1" style="width:400px">
    <tr>
        <th style="width:50%">Name</th>
        <th>Quantity</th>
        <th>Category</th>
    </tr>
    <?php
    foreach($_SESSION['vector'] as $value)
    {
        echo '
        <tr>
            <td>'.$value['name'].'</td>
            <td>'.$value['qty'].'</td>
            <td>'.$value['cat'].'</td>
        </tr>
        ';
    }
    ?>
</table>
<?php
}
?>

【问题讨论】:

  • stackoverflow.com/questions/3185832/… 检查此链接。这可能是由于您的服务器连接问题。
  • 即使我可以加载我网站的所有其他页面?即使当我删除我的 reorganizeVector() 函数时一切正常?

标签: php html session web vector


【解决方案1】:

我认为您在此行中进入了无限循环:while(!empty($index)); 似乎while 循环没有明确定义。

【讨论】:

  • 不要在 cmets 中放太多代码。请编辑您的问题。
  • 如果我将我的While Loop 更改为this,它不应该工作吗?当我的$array 为空时,我的$index 也不应该为空?
  • 我发现了我的问题。我将我的$index 设置为搜索结果加上$x,所以当我的数组为空时,我的$index 等于$x。感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
  • 2016-06-23
  • 2019-10-27
  • 1970-01-01
  • 2021-03-05
  • 2012-08-04
  • 2017-03-28
相关资源
最近更新 更多