PHP - if ... else ...语句

时间:2014-04-25 08:18:33

标签: php

我正在使用一种名为process wire的CMS,它严重依赖于PHP语法。我现在才真正学习PHP,所以这可能是一件容易的事情,但我对else语句有疑问。

我正在努力的部分如下。目前我收到一个服务器错误,说明意外{第104行。

if($user->user_activation != 0) {
    $login_errors .= "Sorry, but you need to activate your account!";
    $out .= $form;
    }

    //Below is line 104
    else($session->login($user, $pass)) {
        // login successful
        $session->redirect("/"); 
        }

用户有一个名为user_activation的字段,其中包含一个字段,一旦用户点击激活电子邮件,该字段就会变为0。所以基本上只检查是否为0然后显示错误。 ELSE做下一个声明并登录。

<?php

$login_errors = "";
$out = "";
$form = "<div class='container'>


        <div class='omb_login'>
            <h3 class='omb_authTitle'>Login or <a href='#'>Sign up</a></h3>
            <div class='row omb_row-sm-offset-3 omb_socialButtons'>
                <div class='col-xs-4 col-sm-2'>
                    <a href='#' class='btn btn-lg btn-block omb_btn-facebook'>
                        <i class='fa fa-facebook visible-xs'></i>
                        <span class='hidden-xs'>Facebook</span>
                    </a>
                </div>
                <div class='col-xs-4 col-sm-2'>
                    <a href='#' class='btn btn-lg btn-block omb_btn-twitter'>
                        <i class='fa fa-twitter visible-xs'></i>
                        <span class='hidden-xs'>Twitter</span>
                    </a>
                </div>  
                <div class='col-xs-4 col-sm-2'>
                    <a href='#' class='btn btn-lg btn-block omb_btn-google'>
                        <i class='fa fa-google-plus visible-xs'></i>
                        <span class='hidden-xs'>Google+</span>
                    </a>
                </div>  
            </div>

            <div class='row omb_row-sm-offset-3 omb_loginOr'>
                <div class='col-xs-12 col-sm-6'>
                    <hr class='omb_hrOr'>
                    <span class='omb_spanOr'>or</span>
                </div>
            </div>

            <div class='row omb_row-sm-offset-3'>
                <div class='col-xs-12 col-sm-6'>    
                    <form class='omb_loginForm' action='./' accept-charset='UTF-8' autocomplete='off' method='POST'>
                        <div class='alert alert-error fade-in alert-dismissable'>
                        </div> 
                        <div class='input-group'>
                            <span class='input-group-addon'><i class='fa fa-user'></i></span>
                            <input type='text' class='form-control' name='user' placeholder='Username'>
                        </div>
                        <span class='help-block'></span>

                        <div class='input-group'>
                            <span class='input-group-addon'><i class='fa fa-lock'></i></span>
                            <input  type='password' class='form-control' name='pass' placeholder='Password'>
                        </div>
                        <span class='help-block'></span>
                        <input class='returnUrl' type='hidden' name='returnUrl' value='[[+request_uri]]' />



                    <input class='loginLoginValue' type='hidden' name='service' value='login' />

                        <button class='btn btn-lg btn-primary btn-block' type='submit' name='submit' value='Login'>Login</button>
                    </form>
                </div>
            </div>
            <div class='row omb_row-sm-offset-3'>
                <div class='col-xs-12 col-sm-3'>
                    <label class='checkbox'>
                        <input type='checkbox' name='rememberme' value='1' checked='checked'>Remember Me
                    </label>
                </div>
                <div class='col-xs-12 col-sm-3'>
                    <p class='omb_forgotPwd'>
                        <a href='[[~5]]'>Forgot password?</a>
                    </p>
                </div>
            </div>          
        </div>"
;

// Check if user is already logged in - redirect to 
if($user->isLoggedin()) {
    // user is already logged in, so they don't need to be here
    $session->redirect("/"); 
}


//Check if submit is issued
if($input->post->submit) {

//check for login before outputting markup
if($input->post->user && $input->post->pass) {

    $user = $sanitizer->username($input->post->user);
    $pass = $input->post->pass; 

    if($user->user_activation != 0) {
    $login_errors .= "Sorry, but you need to activate your account!";
    $out .= $form;
    }


    else($session->login($user, $pass)) {
        // login successful
        $session->redirect("/"); 
        }

    }


}
else {
    $out .= $form;
}


?>


<?php include("./head.inc"); ?>
<?php include("./navbar.inc"); ?>

<div><?php echo $login_errors; ?></div>

<?php echo $out; ?>



<?php include ("./foot.inc"); ?>
<?php include ("./java.inc"); ?>

1 个答案:

答案 0 :(得分:0)

else($session->login($user, $pass))

无效,将被解释为:

else{
    ($session->login($user, $pass))
}

这没有多大意义......

else块是有条件的时,它被称为elseif

if($user->user_activation != 0) {
    $login_errors .= "Sorry, but you need to activate your account!";
    $out .= $form;
}
elseif($session->login($user, $pass)) {
    // login successful
    $session->redirect("/"); 
}
else
{
    // your final default case down here
}