CodeIgniter Forums

Full Version: Generate a random URL to a specific page
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

Hi folks,

Thank you very much for all your help about my other topics.

I am sorry but I have an another trouble.
I would like to create a project where people that are
enregistred on the website can invite their friend by an email that
CodeIgniter will send.

But the fact is I would like that the URL be « dynamic », a « random »
URL. For exemple, the real URL is
Mister X send an invitation to mister Y but in the mail the URL to the
registration will be something like that or whatever.

And I would like that few hour after if someone try to go to,
the page doesn't exist.

I really don't know how to do it.
Is it possible ? Is it easy to do ?

Thank you very much for your help.

El Forum

check the email recovery process and try to emulate it.
basicly at the invite point
generate a random string, that you store in an invite table,
invite table : id, code, inviting_user_id, invited_person_email, issued_at or expire on....

send your link as

lskdjflkjsdlfkjsdlkfj ===> been your random string

when mister Y click on the link in his email

you will have a controller pages that will parse the url, (see uri lib) get the code checked it against your table, see if its there, if its active not expired.....

then its up to you if its a good code to do what you need to do in the controller pages ( eg:pages you can call it what you want).

like show same page with specific content to the invitee, or specific content.......

hope it help.

El Forum

It's an amazing idea !!
I'll try when I'll have the time.

I keep in touch.

Thank you so much.

El Forum

Pretty much what weboap said.

Create a column in your database that stores that random generated url, use a controller method to check if that random key exists, if it does do whatever registering processing you need to do, then regenerate the random key and update the field in the database.

Now the key that was emailed is different from that that is in your database.

Here's some code to play around with:

function activate($hash='') {
        if (!empty($hash)) :
// check if this hash exists
            $active = $this->user->checkactivate($hash, 'confirmhash');
// if hash does exist, log user in
            if ($active['true']) :
                foreach ($active['result'] as $qr) :

                    $session_data = array(
                        'user_id' => $qr->id,
                        'username' => $qr->username,
                        'first_name' => $qr->first_name,
                        'last_name' => $qr->last_name,
                        'user_level' => $qr->user_level,
                        'is_logged_in' => TRUE

                $this->session->set_flashdata('message', 'Congratulations ' . $this->session->userdata('username') . ', you are now logged in!');
// update hash column with new hash
                $this->user->update('user', $this->session->userdata('user_id'), array('confirmhash' => str_shuffle($hash)));
            else :
                echo 'This activation key is invalid';



function checkactivate($hash, $col) {
// $col = column name, $hash = random key
        $query = $this->db->get_where('user', array($col => $hash));
//if this hash exists
        if ($query->num_rows > 0) :

            $this->db->where($col, $hash);
            $this->db->update('user', array('confirmed' => 1));
            $data['true'] = TRUE;
            $data['result'] = $query->result();
        else :
            $data['true'] = FALSE;
        return $data;

    function update($table, $id, $array) {
        $this->db->where('id', $id);
        $q = $this->db->update($table, $array);

Could be written better, but hope that gives an idea..

El Forum


Thank you very much for you help.
One thing that I don't really understand.
How can I redirect the « hash URL » to the
good URL ?

I mean, how do you say (in the code) that will be redirect
to ?

Thank you a lot.

El Forum

Ideally you'd have one register method in your controller, it would also have an arguement/parameter.

The parameter would be for the hash.

El Forum


Ok I believe to understand.
Sorry about that but normaly when
you click on a link that load a controller.
<?php anchor('user/registration','Sign in now'); ?>

But what I don't understand is in the email that the guest
will receive. How can I create a link to load the
controller with a hash code ?

El Forum

anchor('user/registration/'.$query->hash, Click here);

Where $query->hash is the hash result from your database

El Forum

Thank you for your help.

I'll try when I'll have the time.
Thank you very much.

I can't try before sunday, sorry about that.

El Forum


Thank you very much, it's works fine !
I just have one trouble.

The url is like this : /user/signup_unite/00u8b0pir
But the problem is when user make a mistake when they sign in.
Because it reloads the page and redirect to /user/signup_unite/,
but this page doesn't exist.

I would like that when they make a mistake that redirect to
/user/signup_unite/00u8b0pir with the error. I've already create
the form with validation, I just want to now how to redirect to

Thank you very much.
Pages: 1 2