Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8908179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:06:49+00:00 2026-06-15T03:06:49+00:00

I created Login functions in yii but i need to develop it to work

  • 0

I created Login functions in yii but i need to develop it to work with email or User ID in same time .
In below you will check the login by email code and it’s work in perfect mode , Any help to develop it ?

components/UserIdentity.php

<?php

/** Ahmad Samilo
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{

     // Need to store the user's ID:
     private $_id;
     public $password;
     public $status;
     public $session;

    /**
     * Authenticates a user.
     * The example implementation makes sure if the username and password
     * are both 'demo'.
     * In practical applications, this should be changed to authenticate
     * against some persistent user identity storage (e.g. database).
     * @return boolean whether authentication succeeds.
     */


    public function authenticate()
    {
        $user = Users::model()->findByAttributes(array('email'=>$this->username));


            /// set number of faild login
             $session=new CHttpSession;
             $session->open();

           Yii::app()->session['number']= Yii::app()->session['number']+1;
           $session=Yii::app()->session['number'];
              /// end 




        if ($user===null) { // No user found!
            $this->errorCode=self::ERROR_USERNAME_INVALID;
        } else if ($user->password !== SHA1($this->password) ) { // Invalid password!
            $this->errorCode=self::ERROR_PASSWORD_INVALID;







        } else { // Okay!



             $this->errorCode=self::ERROR_NONE;
            // Store the role in a session:
             $this->setState('status', $user->active);

             $this->setState('id', $user->user_id);
            $this->_id = $user->user_id;
        }
        return !$this->errorCode;
    }

    public function getId()
    {
     return $this->_id;
    }


}

view/login.php

<?php
/* @var $this SiteController */
/* @var $model LoginForm */
/* @var $form CActiveForm  */

$this->pageTitle=Yii::app()->name . ' - Login';
$this->breadcrumbs=array(
    'Login',
);
?>

<h1>Login</h1>

<p>Please fill out the following form with your login credentials:</p>

<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'login-form',
    'enableClientValidation'=>true,
    'clientOptions'=>array(
        'validateOnSubmit'=>false,
    ),
)); ?>

    <p class="note">Fields with <span class="required">*</span> are required.</p>

<?php



foreach(Yii::app()->user->getFlashes() as $key=>$message){

    echo "<div class='info'>";
     echo '<div class="flash-' . $key . '">' . $message . "</div>\n";

     echo "</div>";
}


?>


<?php


//// Get Session 
$session=new CHttpSession();
$session->open();
$count= Yii::app()->session['number'];


/// End Get Session 

//// Send Email to user notify him about that with help guide 
           function send_mail($to,$from,$subject,$msg){
        // message
        $message = '
        <html>
        <head>
          <title>Activation </title>
        </head>
        <body dir="rtl">
        <p align="right">'
        . $msg .
        '</p>
        <br>

        </p>
        </body>
        </html>
        ';

        // To send HTML mail, the Content-type header must be set
        $headers  = 'MIME-Version: 1.0' . "\n";
        $headers .= 'Content-type: text/html; charset=windows-1256' . "\n";

        // Additional heade rs
        $headers .= "From: ".$from . "\n";
        //$headers .= 'Bcc: info@illaftrain.co.uk' . "\n";

        // Mail it
        return mail($to, '=?windows-1256?B?'.base64_encode($subject).'?=', $message, $headers);
    }

//////

$email=$model->email;


$subject="تنبيه تكرار تسجيل الدخول الخاطئ ";

$url=Yii::app()->request->serverName;
$message=" 

text here 

";

 send_mail($email,'noreply@domain.net',$subject,$message);


//// End mail Function 





//// End Send mail 


?>
    <div class="row">
        <?php echo $form->labelEx($model,'Email'); ?>
        <?php echo $form->textField($model,'email'); ?>
        <?php echo $form->error($model,'email'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'password'); ?>
        <?php echo $form->passwordField($model,'password'); ?>
        <?php echo $form->error($model,'password'); ?>



    </div>


    <div class="row buttons">
        <?php echo CHtml::submitButton('Login'); ?>
    </div>


    <?php if($count>3) :?>
    <?php if(CCaptcha::checkRequirements()): ?>
        <?php echo $form->labelEx($model,'verifyCode'); ?>
        <div>
        <?php $this->widget('CCaptcha'); ?>
        <?php echo $form->textField($model,'verifyCode'); ?>
        </div>
    <div class="row">
        <div class="hint">Please enter the letters as they are shown in the image above.
        <br/>Letters are not case-sensitive.</div>
        <?php echo $form->error($model,'verifyCode'); ?>
    </div>
    <?php endif; ?>
    <?php endif; ?>









<?php $this->endWidget(); ?>

Models/LoginForm.php

<?php

/**
 * LoginForm class.
 * LoginForm is the data structure for keeping
 * user login form data. It is used by the 'login' action of 'SiteController'.
 */
class LoginForm extends CFormModel
{
    public $email;
    public $password;
    public $login;
    public $verifyCode;


    /**
     * Declares the validation rules.
     * The rules state that username and password are required,
     * and password needs to be authenticated.
     */
public function rules()
{
    return array(
        array('email, password', 'required'),
        array('email', 'email'),
        array('password', 'authenticate'),
            // verifyCode needs to be entered correctly
            array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),

);

}

    /**
     * Declares attribute labels.
     */
    public function attributeLabels()
    {
        return array(
            'email'=>'Email Address',
            'user_id'=>'User ID',
        );
    }

    /**
     * Authenticates the password.
     * This is the 'authenticate' validator as declared in rules().
     */
    public function authenticate($attribute,$params)
    {
        if(!$this->hasErrors())  // we only want to authenticate when no input errors
        {
            $identity=new UserIdentity($this->email,$this->password);
            $identity->authenticate();
            switch($identity->errorCode)
            {
                case UserIdentity::ERROR_NONE:
                    Yii::app()->user->login($identity);

                    break;
                case UserIdentity::ERROR_USERNAME_INVALID:
                    $this->addError('email','Email address is incorrect.');
                    break;
                default: // UserIdentity::ERROR_PASSWORD_INVALID
                    $this->addError('password','Password is incorrect.');
                    break;
            }
        }
    }


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T03:06:52+00:00Added an answer on June 15, 2026 at 3:06 am

    In your authenticate function in UserIdentity

    instead of:

    $user = Users::model()->findByAttributes(array('email'=>$this->username));
    

    Do something like

    //If we have a @ in the username, then it should be an email
    if(strpos($this->username, '@') !== false){
       $user = Users::model()->findByAttributes(array('email'=>$this->username));
    }else{
       //Otherwise we search using the username
       $user = Users::model()->findByAttributes(array('username'=>$this->username));
    }
    

    Note: providen that you do not allow @ in the username

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to switch databases upon user login. I've created this login signal.. but
I have created login page forced login in several pages. Now i need to
i have created one login and register view in that view after login user
I am using Yii framework and I want to create a user login system
I created a login view for my Employee controller , but when the login
I've got a web interface from which I will gather user data (username,pass,email,etc..) and
I created login from that when clicking submit button sends variables to login_success.php page.but
I try to update de last_login_time in my user table, but the time() is
I have created login account on my localhost\sql2008 Server (Eg. User123) Mapped to Database
I have created Login forms and registration forms for a website. The form is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.