Automatically login after registration form

peptobismol 18 Feb, 2009
Hi,
I created a custom registration form with chronoform... Now I need to have it automatically login registration.

I'm thinking I need to put this code in the after on submit code field.. I'm on J! 1.5.8.

<?
global $mainframe;

$usersipass['username'] = JRequest::getVar('username');
      $usersipass['password'] = JRequest::getVar('pass');
      $mainframe->login($usersipass);
      //redirects to another page.... not sure what J! function does this.
      header (Location: "/index.php");
?>


But it doesn't work.. Is it possible?
Thanks.
peptobismol 19 Feb, 2009
i found the error... this works

<?
global $mainframe;

$usersipass['username'] = JRequest::getVar('username');
      $usersipass['password'] = JRequest::getVar('pass');
      $mainframe->login($usersipass);
      //redirects to another page.... not sure what J! function does this.
      header ('Location: /index.php');
?>
Max_admin 19 Feb, 2009
Glad you made it, thanks for posting the working code!

Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
GreyHead 19 Feb, 2009
Hi peptobismol,

If you want to stay with the Joomla code the missing piece is $mainframe->redirect($link, $message, $type); where $message and type are optional. $message will display a system message aftet the redirect, $type sets the format of the message - from memory the options are ('info', 'alert', and 'error').

Bob
peptobismol 26 Feb, 2009
Hi,
there's a problem with my thinking with this code..
If a new user registers with an existing user name, it should throw an error and the code redirects without registering or throwing an error.

The Joomla registration plugin doesn't throw errors?

Any insights would be appreciated.
peptobismol 26 Feb, 2009
this is what I have.. I guess it's working but not sure if it is kosher. It would be nice to print out an error message but not sure how in joomla.. Best I can do is redirect to a page.

onSubmit before email

<?
global $mainframe;

//check for existing username
$usersipass['username'] = JRequest::getVar('username');
$usersipass['password'] = JRequest::getVar('pass');
$email = JRequest::getVar('email');

$db =& JFactory::getDBO();
$query = 'SELECT `id`'
    . ' FROM #__users'
    . ' WHERE username=' . $db->quote( $usersipass['username'] );
$db->setQuery( $query );
$result = $db->loadResult();
 
if ($result) {
    //user already exists
    $link = '/index.php?option=com_content&view=article&id=86';
    $mainframe->redirect($link,'User already exists', 'error');
} 

//check for existing email
$query = 'SELECT `id`'
    . ' FROM #__users'
    . ' WHERE email=' . $db->quote( $email );
$db->setQuery( $query );
$result = $db->loadResult();
 
if ($result) {
    //user already exists
    $link = '/index.php?option=com_content&view=article&id=86';
    $mainframe->redirect($link,'Email already exists', 'error');
} 
   
?>


onSubmit after email
<?
global $mainframe;

$usersipass['username'] = JRequest::getVar('username');
$usersipass['password'] = JRequest::getVar('pass');
$mainframe->login($usersipass);
$link = '/index.php?option=com_content&view=article&id=72:awards-poll&catid=9:voting&Itemid=52';
$mainframe->redirect($link);
   
?>
GreyHead 26 Feb, 2009
HI peptobismol,

That first check is a validation - it could go into the ServerSide validation box.

Bob
peptobismol 26 Feb, 2009
oh yeah,
I forgot about that box. Thanks,
This topic is locked and no more replies can be posted.