EllisLab text mark
Advanced Search
     
How to make these codes DRY?
Posted: 21 November 2012 12:39 AM   [ Ignore ]
Avatar
Joined: 2011-08-08
524 posts
//save username and password to cookie 
 
public function save_cookie($user$pass)
 
{
  $cookie 
= array(
      
'name'   => 'user',
      
'value'  => $user,
      
'expire' => $this->cookie_life,
      
'domain' => $this->cookie_dname,
      
'path'   => '/',
      
'prefix' => '',
      
'secure' => FALSE
  
);

  
$cookie2 = array(
      
'name'   => 'pass',
      
'value'  => $pass,
      
'expire' => $this->cookie_life,
      
'domain' => $this->cookie_dname,
      
'path'   => '/',
      
'prefix' => '',
      
'secure' => FALSE
  
);
  
  
$this->input->set_cookie($cookie); //save info to cookie
  
$this->input->set_cookie($cookie2); //save info to cookie
 

Thanks in advanced.

 

 Signature 

Stick with it, practice it and have fun with it.

 
Posted: 21 November 2012 01:58 AM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Joined: 2009-04-15
453 posts

try serializing your username & password and saving as the value.

any reason you are using cookies rather than sessions?

 Signature 

Code By Jeff

Mahana Messaging Library

Problem with your query? Did you run

$this->db->last_query(); 

before you came to the forums for help?

 
Posted: 21 November 2012 02:20 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2009-06-19
6267 posts

You can also use json_encode and json_decode on the value.

 Signature 

Ceritfied State of CT Computer Programming Teacher.
Custom Designed Icons, eBook Covers Software Boxes. CD, DVD Etc. New iPhone® Tab Bar Icons and iPhone® Applications Icons.

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

Input -> Controller | Processing -> Model | Output -> View

 
Posted: 21 November 2012 02:47 AM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2011-08-08
524 posts

@jmadsen
It’s for remember_me login method.

@insiteFx
What’s the good reason of using json_encode?

Actually making it DRY it’s very easy, I’ll try to figure it out myself later when I have extra time.

 

 Signature 

Stick with it, practice it and have fun with it.

 
Posted: 21 November 2012 03:14 AM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2009-06-19
6267 posts

It’s just another way that you can combine the user and password together into a string for the cookie value.

Most users use the serialize and unserialize methods to do it.

I wrote my own remember me methods that only use a hash key and the database which are update by a timeout.

 Signature 

Ceritfied State of CT Computer Programming Teacher.
Custom Designed Icons, eBook Covers Software Boxes. CD, DVD Etc. New iPhone® Tab Bar Icons and iPhone® Applications Icons.

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

Input -> Controller | Processing -> Model | Output -> View

 
Posted: 21 November 2012 04:32 AM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Joined: 2011-04-28
521 posts

You’re passing 2 separate values to your function, but they are completely unique from each other. Why not combine into an array just pass one variable?

Perhaps you could do it like this:

//CODE UNTESTED, MAY BE BUGGY, BUT THIS IS THE JIST OF IT
//Just guessing that this stuff is happening in your controller, but put it wherever it really is
$user_pass = array ('user' => $user_whatever_it_is'pass' => $pass_whatever_it_is);
set_cookie($user_pass);
...

function 
set_cookie($input){
   
if(is_array($input)){
      
foreach ($input as $key => $value){
         $cookie 
= array(
            
'name'   => $key,
            
'value'  => $value,
            
'expire' => $this->cookie_life,
            
'domain' => $this->cookie_dname,
            
'path'   => '/',
            
'prefix' => '',
            
'secure' => FALSE
          
);
        
$this->input->set_cookie($cookie); //save info to cookie
      
}
   }

This technically, is keeping your code DRY, but…
Depending on what you want to do, this may not be the best set up. If you are setting up lots of cookies, then you may need create a more complex function (which could be isolated into it’s own helper file).
For example, what if one cookie needs a different path, or something else. Then you may want to pass a multi dimensional array instead of an associate array, etc.

 

 Signature 

My new website: www.downundr.com