Jump to content
TNG Community

Grabbing $currentuser details...concisely


subtext

Recommended Posts

OK, so if a user is logged in it's simple enough to get their username by checking $currentuser. Pretty much every page makes that obvious. But what if I want to display their real name and email address. Is there a concise (and secure) way of doing this?

Link to comment
Share on other sites

Hmm, figured this out myself I think. Not sure how secure or robust the code is. Any suggestions?


<?php

  // Ensures 'Photos & Histories for Ancestors of [person]' link automatically links to details 

  // of logged in user.  (Assumes Individual ID (Ix) is declared in user description field. )



if ($currentuser) {

	$query = "SELECT realname FROM $users_table WHERE username = "$currentuser"";

	$result = mysql_query($query) or die ("$text[cannotexecutequery]: $query");

	$row = mysql_fetch_assoc($result);

	$user_realname = $row["realname"];

	if ( $currentuserdesc == "Administrator" ){

  $user_record = "I1";

  $user_tree = "defaulttree";

	} else {

  $user_record = "$currentuserdesc";

  $user_tree = "$assignedtree";

	}

	mysql_free_result($result);

    

	echo "<li><a href="/extrastree.php?personID=$user_record&tree=$user_tree" class="lightlink">Photos &amp; Histories<br>for Ancestors of<br>$user_realname</a></li>";

	}

?>

I thought I'd post the code because I figured others might find this function quite useful.

Link to comment
Share on other sites

  • 10 months later...

Hmm, figured this out myself I think. Not sure how secure or robust the code is. Any suggestions?



<?php

 // Ensures 'Photos & Histories for Ancestors of [person]' link automatically links to details

 // of logged in user.  (Assumes Individual ID (Ix) is declared in user description field. )
if ($currentuser) {

    $query = "SELECT realname FROM $users_table WHERE username = "$currentuser"";

    $result = mysql_query($query) or die ("$text[cannotexecutequery]: $query");

    $row = mysql_fetch_assoc($result);

    $user_realname = $row["realname"];

    if ( $currentuserdesc == "Administrator" ){

 $user_record = "I1";

 $user_tree = "defaulttree";

    } else {

 $user_record = "$currentuserdesc";

 $user_tree = "$assignedtree";

    }

    mysql_free_result($result);

   

    echo "<li><a href="/extrastree.php?personID=$user_record&tree=$user_tree" class="lightlink">Photos & Histories<br>for Ancestors of<br>$user_realname</a></li>";

    }

?>

I thought I'd post the code because I figured others might find this function quite useful.

I tried to add this code to index.php (Somehow I knew it would not be easy) and I get this error:

Parse error: syntax error, unexpected T_VARIABLE in c:\Inetpub\wwwroot\index.php on line 155

The line in question is:

$query = "SELECT realname FROM $users_table WHERE username = "$currentuser"";

Any Ideas?

Link to comment
Share on other sites

I tried to add this code to index.php (Somehow I knew it would not be easy) and I get this error:

Parse error: syntax error, unexpected T_VARIABLE in c:\Inetpub\wwwroot\index.php on line 155

The line in question is:

$query = "SELECT realname FROM $users_table WHERE username = "$currentuser"";

Any Ideas?

extra quote mark just before the semi colon

Link to comment
Share on other sites

T_VARIABLE errors, in my experience, are often caused by a missing quote or semicolon or parenthesis somewhere BEFORE the line given in the error message. It doesn't kill the parser until that line, when it gives up and reports where it was when it bailed out.

Link to comment
Share on other sites

Even if you fix your php code, the underlying logic seems to be faulty. The ID number for each individual is not linked in any way to *anything* in the users table. So you can't pull a rabbit out of that hat because the rabbit isn't there.

The following code is yours with a few corrections. It works, but the result can't be what you wanted.

<?php

// Ensures 'Photos & Histories for Ancestors of [person]' link automatically links to details

// of logged in user. (Assumes Individual ID (Ix) is declared in user description field. )

if ($currentuser) {

$query = "SELECT realname FROM $users_table WHERE username = \"$currentuser\"";

$result = mysql_query($query) or die ("$text[cannotexecutequery]: $query");

$row = mysql_fetch_assoc($result);

$user_realname = $row["realname"];

if ( $currentuserdesc == "Administrator" ){

$user_record = "I1";

$user_tree = "defaulttree";

} else {

$user_record = "$currentuserdesc";

$user_tree = "$assignedtree";

}

mysql_free_result($result);

echo "<a href=\"extrastree.php?personID=$user_record&tree=$user_tree\">Photos & Histories<br>for Ancestors of<br>$user_realname</a>" ;

}

?>

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...