i'm trying to add arrowchat, an tchat box from arrowchat.com And these file is just using php without using twig in my website and i don't know how to do for integrate this file with my session variable from twig.
This is what i need to do for integrate these file:
First, open the arrowchat/includes/integration.php file. This file must be customized to fit your specific website.
get_user_id() Function
The first and most important function to setup is the get_user_id() function. This function will simply return the user's ID or NULL if no user is logged in. The two most common ways to setup this function are by cookie and session.
Using a Session example:
function get_user_id()
{
$userid = NULL;
if (!empty($_SESSION['userid']))
{
$userid = $_SESSION['userid'];
}
return $userid;
}
My problem is:
It's not working and i think it's because $_SESSION['userid'] doesn't exist.
How can i do for take the user id from my session without using twig?
I'm using FOSUserBundle for making the session when i connect to my website
Thanks all :)
If you are into a controller you can do this with FOSUserBundle:
$user = $this->getUser();
if (null != $user) {
$userid = $user->getId();
}
Related
I am manually modifying some key of Auth session in beforeFilter of AppController.php by this code
public function beforeFilter(Event $event){
//$companyId = $this->Companies->find(.........
$this->request->session()->write('Auth.User.company_id', $companyId);
}
And in various actions of different controller i'm trying to get that stored companyid in session by following way
public function add(){
$companyId = $this->Auth->user('company_id');
debug($companyId); die;
}
When i see the value of $companyId, it is showing older value not updated one in beforeFilter method of AppController. However if i refresh the page and donot modify session again i will get updated $companyId value.
So my question is how can i update the value of Auth session data so that i can get updated value with $this->Auth->user('company_id') code in different places in same request?
The session storage used by the authentication component uses a buffering mechanism, ie the value from the session is usually only read once per request (unless it is being deleted/emptied).
https://github.com/cakephp/cakephp/blob/3.2.8/src/Auth/Storage/SessionStorage.php#L81-L83
So either read the value directly from the session in your controller action, or do not write to the session directly, but to the session storage, something along the lines of
$user = $this->Auth->user();
if (is_array($user) && $user) {
$user['company_id'] = $companyId;
$this->Auth->setUser($user);
}
As follow user sessions in Symfony2 , as is the way of working with sessions?
I need to keep the user session by Symfony2 for example, use their name, or that I have some data stored in a database . As recommended me to work ?
im using symfony 2.4
thanks
In your controller, you can access session via
$session = $this->getRequest()->getSession();
$session->set("username", $username);
// ... later
$username = $session->get("username")
A more real-life example is one I use to show a banner only once per visit.
public function bannerWidgetController(Request $request)
{
$session = $request->getSession();
if ($session->get('banner-visited', false))
{
return new Response();
}
$session->set('banner-visited', true);
return $this->render('widget/banner.html.twig');
}
Then I include this in my TWIG via the "renderController" method.
As your question is a bit hard to understand I don't know if it is exactly what you want but there is already a mechanism included in Symfony2 to store sessions in database.
There is a simple guide you can follow here :
http://symfony.com/doc/2.4/cookbook/configuration/pdo_session_storage.html
After the user has logged in I want to be able to save the userId for later use within the application. The only place in the application I retrieve the username is from the login form, through the login controller. However, that structure in my application is that the only thing that is passed to my master controller from the login controller is HTML.
Of course I could include the userId in a hidden field inside the HTML that's passed back to the master controller, but that seems too hacky.
So, is there a way that I can save a value (in this case the username) so that it's accessible from other classes/namespaces/functions whatever? I have read a bit about 'global', but haven't managed to get it work in my application.
from LoginController.php:
if ($loginView->TriedToLogin()){
$loginUsername = $loginView->GetUserName(); //Retrieved from form
$loginPassword = $loginView->GetPassword();
}
Upon login, you need to store your user token in a session.
See: http://au1.php.net/manual/en/features.sessions.php
Store user when logging in:
$_SESSION['user_id'] = 32; // fetch from your user provider
You can then write a class/function that utilises the session to check their login status and fetch their details when required.
Like so:
function getUserId()
{
return isset($_SESSION['user_id']) ? $_SESSION['user_id'] : false;
}
function isLoggedIn()
{
return isset($_SESSION['user_id']) && is_numeric($_SESSION['user_id']);
}
Then use anywhere in your application:
echo isLoggedIn() ? getUserId() : 'Anonymous';
Also, for great information on how to build an MVC framework, check out "Create your own framework... on top of the Symfony2 Components".
How about Sessions?
Session support in PHP consists of a way to preserve certain data
across subsequent accesses.
http://de2.php.net/manual/en/features.sessions.php
If it's only the username you want store, I would go with $_SESSION[].
It's not the most secure in a (shared) hosted environment, but it's so easy to call session_start(); first thing on pages using the stored data.
From my application view I need to programmatically logout current user and login another one right after that.
I want to login the second user into his own different CHttpSession (with another sessionID and so on). I need it for a security reasons.
How to implement this in Yii framework ?
Code below
$oSession->destroy();
$oSession->open();
doesn't work as expected..
looks like you are trying to impersonate users:
Create a function in your UserIdentity that would alow you to login as another known user:
protected function logInUser($user)
{
if($user)
{
$this->_user = $user;
$this->_id=$this->_user->id;
$this->setState('name', $this->_user->name);
$this->errorCode=self::ERROR_NONE;
}
}
In your controller, call this function to get the UserIdentity object and then use the Yii's CWebUser login
$ui = null;
$user = User::model()->findByPk($userId);
if($user)
{
$ui = new UserIdentity($user->email, "");
$ui->logInUser($user);
}
Yii::app()->user->login($ui, 0);
Remember to protect this controller's action from non authorized users.
A possible tricky way (tested):
session_unset();
Yii::app()->user->id = $the_new_id;
When the above code is executed, nothing visible happens on the page so you may want to redirect the browser:
$this->redirect('somewhere');
Upon the next page load, the user with the $the_new_id will be logged in
I am trying to integrate my login system made with PHP with the PHPBB Login system.
My problem is that I am including the PHP login document which contains a class called $user but my login system uses $user as well.
e.g My function for login is executing inside a class called $user and the phpbb login class is $user->login
Is it possible to load the phpbb document, and login in a separate kind of "environment" to my main website?
If you need any more info just let me know
You could run your code in a function. Functions aren't passed global variables if you don't explicitly tell them ;)
Can you not change the variable?
Such as
<?php
include 'the/phpbb/core.pohp';
$phpbb_user = $user;
include 'my/login.pohp';
if($user->valid_uid($phpbb_user->uid))
{
}
?>
edits:
Can you add a second variable
Open common.php and find the following:
$user = new user();
add After
$backup_user = $user;