how can I set in a php-script the username of the user, that is logged in at the moment, as a variable?
I know, how I can get and display the current username, but how can I set this as a variable?
This is the code, which I want to set as a variable:
<?php
$user = JFactory::getUser();
$user->username;
?>
Thanks for your help.
It looks like you are already almost doing it.
<?php
$user = JFactory::getUser();
$username = $user->username;
?>
Though, $user->username is already containing this information, so I think that is why everyone is confused.
Related
I've a website that is built on plain php but at the same time the database is shared with blogging site (wordpress). In my php site, I load wordpress through ./wp-load.php. In this case, everything that loads in the wordpress can be loaded in the php site too. I can retrieve everything except that I can't seem to get/retrieve the ID.
The reason why I want to retrieve ID is to show user description in the php site because description meta data is saved from the wordpress end only..
Here is how I tried to retrieved the ID. Though tried several times in many ways but didn't get through.
$u = $page_username;
echo $u; //here it retrieves the page owner username/ login name
$user = get_user_by('login','$u');// here is the failure. If I directly put username instead of variable $u, I can echo the ID.
if($user)
{
echo $user->ID;
}
I'm not sure where the problem lies, but if I can echo username, why not ID?.. Or if I can retrieve ID by directly putting username, why not from variable?
The end result I'd like to get is something like this:
$u = $page_username;//this displays the current username
$user_id = $u->ID;
echo get_user_meta ($user_id, 'description' , true )// If I directly put user Id, I can retrieve the user description..
Note: If there is a way to retrieve user meta by simply using username/ user_login, then solution to retrieving user_id is not required. I can directly then retrieve usermeta by simply using user_login which is already there..
I hope wordpress gurus will help me out in this.. I'm fairly new to wordpress, so it's taking lot's of time to get through even to a simple functions or those hooks and all..
These codes have been tested and are working properly:
global $current_user;
$user_ID = $current_user->ID ;
$user_info = get_userdata($user_ID);
$user_name = $user_info->user_login;
You can use this
if ( !function_exists( 'get_user_by' ) ) {
require_once ABSPATH . WPINC . '/pluggable.php';
}
$user = get_user_by('login','$u');// here is the failure. If I directly put username instead of variable $u, I can echo the ID.
if($user)
{
echo $user->ID;
}
User following :
$user = wp_get_current_user();
echo $user->roles[0];
I am using a simple PHP/MYSQL login system using sessions and I need to print the username for the person currently logged in to the session.
The current code I am using to do this is:
<?php
session_start();
include_once 'dbconnect.php';
$res=mysql_query("SELECT username FROM users WHERE user_id=".$_SESSION['user']);
$userRow=mysql_fetch_row($res);
print_r($userRow)
?>
But the output of this is:
Array ( [0] => USERNAME )
While I would like it to be:
USERNAME
How would I achieve this, or is this possible?
Thank you in advance!
The value is in array. you have to access the index to get the value like this,
echo $userRow['username'];
I would suggest to store the username in session and access it directly instead of accessing from database everytime.
$_SESSION['username'] = "name here";
echo $_SESSION['username'];
EDIT
echo $userRow[0];
this should help you.
echo $userRow['username'];
Try this out.
this question may seem simple but I have been stuck on it for 3 hours.
Scenario:
An end-user logs into my website.
Question:
How can I print his credentials and store it as a variable?
I have been looking aimlessly and all I see is 50 lines of code with $_SESSION with using multiple php files.
In one page, can I grab the username and store it in a variable... done, thats all.
Thank you in advanced for your responses. Here is my code, you can check the comments to help you out.
<?php
class modProfilePrintHelper
{
public static function getHello( $params )
{
//GRAB USERNAME THAT IS LOGGED IN
//STORE USERNAME IN VARIABLE
//Obtain a database connection
$db = JFactory::getDbo();
//Retrieve the shout TOTAL
$query = $db->getQuery(true)
->select($db->quoteName('smiles'))
->from($db->quoteName('#__smiles_users'))
->where('username = '. $db->Quote('USERNAME VARIABLE HERE'));
//Prepare the query
$db->setQuery($query);
// Load the row.
$result = $db->loadResult();
//Return
echo "<h3>";
echo 'Your Number of Smiles: ';
echo "</h3>";
echo '<font size = "5"><h1>' . $result . '</h1></font>' . '<br>';
}
}
?>
Try this,
Joomla users will be available using User Object.
$user = JFactory::getUser();
echo 'Hello ,'.$user->name;
More about user object in Joomla
For using session variables in Joomla, do not try with normal session use Joomla session library.
like below.
$session = JFactory::getSession();
$session->set('variable','your_value');
echo $session->get('variable');
Joomla session Objects
Hope it helps..
i am making my own php game. So far i have made almost everything. Now to finish it, i need to get id from user who is logged in. I'm not so familiar with the functions and sessions. Please help.
This is what i made so far:
In my index page people login. then they are redirected to this.
So $_POST['username'] is where user type his user name in index.
<?php
$username = $_POST['username'];
include("Files/config.php");
$connect = #mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD);
if($connect) {
if(mysql_select_db(DB_NAME)) {
$sql = mysql_query("SELECT * FROM users WHERE `username`='$username'") or die(mysql_error());
$gatherinfo = mysql_fetch_array($sql);
global $getid;
$getid = $gatherinfo['id'];
echo $getid;
function getuid() {
$_SESSION['getuid'] = $getid;
echo $getid;
}
}
}
else{ echo "Can not connect";}
?>
I searched other scripts for this, i found on one it says just $session->uid and it shows his id from mysql.
In mysql database i have table users with info about them
Id, username, password (password is hashed), email,...
Please help me if you can :D
At the beginning of index file (where your user logging in) start named session (be careful to avoid echo or print any values before session_start:
<?php //index.php
session_name('SAMPLESESSION');
session_start();
then when you will get the logged User ID, write this value to the session variable, like this:
.....
$_SESSION['uid'] = $getid;
.....
in the script you was redirected by your index file start session with the same name and get your user ID:
<?php //redirectedfromindex.php
session_name('SAMPLESESSION');
session_start();
echo $_SESSION['uid'];
....
If I right understand you, these that you need.
I was wondering how I would go about getting the user's username. What i want to do is display it like this:
Username here
I tried this:
{$mybb->user['name']}'
But that was unsuccessful and I cannot seem to find anything on Google.
Thanks for any help!
I am not that experienced with MyBB but after some research i found a few different ways.
$user = get_user($uid);
echo $user['username'];
or
global $db;
$qry = $db->query("SELECT uid FROM ".TABLE_PREFIX."users WHERE username = '".$usernamevar."'");
And you can combine all like this, i think.
<?php
define("IN_MYBB", 1);
require ('global.php'); // be sure that u r running this php-code in the same
// directory of global.php, or else change the path.
if($mybb->user['uid'] > 0)
{
$uid = $mybb->user['uid'];
$user = get_user($uid);
$name = $user['username'];
}
// custom else here: in case of not logged-in user
?>
<?echo $name?>
Try to put this one into your template.
{$mybb->user['username']}
No need to use PHP for an existed variable.