PHP Variables with the same name - php

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;

Related

Symfony Session variable from php file GetuserID

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();
}

How do I create a link in an HTML block that dynamically inserts profile fields in the URL?

I want to be able to create a block in Moodle that links to a Google Form. But, the block needs to insert some information from the user's profile. Specifically, the username, ID number, and time. I know how to create the Google Form and use the "Fillable Link."
How can I accomplish this task? It could also be inserted into the theme's menu, too.
You need to create a new plugin of type "block" as documented here.
If you only need to insert some HTML or even basic PHP stuff, you simply need two files.
Create a new folder inside the Moodle "blocks" directory, example /var/www/moodle/blocks/googleform
Inside this new directory, create a file named version.php. Example :
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2017051500;
$plugin->requires = 2017050500;
$plugin->component = 'block_googleform';
In the same directory, create a block_googleform.php file :
defined('MOODLE_INTERNAL') || die();
class block_googleform extends block_base {
public function init() {
$this->title = get_string('pluginname', 'block_googleform');
}
public function get_content() {
global $USER;
$username = $USER->username;
$userid = $USER->id;
$this->content = "put your Google form here";
return $this->content;
}
}
Then, visit your Moodle homepage to let the software guess you created a new plugin. Install it and try adding a block instance of type "googleform".
If you prefer rely on your theme only, you could try to use the same global $USER infos in the PHP layouts files.

Codeigniter session generates new session on every page

On every page I see that new session is generated with null userdata
On model constructor
$this->config->set_item('sess_table_name', 'xx_sessions');
Because I want to store this session in another table because the other session table is being used for another login activity
Login function
function login($username,$password)
{
$this->db->where('login',$username);
$this->db->where('pass',$password);
$q=$this->db->get('prof');
// print $this->db->last_query();
if($this->db->count_all_results())
{
$arr=$q->row();
// creating the session
$this->session->set_userdata('login',$arr->id);
$this->session->set_userdata('prof',$arr->profile_id);
// print_r( $arr);
}
else
return FALSE;
}
This login function is on a model. After login and generating the session the page redirects to another page, on that page I see the session builds without any problem but when I move to another page the session losses along with the userdata.
I use the following function to check session data
function print_session()
{
print_r( $this->session->all_userdata());
}
Where I'm wrong ? Tank_auth library and ion_auth library works fine .. I had already used the
Put the session library name into the autoloader configuration, in application/config/autoload.php:
$autoload['libraries'] = array('session');
Then it's available automatically in each controller and everywhere in your application and you get your session data from anywhere:
$session_id = $this->session->userdata('session_id');
And if you don't want to auto load session library then you have to initialize the Session class manually in your controller constructor, use the $this->load->library function:
$this->load->library('session');
For details have a look here:http://ellislab.com/codeigniter/user-guide/libraries/sessions.html
Edit /application/config/config.php and set cookie domain variable
$config['cookie_domain'] = ".yourdomain.com";
It will work!
.yourdomain.com makes the cookie available throughout the domain and its sub-domains.
I have met same problem, and i have searched lots of pages.
I figured out that changing sess_cookie_name solves the problem(new sessions generating issue)
$config['sess_cookie_name'] = 'somenewname'

Global access to variable

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.

How to programmatically recreate php yii session?

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

Categories