PHP - 分配的变量不显示

时间:2014-04-30 14:05:00

标签: php

以下是我的登录表单,其中包含变量$login_errors。 当用户出现登录错误时,会为变量分配文本。

出于某种原因,如果在$form变量中调用,则不会显示任何文本。

但我可以使用<div><?php echo $login_errors; ?></div>单独调用     

$login_errors = "";
$out = "";
$form = "<div class='omb_login'>
            <h3 class='omb_authTitle'>Login or <a href='/register/'>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'>
                        " . $login_errors."
                        </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='login_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='/reset-pass/'>Forgot password?</a>
                    </p>
                </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 for login before outputting markup
if($input->post->user && $input->post->pass) { 


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


    //Check if the password provided in $pass equals tmp_pass
    if($u->id && $u->tmp_pass && $u->tmp_pass === $pass) {
    // user logging in with tmp_pass, so change it to be their real pass
    $u->of(false);
    $u->pass = $u->tmp_pass;
    $u->save();
    $u->of(true);

      $u = $session->login($user, $pass); 
      if($u) {
        // user is logged in, get rid of tmp_pass
        $u->of(false);
        $u->tmp_pass = '';
        $u->save();
        // now redirect to the profile edit page
        $session->redirect('/reset-pass-change/'); 
      }
   }


   //If user not logging if with TMP password carry on as normal.
    elseif($session->login($user, $pass)) {
        // login successful
        $session->redirect("/"); 

        }

    else {
        $login_errors .= "Username or Password is incorrect";
        $out .= $form;

    }

}

else {
    $out .= $form;
}


?>


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

<div class="container">


<?php echo $out; ?>

</div>



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

3 个答案:

答案 0 :(得分:1)

从PHP脚本的顶部开始。 $login_errors的价值是多少? 继续进入$form变量,此处$login_errors的值是多少?

在这两种情况下,它们都是空的,因此它“不显示”。

您在哪里使用<div><?php echo $login_errors; ?></div>?再次从脚本顶部开始并确定其值,您应该看到它为什么不为空。

答案 1 :(得分:0)

首先,您应该为 final 值分配一个变量,然后才将其放入字符串中。例如:

$var = '1';
$form = "The value is " . $var;
$var = '2';
echo $form;//Will output : The value is 1

答案 2 :(得分:0)

变量包含空字符串。当你将值设置为$ login_error时,php字符串已经创建,并且不像你想象的那样是可变的。

用占位符替换字符串中的变量,并在获得$ login_errors的最终值后替换它:

<?php
$html = <<<HTML
    <h1>{{login_errors}}</h1>
HTML;


// more code

$login_errors = 'You made an error!';

echo str_replace(array('{{login_errors}}'), array($login_errors), $html);