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..
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 trying to use WordPress logged in user id which should search transaction_user_ID in the table wp_m_subscription_transaction.
The table has a column called transaction_user_ID and another called transaction_paypal_ID.
logic:if user id == transaction user id then get transaction paypal id
which should be passed to the url along with the user id email address
and the execute the url to get the a code - this is my final output
How this can be achieved?
The code which I am developing is like this which is obviously incomplete & error for the getting and using the database query
<?php $user_id = get_current_user_id();
$query = "SELECT * FROM $wpdb->;wp_m_subscription_transaction
if ($user_id ==$query) {
Get ur Code
}
else {
echo 'You are not logged in as user ';}?>
First, since you need the email address as well, get all the current user's details, not just the id, using wp_get_current_user().
Secondly, your query is wrong; you haven't closed the quotes, and have a stray semicolon. If I've read it right, you need something like:
select transaction_paypal_ID
from wp_m_subscription_transaction
where transaction_user_ID = [the user's ID]
If you're only getting a single value in a query, you can retrieve it with $wpdb->get_var()
If the query doesn't return a row, it doesn't necessarily mean the user isn't logged in. You can check if a user is logged in using is_user_logged_in().
Something like this should work. I haven't tested, and you'll have to build up the URL yourself.
<?php
if (is_user_logged_in()) {
$user = wp_get_current_user();
$paypal_id = $wpdb->get_var("select transaction_paypal_ID from wp_m_subscription_transaction where transaction_user_ID = " . (int) $user->ID);
if ($paypal_id) {
// Build up your URL here, with $paypal_id and $user->user_email
}
else {
echo 'No transaction found for the current user';
}
}
else {
echo 'You are not logged in';
}
?>
Try this query.
$query = "SELECT transaction_paypal_ID FROM wp_m_subscription_transaction where transaction_user_ID = ".$user_id;
$result = $wpdb->get_row($wpdb->prepare($query), ARRAY_A);
$transaction_paypal_ID = $result['transaction_paypal_ID'];
This code comes without warrant, because you've not provided database structure, field names, or many other factors.
However, treated as pseudo-code, this will get you going in the right direction.
$user_id = get_current_user_id();
// Global / bring in $wpdb, and the $table_prefix variables
global $wpdb, $table_prefix;
// Use $wpdb prepare to be sure the query is properly escaped
$query = $wpdb->prepare("SELECT * FROM " . $table_prefix . "m_subscription_transaction WHERE user_id = %d", $user_id);
// Turn on error reporting, so you can see if there's an issue with the query
$wpdb->show_errors();
// Execute the query
$results = $wpdb->get_results($query);
// Are there any records? If so, that means the user ID matched
if ($results) {
$row = $results[0];
// Get the data from the row
echo 'Get ur Code';
} else {
echo 'No records for this user.';
}
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.
if(isset($_SESSION['admin'])) {
echo "<li><b>Admin</b></li>";
}
<?php
session_name('MYSESSION');
session_set_cookie_params(0, '/~cgreenheld/');
session_start();
$conn = blah blah
$query2 = 'Select Type from User WHERE Username = "'.$_SESSION['user'].'" AND Type =\'Admin\'';
$result2 = $conn->query($query2);
if($result2->num_rows==1) {
$_SESSION['admin'] = $result2;
}
?>
Hi, I'm trying to set this session variable but it doesn't seem to be setting, and i'm wondering if anyone can help. If session['admin'] isset it should echo the admin button.
But i'm not quite sure why? (I do have session start and everything on everypage, it's not a problem with that or any of the "You don't have php tags" I have checked the mysql query, and it does return something from my table. Any ideas please?
Your session_start(); should be at the top of the page before anything to do with the session variables.
From the docs:
When session_start() is called or when a session auto starts, PHP will call the open and read session save handlers.
Edit from comments:
<?php
session_name('MYSESSION');
session_set_cookie_params(0, '/~cgreenheld/');
session_start();
// Moved to start after answer was accepted for better readability
// You had the <?php after this if statement? Was that by mistake?
if(isset($_SESSION['admin']))
{
echo "<li><b>Admin</b></li>";
}
// If you have already started the session in a file above, why do it again here?
$conn = blah blah;
$query2 = 'Select Type from User WHERE Username = "'.$_SESSION['user'].'" AND Type =\'Admin\'';
// Could you echo out the above statement for me, just to
// make sure there aren't any problems with your sessions at this point?
$result2 = $conn->query($query2);
if($result2->num_rows==1)
{
$_SESSION['admin'] = $result2;
// It seems you are trying to assign the database connection object to it here.
// perhaps try simply doing this:
$_SESSION['admin'] = true;
}
?>
Edit 2 from further comments:
You have to actually fetch the fetch the data like this - snipped from this tutorial which might help you out some more:
$query = "SELECT name, subject, message FROM contact";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "Name :{$row['name']} <br>" .
"Subject : {$row['subject']} <br>" .
"Message : {$row['message']} <br><br>";
}
But having said that, while we are talking about it, you would be better off moving away from the old mysql_* functions and move to PDO which is much better.
Move session_start(); to the top of the page. You are trying to retrieve sessions, where it's not loaded.
EDIT: Try echoing $_SESSION['admin'], if it even contains something. Also try debugging your if($result2->num_rows==1) code by adding echo('its working'); or die('its working'); inside it, to check if $result2 contains exactly 1 row, since currently it seems $result2 contains either more than 1 row or no rows at all.
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.