Redirect after authentification?

2007-07-13 Thread Vincent Fleuranceau

Hi all,

I'm new to CakePHP (and MVC in general). And so far I'm very happy
wiht it.

I've just followed the blog tutorial and I've combined it with the
simple authentification example from the manual. It works well so far,
but...

I would like to know if there is a (simple) way to redirect the user
to the requested page (instead of a default hard-coded URL). I hope
you see what I mean.

The original code reads:

?php
class AppController extends Controller
{
function checkSession()
{
// If the session info hasn't been set...
if (!$this-Session-check('User'))
{
// Force the user to login
$this-redirect('/users/login');
exit();
}
}
}
?

Any idea how I can store the URL of the requested page and pass it
to the redirect() method?

Thanks in advance,

-- Vincent


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



Re: Redirect after authentification?

2007-07-13 Thread kionae

I just store it in a session variable.  Any function that requires a
user to be logged in gets a line fo code like:

$this-Session-write('redirect', '/forums/index');
$this-checkSession();
$this-Session-delete('redirect');

This results in the user either getting redirected to the login page
via the checkSession() function if they're not logged in, or the
'redirect' session getting deleted if they're already logged in
(because if that's the case, we don't need to redirect them).  If the
user isn't logged in, they go to the login form, and enter their
username/password.

At that point (we've now moved to the login() function), if they
manage to log in correctly, I do something like this:

if(session_is_registered('redirect')) {
$redirect = $this-Session-read('redirect');
$this-Session-delete('redirect');
$this-redirect($redirect);
}

... which sends them back to whichever page redirected them to the
login page.  Probably not the prettiest method, but it works.




On Jul 13, 1:39 pm, Vincent Fleuranceau [EMAIL PROTECTED]
wrote:
 Hi all,

 I'm new to CakePHP (and MVC in general). And so far I'm very happy
 wiht it.

 I've just followed the blog tutorial and I've combined it with the
 simple authentification example from the manual. It works well so far,
 but...

 I would like to know if there is a (simple) way to redirect the user
 to the requested page (instead of a default hard-coded URL). I hope
 you see what I mean.

 The original code reads:

 ?php
 class AppController extends Controller
 {
 function checkSession()
 {
 // If the session info hasn't been set...
 if (!$this-Session-check('User'))
 {
 // Force the user to login
 $this-redirect('/users/login');
 exit();
 }
 }}

 ?

 Any idea how I can store the URL of the requested page and pass it
 to the redirect() method?

 Thanks in advance,

 -- Vincent


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



Re: Redirect after authentification?

2007-07-13 Thread Vincent Fleuranceau

On 13 juil, 23:51, kionae [EMAIL PROTECTED] wrote:

 I just store it in a session variable.  Any function that requires a
 user to be logged in gets a line fo code like:

 $this-Session-write('redirect', '/forums/index');
 $this-checkSession();
 $this-Session-delete('redirect');


Thank you Kionae for your answer, it helped me a lot figure out how to
modify the checkSession() function,
(so that I don't end up with duplicate code everywhere).

For those who are interested, here is the modified version:
(the code goes in app/app_controller.php, see the original tutorial
for details)

?php
class AppController extends Controller
{
function checkSession()
{
// If the session info hasn't been set...
if (!$this-Session-check('User'))
{
// Save the requested URL for the final redirection
$this-Session-write('redirect', $this-params['url']
['url']);

// Force the user to login
$this-redirect('/users/login');
exit();
}
}
}
?

(NOTA: it took me some time to realize that params['url'] is an ARRAY
and the actual URL is stored in params['url']['url'])


And in users_controller.php I've changed:

$this-redirect('/posts');

(NOTA : the original code mentions $this-redirect('/clients); if I
remember well but you have to replace 'clients' by 'posts'
if you want to use the auth tutorial combined with the blog tutorial)

into:

$newUrl = $this-Session-read('redirect');
$this-Session-delete('redirect');
$this-redirect($newUrl);

Of course, feel free to correct me if this is a bad way of doing it.

Cheers,

-- Vincent


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