PHP Session Variables in Joomla - joomla

I have the name of my user in $_SESSION["login"], and I'm trying to display this in Joomla, however shows the following "", and doesn't print anything.
In the PHP class, the variable appears correctly, but in Joomla it doesn't.

If the end goal is to get information regarding the currently logged in user, you should simply use the below core utility function to grab the current User object with all the info you could need.
$user = JFactory::getUser();
echo $user->username;
echo $user->id;
echo $user->email;
// and so on

You can use session in joomla with: JFactory::getSession() as follow:
$session = &JFactory::getSession();
$session->set("login","session_value")
and to get this session
$session->get("login")
Thanks.

Related

Preview Laravel Mail before sending

I want to edit my mail and change everything, if I want, as shown here.
Ive imported my file and created a test route to view the page:
use Illuminate\Mail\Markdown;
Route::get('/mail/html', function () {
$markdown = new Markdown(view(), config('mail.markdown'));
return $markdown->render('vendor.mail.html.message'); // or ..markdown.message
});
However, Im having variable errors for #slot. How to view my change/see what the mail looks like before sending? Another package for that?
Thanks
To preview your email in browser please add below code to route
Route::get('preview-notification', function () {
$markdown = new \Illuminate\Mail\Markdown(view(), config('mail.markdown'));
$data = "Your data to be use in blade file";
return $markdown->render("path-of-your-templete-blade-file", $data]);
});
and you will be access your templete using
http://your-application-url/preview-notification
This is the recommended way by the Laravel community
kunal has a nice quick solution and that's how I used to do it. But now I use mailtrap.io to test emails because it allows you to replicate the whole process of sending an email.
Create a (free) account with mailtrap.io.
Add your mailtrap username and password in .env file.
By the way, Laravel is already configured to use mailtrap by default, so it is their recommended way to test emails.
Watch how Jeffrey Ways does it in his lesson:
Laravel 5.4 From Scratch: Sending Email
https://laracasts.com/series/laravel-from-scratch-2017/episodes/26
If you want to test in locally. You can put echo command in blade file at end and put die; this way you can test.Suppose you have test-email.blade.php
test-email.blade.php // file name
This is tets mail
<?php echo "test"; die; ?>
Hope it helps!

Yii2 $session->setId() not working

I'm using Ajax to log in a user from subdomain. The Yii2 app is on another subdomain. Both subdomains are configured to use same cookie and session domains and save paths. I'm including session ID with Ajax call to write the user information to the same session used by non-app subdomain like this:
$session = Yii::$app->session;
$session->open();
$session->setId($post["session"]);
$session["user.id"] = $user->id;
echo $session->id; // This does not return the same ID originating from post!
Unfortunately the user information IS NOT written to the session already existing, but a new one. Is there a session involved somewhere in the middle of login process or why isn't it working? I've also tried session_id($post["session"]), but nothing.
This was actually working on previous domain, so I must be missing something. All of the AJAX posted info is correct and checked, the user is logged in properly (checked the logs) but into wrong session.
Thanks in advance!
yii\web\Session::setId() is a wrapper for session_id(), you should read PHP documentation about this function :
string session_id([ string $id ])
If id is specified, it will replace the current session id. session_id() needs to be called before session_start() for that purpose.
So you should simply try :
$session = Yii::$app->session;
$session->setId($customId);
$session->open();
I Don't think you are following the correct way to SET & GET session.
Try This:
$session = Yii::$app->session;
$session->open();
$session->set('id', $post["session"]);
echo $session->get('id');
For more info, please click Session Management - Yii2

Tell Joomla to be set to logged-in status via outside PHP script

I have a site that I am migrating from Joomla 1.7 to 3.3 and the previous site had a PHP script that when someone logged into a different server that handles all membership accounts (and completely separate from Joomla) to go back to the Joomla site and change to logged-in status as a registered user. How would I accomplish this in Joomla 3.3?
Here's a possible start (but incomplete):
$user = JFactory::getUser();
if($_GET['login']=='true'){
$user->guest = 0;
}
Something like this might work, but at the very least I don't know what all settings Joomla needs to determine that the current user is logged in.
Try below code:
if($_GET['login']=='true'){
$userid = $_GET['userid']; //This is the user id which you get from remote website.
$user = JFactory::getUser($userid);
$session =& JFactory::getSession();
$session->set("user",$user);
}
Using about code the user will be logged in on Joomla website.

Cakephp - Auth->login() returns true but next page I'm no longer logged in

I'm trying to code an SSO between Joomla and my CakePHP (1.3) app. Users should be able to log into Joomla, click a link to go to the CakePHP app and be logged in with the same user.
In my users_controller, I have an action called 'joomlalogin()' where I read the Joomla session variables, check to see whether the joomla user already exists in my CakePHP app, create the user if not. This all works.
The issue is that I can't seem to manually log the user in using Auth->login(). I tried passing a full user, tried passing the user_id. The function returns 'true' but as soon as I go to a next page, the login seems to have failed. Here's one version of the code:
$existing_user= $this->User->find('first', array('conditions' => array('User.joomla_userid' => $joomla_user->id)));
if ($existing_user && $this->Auth->login($existing_user)) {
$this->Session->setFlash('You have successfully logged in.');
//debug($this->Auth);
$this->redirect('/users');
}
The flash message appears, the redirect happens but it immediately gets redirected to the (regular non-sso) login form because the '/users' is not allowed for anonymous users.
Why is Auth->login() returning 'true' if the user is not really logged in. When I look at Auth->User(), it contains the correct user data, everything indicates the user is logged in until the redirect happens. Any idea what could be the cause of this or what I am doing wrong?
I was having the same issue. Turns out the system clock running on my server VM was incorrect. I ran this command to update it.
ntpdate time.windows.com
Note that this doesn't fix the underlying problem, which is that VM's have trouble keeping accurate time due to the nature of clock cycles being halted and suspended. Talk to your server administrator for a more permanent solution. Cheers to #Olivier for the tip
Check that sessions are enabled for all controllers you want to carry across user information; It's handy to put it in AppController, usually.
For example:
class AppController extends Controller {
public $helpers = array('Html', 'Form');
public $components = array('Auth', 'RequestHandler', 'Session', 'Security');
}
I sort of found the cause. The issue had something to do with the inclusion of the Joomla files (to access the Joomla session data & user). If I try to log the user in using a function where these files are not included, it does work. Some of the code that screwed it up was:
require_once (JPATH_BASE . DS . 'includes' . DS . 'defines.php');
require_once (JPATH_BASE . DS . 'includes' . DS . 'framework.php');
$joomla_mainframe = JFactory::getApplication('site');
$joomla_mainframe->initialise();
$joomla_session =& JFactory::getSession();
$joomla_user = $joomla_session->get('user');

Access cookie information from JOOMLA Article

My aim is to show the text - "Welcome username"
The username info is stored in the cookie. How do i access that cookie information to show it up in the Joomla! Article.
I would like to know if there's a way to do this without creating a module.
Thanks
You can't access the cookie from the article because cannot insert PHP code in the article content, it is stripped by the editor.
But you can create module which is quite easy all you have to use is:
$user =& JFactory::getUser();
echo 'Welcome ' . $user->name;
and add this module to your article using {loadposition myposition}
If you want to use the cookie you have set you can use:
echo 'Welcome ' . JRequest::getString('username', '', 'cookie');
Also you can have a look at this blank module, it;s good start:
http://extensions.joomla.org/extensions/edition/custom-code-in-modules/3668
Without creating any module, you can also directly use php code in joomla article by using some extensions as follows:
http://extensions.joomla.org/extensions/....custom-code-in-content/4470
http://extensions.joomla.org/extensions/1023/details
http://www.ostraining.com/blog/joomla/use-javascript-or-php-in-a-joomla-article/

Resources