PHP if / else同时执行

时间:2018-07-06 17:19:27

标签: php if-statement

我的状况很奇怪。我不是PHP的高手,但我不知道为什么,但是如果同时执行其他代码,我的代码将最后执行,而且我猜想,通常这是第一或第二选择。

    function schimbare_pw_confirmata()
{
    if(isset($_POST['SubmitLostPasswordCodeForm']))
    {
        $cont = $_SESSION['user'];
        $newpw = replace($_POST['newPassword']);

        if( (strlen($newpw) < 8) || (strlen($newpw) > 16) )
        {
            $error .= error("Parola trebuie s&#259; con&#355;in&#259; 8-16 caractere.<br>");
        }
        if( !preg_match("#[a-z]+#", $newpw) || !preg_match("#[A-Z]+#", $newpw) )
        {
            $error .= error("Parola trebuie s&#259; con&#355;in&#259; cel pu&#355;in o liter&#259;.<br>");
        }
        if( !preg_match("#[0-9]+#", $newpw) )
        {
            $error .= error("Parola trebuie s&#259; con&#355;in&#259; cel pu&#355;in o cifr&#259;.<br>");
        }
        if( !preg_match("#\W+#", $newpw) )
        {
            $error .= error("Parola trebuie s&#259; con&#355;in&#259; cel pu&#355;in un caracter special. (exemplu: <b>!@#$%^&*()[],./<>?</b>)<br>");
        }

        if($error)
        {
            echo $error;
        }
        else
        {
            mysql_query("Update account.account set password=PASSWORD('$newpw') where login='$cont'");
            mysql_query("Update account.account set passchange_token='1' where login='$cont'");
            $data = date("h:i:s d/m/Y");
            mysql_query("Insert into web.dev_player_log (account,data,actiune) values ('$cont','$data','Parola a fost schimbata in $newpw.')");
            echo succes("Parola a fost modificat&#259; cu succes.");
        }
    }
}

这里叫做这个函数:

<?php
    include ('inc/configurare.php');

    if(isset($_SESSION['user']) && isset($_SESSION['pass']))
    {
        if(isset($_GET['cod']) && $_GET['cod']!=NULL)
        {   
            $cod = replace($_GET['cod']);
            $log = $_SESSION['user'];
            $vrf = mysql_query("Select * from ".$account_db.".account where passchange_token='$cod' and login='$log'");

            if(mysql_num_rows($vrf)==0)
            {
                echo error("Link incorect sau expirat.");
            }
            else
            { 
            schimbare_pw_confirmata();
?> 
        <h4>Schimbare parol&#259;:</h4>
            Noua parol&#259; : 
            <form action="" method="POST">
                <input type="password" id="newPassword" name="newPassword" value="" maxlength="16" class="iRg_input"/>
                <input id="submitBtn" type="submit" name="SubmitLostPasswordCodeForm" value="Schimb&#259;" class="iR_stats"/>
            </form>
<?php   
            }   
        }
        else 
        {
?>
            <h4>SCHIMBARE PAROL&#258;:</h4>
<?php
            schimbare_pw();
?>
            Din motive de securitate trebuie să confirmi prin email inten&#539;ia de a schimba parola.<Br />
            Pentru a confirma apas&#259; pe linkul primit.<br>
            <div align="right">
                <form action="" name="passwordchangerequestForm" method="POST">
                    <input type="submit" name="passwordchangerequest"  class="buton" value="TRIMITE EMAIL CONFIRMARE"/>
                </form>
            </div>
<?php
        }
?>

<?php
    }
    else
    {
        echo "Zon&#259; restric&#539;ionat&#259;.";
    }
?>

当我输入123作为密码时输出:

  

Parola trebuiesăconţină8-16 caractere。 Parola trebuiesăconţină   celpuţinoliteră。演员:Parola trebuiesăconţinăcelpuţin   特别。 (示例:!@#$%^&*()[],。/ <>?)   成功。

2 个答案:

答案 0 :(得分:1)

已解决的语法已从if($error) {} else {}更改为if($error) {},并且else变成了if(!count($error))

答案 1 :(得分:-1)

<?php
    function schimbare_pw_confirmata()
{
    if(isset($_POST['SubmitLostPasswordCodeForm']))
    {
        $cont = $_SESSION['user'];
        $newpw = replace($_POST['newPassword']);

        if( (strlen($newpw) < 8) || (strlen($newpw) > 16) )
        {
           echo $error = error("Parola trebuie s&#259; con&#355;in&#259; 8-16 caractere.<br>");
        }
        else if( !preg_match("#[a-z]+#", $newpw) || !preg_match("#[A-Z]+#", $newpw) )
        {
           echo $error = error("Parola trebuie s&#259; con&#355;in&#259; cel pu&#355;in o liter&#259;.<br>");
        }
        else if( !preg_match("#[0-9]+#", $newpw) )
        {
          echo  $error = error("Parola trebuie s&#259; con&#355;in&#259; cel pu&#355;in o cifr&#259;.<br>");
        }
        else if( !preg_match("#\W+#", $newpw) )
        {
          echo  $error = error("Parola trebuie s&#259; con&#355;in&#259; cel pu&#355;in un caracter special. (exemplu: <b>!@#$%^&*()[],./<>?</b>)<br>");
        }
        else
        {
            mysql_query("Update account.account set password=PASSWORD('$newpw') where login='$cont'");
            mysql_query("Update account.account set passchange_token='1' where login='$cont'");
            $data = date("h:i:s d/m/Y");
            mysql_query("Insert into web.dev_player_log (account,data,actiune) values ('$cont','$data','Parola a fost schimbata in $newpw.')");
            echo succes("Parola a fost modificat&#259; cu succes.");
        }
    }
}
?>