I have tried making code to tell a user that they need to be logged in to view a link, and part of the link will also be generated by Jfactory, I am writing this code on a separate php file and I have tried all sorts of things from stackoverflow, and I keep getting you are logged in and can get different results here is my code:
<?php
define( 'DS', DIRECTORY_SEPARATOR );
$rootFolder = explode(DS,dirname(__FILE__));
//current level in diretoty structure
$currentfolderlevel = 9;
array_splice($rootFolder,-$currentfolderlevel);
$base_folder = implode(DS,$rootFolder);
if(is_dir($base_folder.DS.'libraries'.DS.'joomla'))
{
define( '_JEXEC', 1 );
define('JPATH_BASE',implode(DS,$rootFolder));
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
$userid='';
$usertype='';
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
$user =& JFactory::getUser();
$userid = $user->get('id');
$usertype = $user->get('usertype');
}
if ($user->guest) {
echo 'You are not logged in';
} else {
echo 'You are logged in';
}
?>
Also my joomla version is Joomla 3.2.4 and the url to the code i am referring to is https://wgalive.com/teamspeakinfo.php
I managed to find a solution to my problem I had to install an extension to embed php code into my article on joomla: Sourcerer Code Helper and then had to use the following code and it worked:
<?php
defined('_JEXEC') OR die("Restricted access"); // Prevent direct Access to the program
$user =& JFactory::getUser(); // get user
if ($user->guest) {
echo "<h3>Please log in to see your teamspeak login link! </h3>"; // Show this message if user not yet log in
} else {
echo "<h3>Hello {$user->name}! Below is a link to logging into our teamspeak server with your username!</h3>";
echo '<p><a href="ts3server://srv.wgalive.com?port=9987&nickname='.$user->username.'&password=passwordhere&addbookmark=WGA-Server">Login to Teamspeak</a></p>';
}
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.