05-10-2017, 12:24 PM
(This post was last modified: 05-13-2017, 03:08 AM by valkaycelestino.)
I need to create a referral url for every member of my site, something like this: http://mywebsite.com/ref/user, where user is a unique field in a database table. Please can someone give me a hint on how to go about this? I'll appreciate a dummy code, since I'm new to PHP and CodeIgniter.
I did some digging on the internet and found this code on github:
Code: <?php
//check for referal links
function referal()
{
$CI =& get_instance();
$cookie_value_set = $CI->input->cookie('_tm_ref', TRUE) ? $CI->input->cookie('_tm_ref', TRUE) : '';
if ($CI->input->get('ref', TRUE) AND $cookie_value_set == '') {
// referred user so set cookie to ref=username
$cookie = array(
'name' => 'ref',
'value' => $CI->input->get('ref', TRUE),
'expire' => '7776000',
);
$CI->input->set_cookie($cookie);
return TRUE;
}elseif ($cookie_value_set == '') {
$cookie = array(
'name' => 'ref',
'value' => 'sso',
'expire' => '15552000',
);
$CI->input->set_cookie($cookie);
return TRUE;
}elseif ($cookie_value_set != '') {
//already referred so ignore
return TRUE;
}else{
return TRUE;
}
}
//end of hooks file
?>
The owner of the gist only mentioned saving the file as referral.php inside the **hook** folder. This is not helping me with what I want to achieve, I don't know how to use the code:
1. How do I pass the **referrer** field to the variable **username** from the **users** table?
2. How do I load the hook file to view (register.php)?
3. How and where do I call the hook file?
So can anybody give me an insight?
-
raknjak
Junior Member
-
Posts: 11
Threads: 2
Joined: Oct 2015
Reputation:
0
05-10-2017, 12:58 PM
(This post was last modified: 05-10-2017, 01:02 PM by raknjak.)
ok editing because I misunderstood.
With referral urls you need to know the username beforehand no? I mean what is the use if not.
-
InsiteFX
Super Moderator
-
Posts: 6,646
Threads: 338
Joined: Oct 2014
Reputation:
243
05-11-2017, 03:44 AM
(This post was last modified: 05-11-2017, 03:56 AM by InsiteFX.
Edit Reason: Added referral code
)
You would need to use CodeIgniter's _remap() method and catch the referral code through your index($referral) method.
Here is how we did it, but we assigned a profile direct with user id directories this way we could store their
images etc in them.
PHP Code: /** * index() * ------------------------------------------------------------------------ * * index - default method called. * * @access public */ public function index($referrer = null) { //echo $referrer;
if ( ! empty($referrer)) { $user->query_user_by_url($referrer); $data['referral_user_id'] = $user->id; $this->session->set_userdata('referral_user_id', $user->id); } else { $this->session->set_userdata('referral_user_id', 0); $data['referral_user_id'] = 0; // Direct }
$this->load->view('your_view', $data); }
/** * _remap() * ------------------------------------------------------------------------ * * Remaps the URL segments. * * @param string * @param array */ public function _remap($method, $params = array()) { if (method_exists($this, $method)) { return call_user_func_array(array($this, $method), $params); }
show_404(); }
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
(05-10-2017, 07:03 PM)JayAdra Wrote: Or you could generate a "referral code" and assign that to a user. That way, the URL doesn't give away what user it's for, and it still can be tracked against that user.
Jay please help me with a dummy code. I did mention I'm new to PHP and Code Igniter.
(05-10-2017, 12:58 PM)raknjak Wrote: ok editing because I misunderstood.
With referral urls you need to know the username beforehand no? I mean what is the use if not.
Yes, the username will be assigned to users during registration. I'm looking for a way to create a unique url for each user using their username eg. http://mywebsite.com/ref/david08, or something similar.
(05-11-2017, 03:44 AM)InsiteFX Wrote: You would need to use CodeIgniter's _remap() method and catch the referral code through your index($referral) method.
Here is how we did it, but we assigned a profile direct with user id directories this way we could store their
images etc in them.
PHP Code: /** * index() * ------------------------------------------------------------------------ * * index - default method called. * * @access public */ public function index($referrer = null) { //echo $referrer;
if ( ! empty($referrer)) { $user->query_user_by_url($referrer); $data['referral_user_id'] = $user->id; $this->session->set_userdata('referral_user_id', $user->id); } else { $this->session->set_userdata('referral_user_id', 0); $data['referral_user_id'] = 0; // Direct }
$this->load->view('your_view', $data); }
/** * _remap() * ------------------------------------------------------------------------ * * Remaps the URL segments. * * @param string * @param array */ public function _remap($method, $params = array()) { if (method_exists($this, $method)) { return call_user_func_array(array($this, $method), $params); }
show_404(); }
Thanks InsiteFX, but I'm confused about the argument $referrer passed to the index function. Is it a database field name, and if yes, from which table? Or do i need a model to handle the data retrieval from my 'members' table? Please break the code down for me. Pardon my ignorance.
-
raknjak
Junior Member
-
Posts: 11
Threads: 2
Joined: Oct 2015
Reputation:
0
the username is unique in itself so we could just add some random characters on it:
PHP Code: $char = substr(str_shuffle(str_repeat("0123456789abcdefghijklmnopqrstuvwxyz", 5)), 0, 5); $username .= $char;
I am currently using the user id, it's simple and unique so why not.
-
InsiteFX
Super Moderator
-
Posts: 6,646
Threads: 338
Joined: Oct 2014
Reputation:
243
It's the users referral id when they register through your auth system you assign it to them.
You can assign a unique number to it if you want, but then you would need to store that also.
We used a referred_users table that links to the users table.
Code: -- --------------------------------------------------------
--
-- Table structure for table `referred_users`
--
DROP TABLE IF EXISTS `referred_users`;
CREATE TABLE IF NOT EXISTS `referred_users` (
`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`user_id` BIGINT(20) UNSIGNED DEFAULT NULL,
`referreduser_id` BIGINT(20) UNSIGNED DEFAULT NULL,
`added_on` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE = MyISAM DEFAULT CHARSET = `utf8` COLLATE = `utf8_unicode_ci`;
--
-- Dumping data for table `referred_users`
--
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
DROP TABLE IF EXISTS `users`;
CREATE TABLE IF NOT EXISTS `users` (
`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(55) NOT NULL,
`last_name` VARCHAR(55) DEFAULT NULL,
`gender` VARCHAR(8) DEFAULT NULL,
`activationkey` VARCHAR(255) DEFAULT NULL,
`password` VARCHAR(255) NOT NULL,
`email_address` VARCHAR(255) NOT NULL,
`group_id` BIGINT(20) UNSIGNED NOT NULL DEFAULT '100',
`role` VARCHAR(15) DEFAULT NULL,
`remember_me` INT(1) UNSIGNED NOT NULL DEFAULT '0',
`date_added` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`confirmed` TINYINT(4) UNSIGNED DEFAULT '0',
`banned` TINYINT(4) UNSIGNED DEFAULT '0',
`deleted` TINYINT(4) UNSIGNED DEFAULT '0',
`facebook_id` VARCHAR(255) DEFAULT NULL,
`token` VARCHAR(255) DEFAULT NULL,
`timeout` INT(11) UNSIGNED DEFAULT '1800',
`user_url` VARCHAR(255) NOT NULL,
`receive_emails` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0',
`read_terms` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0',
`email_referral` TINYINT(4) UNSIGNED NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE = InnoDB DEFAULT CHARSET = `utf8` COLLATE = `utf8_unicode_ci`;
--
-- Dumping data for table `users`
--
But you can add to it if need be.
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
|