Page 1 of 1

How to retrieve username?

Posted: Fri Feb 22, 2008 1:03 pm
by deegiijoom
Hi all, how to get username?
anyone knows this help me.
I did
echo($_SESSION['username']);
but nothing output.
else I may be need to change ['username'].
do you have another way?

Re: HOW TO GET USER NAME?

Posted: Fri Feb 22, 2008 2:06 pm
by tusker
Do you want it as a frontpage out put or what? Tell exactly what you want to do so that you can get proper help. Check in your sql DB and replace it with the value there..

Re: HOW TO GET USER NAME?

Posted: Fri Feb 22, 2008 2:58 pm
by deegiijoom
I made little web chat system but I don't know how to get username.
when user logged in as a username but I don't know how to get the username.
I use joomla 1.5 .
how does the joomla register the user in session?.
e.g /session_register("username"); /
$_session['username'];
I think I need to use another word instead of username.

your any help is important me.
thank you for replying.

Re: HOW TO GET USER NAME?

Posted: Fri Feb 22, 2008 3:10 pm
by igrimpe
Dig into the Joomla Framework ...
All necessary informations are accessed via Jxxxxx classes. In your case, JUser, JApplication etc would be a good starting point.

Re: HOW TO GET USER NAME?

Posted: Fri Feb 22, 2008 3:35 pm
by iosoft
use this -

Code: Select all

global $my;
$my->id;
$my->name
print_r($my);
Then send this info as GET parameter to your chat software.

Re: HOW TO GET USER NAME?

Posted: Fri Feb 22, 2008 4:01 pm
by igrimpe
Only works if legacy mode is enabled:

Code: Select all

// Handle legacy globals if enabled
		if ($mainframe->getCfg('legacy'))
		{
			// Include legacy globals
			global $my, $database, $acl, $mosConfig_absolute_path;
If legacy mode is not enabled you are still on:
JFactory::getUser();
;)

Re: HOW TO GET USER NAME?

Posted: Fri Feb 22, 2008 4:21 pm
by deegiijoom
hi all,
many thanks for all replying.

I think It isn't difficult getting the username from currenty SESSION.
when user logged in my site. The Joomla starts a SESSION with username.
I will try all advices.
I'll finish it.
thanks. :)

Re: HOW TO GET USER NAME?

Posted: Fri Feb 22, 2008 4:29 pm
by Aurogha
<html>
<head>
<title>Documento senza titolo</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
Inserimento di una Nuova Segnalazione :
<form action="ConfermaPegasoCS.php" method="post">
<p>Data della segnalazione:
<input type="text" Name="txtdatseg">
<br>
Ragione Sociale:
<input type="text" Name="txtragsoc">
<br>
Cognome:
<input type="text" Name="txtcognome">
<br>
Nome:
<input type="text" Name="txtnome">
<br>
Indirizzo:
<input type="text" name="txtindirizzo">
<br>
C.A.P.:
<input type="text" name="txtcap">
<br>
Località:
<input type="text" name="txtlocalita">
<br>
Provincia:
<input type="text" name="txtprovincia">
<br>
Partita IVA:
<input type="text" name="txtpariva">
<br>
Telefono:
<input type="text" name="txttelefono">
<br>
Fax:
<input type="text" name="txtfax">
<br>
Email:
<input type="text" name="txtemail">
<br>
<input type="submit" Value="Aggiungi" >
</p>
<p>NomeLogIn: <input type="text" Name="IDLog">

<?php /////////////////////////////////////////////////////// ?>

</form>


</body>
</html>

how i can input on textbox IDLog the username of current session? plz i need someone help me!!!

Re: HOW TO GET USER NAME?

Posted: Fri Feb 22, 2008 4:37 pm
by Aurogha
i use this code but i think it isn't correct

$VarUser = $my-> username
print "the username is $VarUser"

i need to insert in a variable current session username sorry if i repeat my message but i think that the people can't understand my problem..

Re: HOW TO GET USER NAME?

Posted: Fri Feb 22, 2008 4:45 pm
by Aurogha
the code of the page after the click on the button by the user is it:

<html>
<head>
<title>Documento senza titolo</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
Inserimento di una Nuova Segnalazione :
<form action="ConfermaPegasoCS.php" method="post">
<p>
Data della segnalazione:
<input type="text" Name="txtdatseg">
<br>
Ragione Sociale:
<input type="text" Name="txtragsoc">
<br>
Cognome:
<input type="text" Name="txtcognome">
<br>
Nome:
<input type="text" Name="txtnome">
<br>
Indirizzo:
<input type="text" name="txtindirizzo">
<br>
C.A.P.:
<input type="text" name="txtcap">
<br>
Località:
<input type="text" name="txtlocalita">
<br>
Provincia:
<input type="text" name="txtprovincia">
<br>
Partita IVA:
<input type="text" name="txtpariva">
<br>
Telefono:
<input type="text" name="txttelefono">
<br>
Fax:
<input type="text" name="txtfax">
<br>
Email:
<input type="text" name="txtemail">
<br>
<input type="submit" Value="Aggiungi" ><?php print "sei connesso con l'utente $VarUtente"?>
</p>
</form>

<?php
//connessione al db
$hostname = "localhost";
$username = "root";
$password = "";
$conn = mysql_connect($hostname,$username,$password);
if (! $conn)
{
echo ("Errore durante la connessione a MySQL.");
exit();
}
else
{
echo ("Connessione a MySQL effettuata con successo.");
}
//inizio operazioni
//selezione del database
mysql_select_db("pegasocs");
//assegnazione delle variabili
$VarCodSeg = " ";
$VarDatSeg = $_POST["txtdatseg"];
$VarRagSoc = $_POST["txtragsoc"];
$VarCognome = $_POST["txtcognome"];
$VarNome = $_POST["txtnome"];
$VarIndirizzo = $_POST["txtindirizzo"];
$VarCap = $_POST["txtcap"];
$VarLocalita = $_POST["txtlocalita"];
$VarProvincia = $_POST["txtprovincia"];
$VarParIva = $_POST["txtpariva"];
$VarTelefono = $_POST["txttelefono"];
$VarFax = $_POST["txtfax"];
$VarEmail = $_POST["txtemail"];
$VarPartner = "HERE I NEED TO MEMORIZE THE USERNAME OF CURENT SESSION";
$VarStaSeg = " ";
$VarNote = "";
//Codice SQL
$strSQL = "INSERT INTO segnalazioni ( CodiceSegnalazione, DataSegnalazione, RagioneSociale, Cognome, Nome, Indirizzo, CAP, Localita, Provincia, PartitaIVA, Telefono, Fax, Email, Partner, StatoSegnalazione, Note) ";
$strSQL .= "VALUES ('$VarCodSeg', '$VarDatSeg', '$VarRagSoc', '$VarCognome', '$VarNome', '$VarIndirizzo', '$VarCap', '$VarLocalita', '$VarProvincia', '$VarParIva', '$VarTelefono', '$VarFax', '$VarEmail', '$VarPartner', '$VarStaSeg', '$VarNote')";
if (! mysql_query($strSQL))
{
echo("Errore nel comando INSERT");
exit();
}
echo("Segnalazione effettuata con successo.");
//fine codice SQL
//invio di una mail
mail("dago_goda@yahoo.it","Nuova Segnalazione effettuata in data $VarDatSeg", "nuova segnalazione inviata, vedere segnalazione del sig. $VarCognome $VarNome o eventualmente contattare tramite l'indirizzo email $VarEmail ");
//chiusura della connessione al db
mysql_close($conn);

?>
</body>
</html>

How to retrieve username for use in chat system?

Posted: Wed Feb 27, 2008 5:47 pm
by deegiijoom
Hi all,
I use joomla 1.5
how to get currenty logged username?.
my usage is putting the username in chat system.

Re: How to retrieve username for use in chat system?

Posted: Wed Feb 27, 2008 7:37 pm
by pe7er
You can use something like:

Code: Select all

global $my;
$username = $my->username;
to retrieve the username.

Re: How to retrieve username for use in chat system?

Posted: Thu Feb 28, 2008 8:26 am
by Aurogha
Peter, i use this code for do an example and verify the effective code working:

Code: Select all

<?php 
  global $my;
  $username = $my->username;
  print "your username is $username " 
  ?>
the output is this:

Code: Select all

your username is
what's i wrong?

Re: How to retrieve username for use in chat system?

Posted: Thu Feb 28, 2008 9:22 am
by igrimpe
hmmm. let's think ...
you create a php file. upload it. run it and you get this result?
I wonder: What makes you think, that if you run any php script OUPTSIDE THE SCOPE OF JOOMLA, you have access to any variable, object, etc that is part of the Joomla framework?
You could change the code to:

Code: Select all

global $world;
  $diameter= $world->diameter;
  print "the diameter of the world is $diameter" 
[/quote]
The result is the same as with "$my" ...

I'd suggest to download a simple Joomla module and to is it as starting point for own experiments.

EDIT:
Or check this: http://forum.joomla.org/viewtopic.php?f=483&t=63383

Re: How to retrieve username for use in chat system?

Posted: Thu Feb 28, 2008 10:36 am
by Aurogha
i forget to tell you that my joomla version is the version 1.0.13 ..... it is a problem?

Re: How to retrieve username for use in chat system?

Posted: Thu Feb 28, 2008 10:39 am
by igrimpe
it is a problem?
Not for me. After being in the wrong category "security" it's now in the wrong category "1.5 - General questions"

Maybe you start with one of the HelloWorld tutorials that I mentioned?
Maybe you ask any further question about writing code for Joomla in one of the developer categories?

Re: How to retrieve username for use in chat system?

Posted: Thu Feb 28, 2008 11:01 pm
by oc666
Try this two functions(J!1.5):

Code: Select all

function _getUser()
	{
		$session =& JFactory::getSession();
		$user = $session->get("user");
		return $user->username;
	}

	function _getUserId()
	{
		$session =& JFactory::getSession();
		$user = $session->get("user");
		return $user->id;
	}

Re: How to retrieve username for use in chat system?

Posted: Tue Apr 15, 2008 8:21 am
by Alex53
Thanks oc666 that worked perfectly!

I have a little question. What other session variables are available? I cant seem to find documentation on this!

Re: How to retrieve username for use in chat system?

Posted: Tue Apr 15, 2008 11:42 am
by oc666
Alex53 wrote: I have a little question. What other session variables are available? I cant seem to find documentation on this!
Check the JSession:
http://smink.student.utwente.nl/joomla1 ... ssion.html
JFactory:
http://smink.student.utwente.nl/joomla1 ... ctory.html

And google sometimes ;)

Re: How to retrieve username for use in chat system?

Posted: Mon Jun 23, 2008 7:59 am
by SpArKy87
Can I somehow forward the JUser Object to a subfolder index.php?

Re: How to retrieve username for use in chat system?

Posted: Mon Jun 23, 2008 3:08 pm
by oc666
SpArKy87 wrote:Can I somehow forward the JUser Object to a subfolder index.php?
What do you mean forward the JUser?

Re: How to retrieve username for use in chat system?

Posted: Sun Jul 20, 2008 4:51 am
by zrnchikw
Okay... I'm trying to follow along and it isn't working. I've got a 1.5 install and phpFreeChat in a sub directory. I am trying to pass the Joomla username to phpFreeChat's $nick variable.

Joomla 1.5 is here => http://community.mysite.com
phpFreeChat is here => http://community.mysite.com/chat

I have tried modifying PFC's index.php from:

Code: Select all

function _getUser()
require_once dirname(__FILE__)."/src/phpfreechat.class.php";
$params = array();
$params["title"] = "Chat Name";
$params["nick"] = "Guest".rand(1,1000);  // setup the intitial nickname 
$params["isadmin"] = true; // do not use it on production servers ;)
to the following:

Code: Select all

function _getUser()
   {
      $session =& JFactory::getSession();
      $user = $session->get("user");
      return $user->username;
   }

   function _getUserId()
   {
      $session =& JFactory::getSession();
      $user = $session->get("user");
      return $user->id;
   }

require_once dirname(__FILE__)."/src/phpfreechat.class.php";
$params = array();
$params["title"] = "Chat Name";
$params["nick"] = & JFactory::getUser();  //"Guest".rand(1,1000);  // setup the intitial nickname 
$params["isadmin"] = true; // do not use it on production servers ;)
I get the infamous error:
Fatal error: Class 'JFactory' not found in /home/mydomain/public_html/community/chat/index.php on line 20
with $params["nick"] = & JFactory::getUser(); //"Guest".rand(1,1000); // setup the intitial nickname on line 20. I'm guessing it is because I have not declared JFactory anywhere in PFC... but where do I do that?

I'm quite sure I'm missing more than I imagine, but can someone point me in the right direction?

Thanks
Will

Re: How to retrieve username for use in chat system?

Posted: Mon Aug 25, 2008 6:05 pm
by dizzi
Hi

You are doing a few thing wrongly here.

Firstly you are using a "=&" assignment operator for the "nick" parameter which most likey will not be recognised.
Secondly the function:

Code: Select all

function _getUser()
   {
      $session =& JFactory::getSession();
      $user = $session->get("user");
      return $user->username;
   }
returns the session's logged in username, which is the value you are after. So instead of what you currently have:

Code: Select all

$params["nick"] = & JFactory::getUser();
you need,

Code: Select all

$params["nick"] = _getUser();
Regards