0

Below is my login form, which contains a variable $login_errors. The variable is assigned text when a user has a login error.

For some reason it is not showing up with any text if called within the $form variable.

I can however called separately using <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 Answers 3

1

Start from the top of your PHP script. What is the value of $login_errors ? Continue into your $form variable, what is the value of $login_errors here?

In both cases, they are empty, and so it "does not display".

Where did you use <div><?php echo $login_errors; ?></div>? Follow from the top of your script again and determine its value, and you should see why it is not empty.

Sign up to request clarification or add additional context in comments.

Comments

0

At first you should assign a variable with the final value and only after that put it into string. For example :

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

2 Comments

sorry im confused, could you elaborate.
@Ankh2054 : You are assigning $login_errors = "" to the $form and no matter what you do with $login_errors after it it will never change content inside $form. You should put $form = "<div class='omb_login'> ... after the last possible assignment of $login_errors.
0

The variable contains an empty string. Whenn you set the value to $login_error, the php string is already created and is not mutable in the way you think.

Replace the variable in your string with a placeholder and replace it once you have the final value for $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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.