2

in my Yii2 frame work project i want to include a php file. the file contain two function file name "encryptdecrypt.php" and save it in common\extension folder

<?
    public function encryptIt( $q ) {
        $cryptKey  = 'OrangeOnlineMedia';
        $qEncoded      = base64_encode( mcrypt_encrypt( MCRYPT_RIJNDAEL_256, md5( $cryptKey ), $q, MCRYPT_MODE_CBC, md5( md5( $cryptKey ) ) ) );
        return( $qEncoded );
    }

    public function decryptIt( $q ) {
        $cryptKey  = 'OrangeOnlineMedia';
        $qDecoded      = rtrim( mcrypt_decrypt( MCRYPT_RIJNDAEL_256, md5( $cryptKey ), base64_decode( $q ), MCRYPT_MODE_CBC, md5( md5( $cryptKey ) ) ), "\0");
        return( $qDecoded );
    }

    ?>

i include this line in controller page("CustomersController")

top of the page include using this line

$encFile =Yii::getAlias('@common'). '\extensions\encryptdecrypt.php';
require_once($encFile);

and use the function in an action code bellow

public function actionCreate()
{
    $model = new Customers();

    if ($model->load(Yii::$app->request->post()) ) {

        $model->password=encryptIt($model->password);            
        if($model->created_date==null)
        {
          $model->created_date=date('y-m-d') ; 
        }
        $model->save();
        return $this->redirect(['view', 'id' => $model->customer_id]);
    } else {
        return $this->render('create', [
            'model' => $model,
        ]);
    }
}

here i am getting the following error "Call to undefined function backend\controllers\encryptIt()"

thanks

1

4 Answers 4

6

Yii2 Uses PSR-4 AutoLoader Rule, so first save Security.php

In common\extensions folder, then open Security.php and create class in it.

<?php

namespace common\extensions;

class Security {

    public function encrypt(){
    // todo
    }

    public function decrypt(){
    // todo
    }

}

and then in your CustomersController action Create use it like this:

public function actionCreate()
{
    $model = new Customers();

    if ($model->load(Yii::$app->request->post()) ) {
        $security = new \common\extensions\Security(); // <-- Create Object Here
        $model->password= $security->encrypt($model->password);            
        if($model->created_date==null)
        {
          $model->created_date=date('y-m-d') ; 
        }
        $model->save();
        return $this->redirect(['view', 'id' => $model->customer_id]);
    } else {
        return $this->render('create', [
            'model' => $model,
        ]);
    }
}

BTW in Yii2 you can generate secure password hash like this too: Yii::$app->security->generatePasswordHash($password);

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

2 Comments

here I am using windows 10 and xampp.so i am getting path error ."Unknown Class – yii\base\UnknownClassException Unable to find 'backend\components\Crypt' in file: E:\xampp\htdocs\pope-Admin/backend/components/Crypt.php. Namespace missing?". i don't know how to solve it.. thanks once again
You should specify namespace of this Crypt class
0

May be you are using wrong folder. As you mentioned file is in common\extension folder

$encFile =Yii::getAlias('@common'). '\extension\encryptdecrypt.php';
require_once($encFile);

3 Comments

sorry typing mistake. but in coding i type correctly.
require_once(Yii::getAlias('@common').'/extensions/func.php'); must be used before class Defination and after namespace includation
see your php file.. not enclosed with<?php ?>
0

try getting

 $encFile = Yii::getAlias('@common/extensions/encryptdecrypt.php'); 

try also

var_dump($encFile) 

and check the pathname

1 Comment

not working... its getting error.. $encFile = Yii::getAlias('@common\extensions\encryptdecrypt.php');
-2

Make use of DOCUMENT_ROOT

The document root directory under which the current script is executing, as defined in the server's configuration file.

Example

To include files regardless of server type, do this:

$_SERVER['DOCUMENT_ROOT'] .Yii::getAlias('@common/sub_directory/yourFileName.php');
// outputs something like: /var/www/YiiApp/common/sub_directory/yourFileName.php

# or 
$_SERVER['DOCUMENT_ROOT'] .Yii::getAlias('@web/images/image.jpg');
// outputs something like: /var/www/YiiApp/web/images/image.jpg

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.