Show User’s Name On Website Header

Paramita Daniswari
2 min readApr 30, 2024

--

PHPMaker Tutorial Series

Hi All, welcome to PHPMaker tutorial series!

After login into the website, it will be nice if we can see the user’s name on the header. Here’s the tutorial to show the User’s Name on The Header in PHPMaker.

Picture 1. User’s Name on Website Header

Pass User’s Name In Session Variable

PHPMaker’s session didn’t provide User’s name variable by default, so we have to get it from user validated function and stored it in website’s session variable. User validated function contain our data in users table (table used for login). Before we setting the user validated function, look at our users table

Picture 2. Users Table

as we can see, Users Table contain all users information including their name. Please have a note for the column name we want to get, in this case column for User’s name called “nama”.

Picture 2. Set The Variable

We will asign “nama” from users table into session variable called “UserNamaLengkap”.

Here’s The code

// User Validated event
function User_Validated(&$rs)
{
// Example:
//$_SESSION['UserEmail'] = $rs['Email'];
if (IsLoggedIn() && !IsSysAdmin()) {
$_SESSION['UserNamaLengkap'] = $rs['nama'];
}
}

Call it

After assign the variable, now we have to call it on the header.

Picture 3. Write Code to Call

Go to Code > Server Event > Global > All Pages > Page_Foot. And then copy paste this code

<script type="text/html" class="ew-js-template" data-name="myControlSidebar" data-method="prependTo" data-target="#ew-navbar-right" data-seq="10">
<li class="nav-item">
<b><a class="nav-link" data-widget="control-sidebar" data-slide="true" href="#"><?php error_reporting(E_ALL ^ E_NOTICE); if($_SESSION["UserNamaLengkap"]){ echo 'Welcome:'.$_SESSION["UserNamaLengkap"];}else{echo 'Hai!';} ?></a></b>
</li>
</script>

Make sure the Session Name is correctly spelled.

Try it Out!

After we write the code, re-generate the code and try accessing the website. Because it use session variable, please log out and re-login to refresh the variable.

That’s all. Hope it helps!

--

--

Paramita Daniswari
Paramita Daniswari

Written by Paramita Daniswari

Programming Enthusiast - The written words yearn for timeless wings

No responses yet