Forums

How to allow a form to be seen only after Login in Joomla

temp2010 07 May, 2011
Hello all,

I created a form in chronoforms, now i want it to be displayed only after the user login. Could anyone help me to code this?🙂 Any help will be appreciated!

Joe
GreyHead 07 May, 2011
Hi joe,

Add this code to beginning of the Form HTML in CFv3 or into a Custom Code action in CFv4
<?php

$user =& JFactory::getUser();
if ( !$user->gid ) {
  $mainframe =& JFactory::getApplication();
  $mainframe->redirect('index.php', 'You are not allowed to access this page', 'error');
}
?>
Replace 'index.php' with some other url to redirect to the user there.
Replace 'error' with 'info' or 'alert' to change the message format.
Leave the message string out - just use $mainframe->redirect('index.php'); to redirect silently

Bob
temp2010 09 May, 2011
Hi Greyhead,

Thanks for your reply, I created the custom code section as you suggested. However, the form doesn't display at the frontend and shows "HTTP 500 internal server error". Could you help me out again?

Many Thanks
Joe
GreyHead 09 May, 2011
Ho Joe,

My apologies. Please replace
$mainframe =& JFactory::getFramework();
with
$mainframe =& JFactory::getApplication();
I must remember which one is correct :-(

Bob

PS I updated my earlier post
temp2010 09 May, 2011
Thanks a lot GREYHEAD! You are a hero! It works really nice, I am happy to see your reply in the forum, because it is like a solution bank!😀
Joe
temp2010 09 May, 2011
Hello Greyhead,

Regarding to the above code you posted, i forgot to mention that i replaced the code below

if ( !$user->gid ) {
...
...
...
}

with

if ( !$user->username) {
...
...
...
}

It then works fine, otherwise i keep on getting redirected back to index.php even if i logged in. I am using Joomla 1.6, is this error occured because of the version inconsistency? Just a guess. Anyways, thank you a lot for all your time and help!:D
Joe
GreyHead 09 May, 2011
Hi Joe,

Almost certainly that's a Joomla! 1.6 change. The User code has changed a lot and so far I have no idea how the new version works :-(

Bob
This topic is locked and no more replies can be posted.

VPS & Email Hosting 20% discount
hostinger