Welcome Guest, Not a member yet? Register   Sign In
Where do I put global code?
#1

[eluser]koryrok[/eluser]
Hi All,

I would like to know where to place global code, for example, I want to run this on every page request:

Code:
function cookie_user()
{
     if ( get_cookie('thecookie')){
     }
     else{
        
        $uuid = md5(uniqid(rand(), true));
        
        $cookie = array(
                           'name'   => 'thecookie',
                           'value'  => $uuid,
                           'expire' => '86500',
                           'domain' => '.domain.com',
                           'path'   => '/'
                       );
        
        set_cookie($cookie);
     }
}

Thanks!
#2

[eluser]TheFuzzy0ne[/eluser]
Welcome to the CodeIgniter forums!

You could create a helper, and have that helper auto-loaded.
#3

[eluser]koryrok[/eluser]
Hi Fuzzy, thanks!

Heres what I did..

update autoload.php to:
Code:
$autoload['helper'] = array('cookie', 'cookie_user_helper');

create the following helper and put it in helper directory:
Code:
<?php

function cookie_user()
{
     if ( get_cookie('thecookie')){
     }
     else{
        
        $uuid = md5(uniqid(rand(), true));
        
        $cookie = array(
                           'name'   => 'thecookie',
                           'value'  => $uuid,
                           'expire' => '86500',
                           'domain' => '.domain.com',
                           'path'   => '/'
                       );
        
        set_cookie($cookie);
     }
}

cookie_user();

?>

I'm not seeing the cookie though, thanks!
#4

[eluser]jedd[/eluser]
Quote:
Code:
$autoload['helper'] = array('cookie', 'cookie_user_helper');

You don't need the _helper on that configuration item - it's assumed. You do, of course, need it on the filename proper.


Instead of :
Code:
function cookie_user()
{
     if ( get_cookie('thecookie')){
     }
     else{

... why not just ..

Code:
function cookie_user()
{
     if ( ! get_cookie('thecookie'))  {

When you say 'I'm not seeing the cookie' - what does that mean?
#5

[eluser]Vicente Russo[/eluser]
Maybe there is another option, an elegant solution, or not?




Theme © iAndrew 2016 - Forum software by © MyBB