CodeIgniter Forums
Assign promocode, but only if cookies are accepted - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Assign promocode, but only if cookies are accepted (/thread-26638.html)



Assign promocode, but only if cookies are accepted - El Forum - 01-19-2010

[eluser]mvdg27[/eluser]
Hi guys,

I'm struggling with the following situation:

I want to assign promocodes to visitors of my website. I have a list of 5000 valid codes and each code can be used only once.

Each visitor should be getting only one code, and this should be saved in a cookie. My initial idea is to get a valid, unused code from the database on a users first page request and then save the code in a cookie. Upon assignment the code is marked as assigned. On each following page request (not necessarily in the same session), the script checks for the existence of a cookie, and if it exists doesn't assign a new code.

Seems all good, except in the case a user doesn't accept cookies, and thus get's assigned a new promocode upon each request and using up all the codes.

Basically it's fine to notify the user that cookies are required to receive a promocode.

Any ideas on how to solve this issue?

Thanks, Michiel

PS. I'm fully aware that the cookie solution in itself is not the best option anyway, but I'm not dealing with a high risk operation here. Basically looking for a quick and dirty solution for a temporary website.


Assign promocode, but only if cookies are accepted - El Forum - 01-19-2010

[eluser]Ian Jones[/eluser]
Why don't you try writing a cookie, with a user set variable 'cookiesEnabled=True'. Read that back in to check if the clients browser will set cookies in the first place. Quick and dirty.


Assign promocode, but only if cookies are accepted - El Forum - 01-19-2010

[eluser]mvdg27[/eluser]
Yeah, I was thinking something similar, but that approach would mean that I can't give a visitor a promocode, until his 3rd page request, right?


Assign promocode, but only if cookies are accepted - El Forum - 01-19-2010

[eluser]Ian Jones[/eluser]
Code:
if($this->session->userdata('cookies_enabled'))
{
    if($this->session->userdata('promo_code') == '123456')
    {
        // Found a matching promo, user is valid
    }
    else
    {
        // No promo assigned, so assign one
        $userData = array('promo_code' => '123456');
        $this->session->set_userdata($userData);
        redirect('home', 'refresh');
    }
}
else
{
    // No cookie, set a cookie
    $userData = array('cookies_enabled' => True);
    $this->session->set_userdata($userData);
    redirect('home', 'refresh');
}


Sure it means redirects, but at least the user doesn't need to click. Remember you could actually send the cookie with an HTTP header rather than a full page refresh.

You could also make a system which just uses the clients IP and assigns that IP a unique promo code. Then you just check the database to match the IP to the promo code