Quantcast

Retrieve URL - New thread.

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Retrieve URL - New thread.

Bernard Grosperrin

I would like to improve my rather crude login system. For now, after
login, I redirect the user to '/', or the "Home" page.
I would like to "trap" or retrieve the URL the user intended to go to,
pass it as parameter to my login function, to redirect to this URL
instead of Home.
Is there a way to do this, or should I write a "checkSession" with
parameter, which would be the URL from where it is called? (which I tried and does not seems able to make it work.)

Thanks,
Bernard


--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Retrieve URL - New thread.

Mariano Iglesias

Quite simple. Take advantage of the HTTP_REFERER index available at
$_SERVER.

You should take this into consideration and add it to your login form
when the login was accessed without submitting any data. So on your
controller where you handle the login (I assume you use a login()
action) you could do something like:

function login()
{
        if (!empty($this->data))
        {
                $user = $this->User->findByLogin($this->data['User']['login']);

                if($user !== false && $user['User']['password'] ==
sha1($this->data['User']['password']))
                {
                        $this->Session->write('User', $user);

                        if (isset($this->data['User']['url']))
                        {
                                $this->redirect($this->data['User']['url']);
                        }
                        else
                        {
                                $this->redirect('/');
                        }
                }
                else
                {
                        $this->set('errorMessage', 'The account you\'ve specified is not
valid.');
                }
        }
        else if (isset($_SERVER['HTTP_REFERER']))
        {
                $this->set('url', $_SERVER['HTTP_REFERER']);
        }
}

and on your login.thtml view file you would have something like:

<?php if(!empty($errorMessage)): ?>
<p class="error"><?php echo $errorMessage; ?></p>
<?php endif; ?>
<?php echo $html->formTag('/account/login'); ?>
        <?php
        if (isset($url))
        {
                $html->hiddenTag('User/url', $url);
        }
        ?>
        User: <?php echo $html->input('User/login'); ?><br />
        Password: <?php echo $html->password('User/password'); ?><br />
        <br />
        <?php echo $html->submit('Login'); ?>
</form>

Hope it helps.

-MI


--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Retrieve URL - New thread.

Mariano Iglesias

One thing I forgot on the above code. If there is an error on the login
details the URL gets lost. So on the login() action right before you
set the errorMessage add:

if (isset($this->data['User']['url']))
{
        $this->set('url', $this->data['User']['url']);
}


--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Retrieve URL - New thread.

Matt Adams
In reply to this post by Bernard Grosperrin

Bernard Grosperrin wrote:

> I would like to improve my rather crude login system. For now, after
> login, I redirect the user to '/', or the "Home" page.
> I would like to "trap" or retrieve the URL the user intended to go to,
> pass it as parameter to my login function, to redirect to this URL
> instead of Home.

Maypole (a Perl MVC project) and Catalyst (and other projects) have a
nice way of stopping a user for authentication (so if I requested /home
but needed to be authenticated first then /home would bring up an login
page instead).  After logging in the user would be sent to /home.

I would like to see this sort of flow available in Cake as well.  I just
haven't gotten around to it :-)


Matt
--
BASIC: A programming language.  Related to certain social diseases
in that those who have it will not admit it in polite company.

--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Retrieve URL - New thread.

Nate Abele

class AppController extends Controller {

        function beforeFilter () {
                if (!$this->Session->check('User') && $this->params['action'] !=
'login') {

                        if (!$this->RequestHandler->isAjax()) {
                                // If this is not an Ajax Request, just redirect the user to the
login
                                $this->redirect('/accounts/login/'.$this->params['url']['url']);
                                exit();
                        } else {
                                // Otherwise, throw up a login error with a link back to the login
                                $this->set('url', $this->referer());
                                $this->viewPath = 'elements';
                                $this->render('login_error', 'ajax');
                                exit();
                        }
                }
        }
}

class AccountsController extends AppController {

        function login () {

                if (!empty($this->data)) {
                        if ($login = $this->Contact->login($this->data)) {
                                $params = func_get_args();
                                $url = empty($params) || in_array('logout', $params) ? '/' : '/' .
implode('/', $params);

                                $this->Session->write('Contact', $login['Contact']);
                                $this->redirect($url);
                                exit();
                        }
                }
        }
}


--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Retrieve URL - New thread.

Nate Abele

The above code was lifted from an Actual Application (tm), but some of
the variable names were changed for clarity, which is why some of the
session setting doesn't match up with the session checking, but you get
the idea.  There's also some extra goodies in there on how to handle
login expirations for both regular and Ajax requests.


--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Loading...