简体   繁体   中英

How to generate a string in CodeIgniter?

I have to generate a string using the email id and current date.

This is for the purpose of providing an encrypted key to the user at the time of forgot password.

How can I generate this?

i can't say this is the right thing, but you can try this

<?php
$this->load->library('encrypt');
$email  = "address@domain.com";
$date   = date(Y-m-d);

$string = $email."-".$date;
$encrypted_string = $this->encrypt->encode($string);
?>

to decode the string, use this

<?php
$this->load->library('encrypt');
$encrypted_string = 'APANtByIGI1BpVXZTJgcsAG8GZl8pdwwa84';
$string = $this->encrypt->decode($encrypted_string);
?>

You can generate string in Codigniter by using "encryption_key" in config file. You can easily generate encrypted string by following these steps.

  1. Go to config.php file in config folder. Find $config['encryption_key'] and set your encryption key. " !@#oiqjwd2131asda3ir0000wqe+_) "
  2. Now go to your controller and call encryption class and generate your two variables $email, $date .
  3. Concatenate those variables and pass them through encrypt() function OR you can pass them one by one (according to your need) and echo them.
  4. Call the controller method and you will see your encrypted string.
  5. You can also decrypt your string by using decrypt() function

Config.php

$config['encryption_key'] =  "!@#oiqjwd2131asda3ir0000wqe+_)";

Tested code

    class trying extends CI_controller
    {
        public function index()
        {
            1. $this->load->library('encryption');
            2. $email  = "address@domain.com";
            3. $date   = date("Y/m/d");
            4. $string = $email.$date;
            5. echo $encrypted_string = $this->encryption->encrypt($string);
            6. echo "<br>".$decrypt = $this->encryption->decrypt($encrypted_string);
        }
    }

Now when you call the controller then by default index() method will call and then on line number 5 your your concatenated values will convert into encrypted form and on line number 6 your encrypted string will convert into original form.

Result

Result will be something like this

efc080b13a0fc4d1d220c5dcecd87479eca3a2872fa349f0bbf7827a6652ba870bf1b877f83d3f81e33aa8ae8634d97696864224cbfba14c0a3ca2edb50205c4axQjvBYx93dTrN0ZBiJLQcAu+WzFmdIyMYqwxwkRyIlYvCOIRO4Qgec9Nz8Ds1Kn (encrypted form)

address@domain.com2017/11/13 (decrypted form)

I hope all this will help you a lot.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM