如何在else函数中使用if函数

时间:2013-01-02 13:24:19

标签: php javascript preg-match

我试图在if命令的else函数中使用preg_match函数。

$month1data从CURL获取并已经过检查才能正常工作。

以下代码为:

global $attempt;
$attempt = mysql_escape_string($_GET['attempt']);

if (preg_match('/<td colspan=8(.*)<\/table/s', $month1data, $matches)) {
    //Content found do stuff here

    unset ($ch);
    unset ($cache);
    unset ($firstmonthdata);
    unset ($matches);
} else { // start else preg match
    if ($attempt = '3') { //start if attempt = 3

        echo 'failed 3 times - showing error';
        echo '<script type="text/javascript">
        <!--
        window.location = "http://www.website.com/error.php?error=2"
        //-->
        </script>';

    } // end if attempt = 3
    else { //start if attempt dont = 3

        echo 'keep trying for 3 times';
        $attempt = $attempt +1;
        echo '<script type="text/javascript">
        <!--
        window.location = "http://www.website.com/page.php?email=' . $email . '&password=' . $password . '&attempt=' . $attempt . '"
        //-->
        </script>';
    }// end if attempt dont 3 else
} // end else preg match

然而,无论何时加载页面,它都会直接指向错误页面:

"http://www.website.com/error.php?error=2"

我看了几篇其他帖子,但看不出有什么问题,是否有可能以这种方式实现这些方法,或者只是缺少某些东西?

2 个答案:

答案 0 :(得分:5)

应该是:

if ($attempt === '3') {
             ^^^

您要将3分配给$attempt

甚至更好,因为似乎没有涉及数据库:

$attempt = (int) $_GET['attempt'];

...

if ($attempt === 3) {

编辑除此之外,您最好使用会话进行此类尝试检查,因为访问者可以轻松操作查询字符串。

答案 1 :(得分:2)

以下

if ($attempt = '3')

应该是

if ($attempt == '3')
相关问题