Show error login in my login page custom

Show error login in my login page custom

by Handerson Medeiros -
Number of replies: 11

Hi everybody,

I'm not wanting to use the default login page moodle, so I made a custom. The code is:


            <form class="loginform" name="login" method="post" action="/moodle/login/index.php">
               <p>Username :
                 <input size="10" name="username" />
               </p>
               <p>Password :
                 <input size="10" name="password" type="password" />
               </p>
               <p>
                 <input name="Submit" value="Login" type="submit" />
               </p>
            </form>  


My question is how i make for get e show a error the login os password for the user?

Average of ratings: -
In reply to Handerson Medeiros

Re: Show error login in my login page custom

by Franco Pantoja -
Picture of Particularly helpful Moodlers

Hi


Moodle gives us this variable by GET in the url, 

?errorcode=n


        $errormsg = get_string("cookiesnotenabled");
        $errorcode = 1;
        $errormsg = get_string('username').': '.get_string("invalidusername");
        $errorcode = 2;
        $errormsg = get_string("invalidlogin");
        $errorcode = 3;
    $errormsg = get_string('sessionerroruser', 'error');
    $errorcode = 4;

Do not forget to activate the $CFG->alternateloginurl


Regards!!

Average of ratings: Useful (1)
In reply to Franco Pantoja

Re: Show error login in my login page custom

by Davi DePaula -

Thank you Paco, 


I used the following code to display a wrong username password message on my custom login page. 


<?php 

if ($_GET["errorcode"] == 3){

   echo '<div class="error_msg">The Username and/or Password entered is invalid, please try again.</div></br>';

}

?>

Average of ratings: Useful (1)
In reply to Davi DePaula

Re: Show error login in my login page custom

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

In Moodle code, you should never access $_GET or $_POST directly. Always use optional_param or required_param instead, with an appropriate PARAM_... type. It does not really matter here, but you should get into good habits. That way you won't start accidentally write in-secure code in future. So, this should be:

if (optional_param('errorcode', 0, PARAM_INT) == 3) {
   echo '<div class="error_msg">The Username and/or Password entered is invalid, please try again.</div></br>';
}

Average of ratings: Useful (2)
In reply to Tim Hunt

Re: Show error login in my login page custom

by Davi DePaula -

That is a good point, I was going that route, but I couldn't figure out the code to do it that way.  Thank you for the code Tim. 

In reply to Davi DePaula

Re: Show error login in my login page custom

by Robin Leung -

I tried to implement the code from Tim into my custom login page. But it didn't do anything. Am I missing something?

Thanks.

In reply to Tim Hunt

Re: Show error login in my login page custom

by Brandon Jimenez -

Hello, I tried both of your solutions on my login.php and index php (I just recently came to administer this Moodle site), and none of them work.

Ex 1:

if (optional_param('errorcode', 0, PARAM_INT) == 3) {
   echo '<div class="error_msg">The Username and/or Password entered is invalid, please try again.</div></br>';
}

Ex 2:

if ($_GET["errorcode"] == 3){
   echo '<div class="error_msg">The Username and/or Password entered is invalid, please try again.</div></br>';
}

Ex 3:

if ($errorcode == 3) {
   echo '<div class="error_msg">The Username and/or Password entered is invalid, please try again.</div></br>';
}

However, none of them works

I think I'm doing something wrong because I'm not using any customized auth php script, and it should reference the default login process. Even more, I found there and in the moodle.php the appropriate reference to the message it should display in the event of an error. So, what is that I'm missing?

In reply to Davi DePaula

Re: Show error login in my login page custom

by Robin Leung -

I used this code on my custom login page. But nothing happened.

Am I missing something? Thanks.

In reply to Robin Leung

Re: Show error login in my login page custom

by Sean Marx -
Picture of Testers

Hi Robin

I had the same problem today on my Moodle 2.7 localhost dev site. A student could log in, but as admin I could not log in. I have a custom login page as well.

My fix was to go to mdl_user and reset the admin password. In my case I simply changed it to password (5f4dcc3b5aa765d61d8327deb882cf99) and it worked.

In reply to Franco Pantoja

Re: Show error login in my login page custom

by evan skull -

May I know how to activate the $CFG.

In reply to evan skull

Re: Show error login in my login page custom

by Yousuf Tafhim -

Include Moodle's config.php file in the login page file. That will activate $CFG