session - How can I restrict an external script to users that are logged in as an Administrator? -
i creating new page getting redirected administrator page in joomla 2.5.the page getting displayed when type url in browser . need restrict view such can visible when administrator logs account . can me on ?
this code:
define( '_jexec', 1 ); define('jpath_base', dirname(__file__) );//this when in root define( 'ds', directory_separator ); require_once ( jpath_base .ds.'includes'.ds.'defines.php' ); require_once ( jpath_base .ds.'includes'.ds.'framework.php' ); $app = jfactory::getapplication('site'); $user = jfactory::getuser(); if ($app->isadmin()) echo 'running joomla administrator site <br/>'; //$app->isadmin() through null value print_r($user); echo $user->username;
when login site code works when login using administrator interface code display's null value. don't know why. there mistake in it? output when login using administrator interface:
juser object ( [isroot:protected] => [id] => 0 [name] => [username] => [email] => [password] => [password_clear] => [usertype] => [block] => [sendemail] => 0 [registerdate] => [lastvisitdate] => [activation] => [params] => [groups] => array ( ) [guest] => 1 [lastresettime] => [resetcount] =>
when login site user site (member login) this:
juser object ( [isroot:protected] => [id] => 2 [name] => name of user [username] => username [email] => username@gmail.com [password] => 695c263968014c89bbf3159aa4:yobwr6uzmubmcqfj5hpzcip7a6mayd [password_clear] => [usertype] => [block] => 0 [sendemail] => 1 [registerdate] => 2014-11-24 [lastvisitdate] => 2014-11-25 15:29:45 [activation] => [params] => {} [groups] => array ( [2] => 2 ) [guest] => 0
i suffering problem last 8 days. can me this..
be sure you're loading joomla start (see this question @ joomla stackexchange),
update added complete script.
save code in php file located in root of joomla folder. work, , you'll see output. there, can adapt it's location , such work whatever you're trying accomplish.
if (!defined('_jexec')) { define( '_jexec', 1 ); define('jpath_base', realpath(dirname(__file__))); require_once ( jpath_base .'/includes/defines.php' ); require_once ( jpath_base .'/includes/framework.php' ); } defined('ds') or define('ds', directory_separator); $app = jfactory::getapplication('site'); $user = jfactory::getuser(); $groups = $user->groups; if ($app->issite()) echo 'running joomla front end site<br/>'; if ($app->isadmin()) echo 'running joomla administrator site <br/>'; if($user->id) { echo $user->username.' logged in<br/>'; if (isset($groups[8])) echo " - user super user <br/>"; if (isset($groups[7])) echo " - user administrator <br/>"; if (isset($groups[6])) echo " - user manager <br/>"; }else{ echo 'not logged in<br/>'; } if(!isset($groups[7])) die("you must administrator run this");
variation use in \administrator
if (!defined('_jexec')) { define( '_jexec', 1 ); define('jpath_base', realpath(dirname(__file__))); require_once ( jpath_base .'/includes/defines.php' ); require_once ( jpath_base .'/includes/framework.php' ); defined('ds') or define('ds', directory_separator); } //$app = jfactory::getapplication('site'); $app = jfactory::getapplication('administrator'); if ($app->issite()) echo 'running joomla front end site<br/>'; if ($app->isadmin()) echo 'running joomla administrator site <br/>'; $user = jfactory::getuser(); if($user->id) { echo $user->username.' logged in<br/>'; $groups = $user->groups; if(isset($groups[8])) { die("you super user - administrators can run this"); // superuser coding here }elseif(isset($groups[7])) { die("you administrator - can run this"); // admin coding here } }else{ echo 'not logged in<br/>'; }
Comments
Post a Comment