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
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);
try getting
$encFile = Yii::getAlias('@common/extensions/encryptdecrypt.php');
try also
var_dump($encFile)
and check the pathname
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);
Make use of DOCUMENT_ROOT
The document root directory under which the current script is executing, as defined in the server's configuration file.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.