Using CurrentUserID() in Custom Files

Hi,
I want to use CurrentUserID() function in my custom page (common files not included).
I receive the following error:Fatal error: Uncaught Error: Call to a member function currentUserID() on null in C:\xampp\htdocs\pedra\src\phpfn.php:4132 Stack trace: #0 C:\xampp\htdocs\pedra\getchats.php(65): PHPMaker2021\PEDRA\CurrentUserID() #1 {main} thrown in C:\xampp\htdocs\pedra\src\phpfn.php on line 4132Thanks

Please post your related code in that Custom File for more discussion.

Hi,
This is the entire page :

<?php
namespace PHPMaker2021\PEDRA; 
use PHPMaker2021\PEDRA\{UserProfile, Language, AdvancedSecurity, Timer, HttpErrorHandler};
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Container\ContainerInterface;
use DI\Container as Container;
use DI\ContainerBuilder;
use Selective\SameSiteCookie\SameSiteCookieConfiguration;
use Selective\SameSiteCookie\SameSiteCookieMiddleware;
use Selective\SameSiteCookie\SameSiteSessionMiddleware;
use Slim\Factory\AppFactory;
use Slim\Factory\ServerRequestCreatorFactory;
use Slim\Exception\HttpInternalServerErrorException;
use Nyholm\Psr7\Factory\Psr17Factory;
use Nyholm\Psr7Server\ServerRequestCreator;
global $RELATIVE_PATH;
// Auto load
require_once "vendor/autoload.php";
require_once "src/constants.php";
require_once "src/config.php";
require_once "src/phpfn.php";
require_once "src/userfn.php";
	$sql = "SELECT exchat.*, patients.FullName FROM exchat join patients on exchat.PatientID = patients.PatientID WHERE exchat.PatientID = '".$_COOKIE['PatientID']."' ORDER BY exchat.ID DESC";
	$stmt = ExecuteQuery($sql);
	if ($stmt->rowCount() > 0) { 
	while ($row = $stmt->fetch()) {
	if ($row['User'] == 0){
	$sendername = "کاربر";
	$clss = "direct-chat-msg";
	$senderclss = "direct-chat-name float-left";
	$dateclss = "direct-chat-timestamp float-right";
	$photoclss = "direct-chat-timestamp float-right";
	$toolsalign = "float-right";
	$photoiconclass = "text-success";
	$painiconclass = "text-success";
	$postureiconclass = "text-success";
	$trashiconclass = "text-danger";	
	} else {
	$levelID = ExecuteScalar("SELECT LevelID FROM members WHERE MemberID = '".$row['User']."'");	
	if ($levelID == 0){
	$sendername = "کاربر";
	$clss = "direct-chat-msg";
	$senderclss = "direct-chat-name float-left";
	$dateclss = "direct-chat-timestamp float-right";
	$photoclss = "direct-chat-timestamp float-right";
	$toolsalign = "float-right";
	$photoiconclass = "text-success";
	$painiconclass = "text-success";
	$postureiconclass = "text-success";
	$trashiconclass = "text-danger";	
	} else {
	$sendername = "درمانگر";
	$clss = "direct-chat-msg right";
	$senderclss = "direct-chat-name float-right";
	$dateclss = "direct-chat-timestamp float-left";
	$photoclss = "direct-chat-timestamp float-left";
	$toolsalign = "float-right";
	$photoiconclass = "text-light";
	$painiconclass = "text-light";
	$postureiconclass = "text-light";
	$trashiconclass = "text-danger";	
	}
	}
	if ($row['User'] == CurrentUserID()){
	$trashIcon = "<div class=\"showtrash $toolsalign ml-2 mr-2\"><i class=\"far fa-trash-alt $trashiconclass trash\" id=\"".$row['ID']."\"></i></div>";	
	} else if(($row['PatientID'] == $_COOKIE['PatientID']) AND $row['User'] == "0"){
	$trashIcon = "<div class=\"showtrash $toolsalign ml-2 mr-2\"><i class=\"far fa-trash-alt $trashiconclass trash\" id=\"".$row['ID']."\"></i></div>";	
	} else{
	$trashIcon = "";	
	}
	if (is_null($row['ChatPhoto']) OR $row['ChatPhoto'] == ""){
	$fileicon = "";
	} else {	
	$fileicon = "<div class=\"showphoto $toolsalign\">&nbsp;<i class=\"fas fa-camera $photoiconclass photo\" id=\"".$row['ID']."\"></i>&nbsp;&nbsp;</div>";
	}	
	if (is_null($row['PainMap']) OR $row['PainMap'] == ""){
	$painmapicon = "";
	} else {	
	$painmapicon = "<div class=\"showpain $toolsalign\">&nbsp;<i class=\"fas fa-map-marked $painiconclass pain\" id=\"".$row['ID']."\" data-name=\"".$row['FullName']."\"></i>&nbsp;&nbsp;</div>";
	}
	if (is_null($row['Posture']) OR $row['Posture'] == ""){
	$postureicon = "";
	} else {	
	$postureicon = "<div class=\"showposture $toolsalign\">&nbsp;<i class=\"fas fa-user-check $painiconclass posture\" id=\"".$row['ID']."\" data-name=\"".$row['FullName']."\"></i>&nbsp;&nbsp;</div>";
	}
	echo "<div class=\"$clss\"><div class=\"direct-chat-infos clearfix\"><small class=\"$senderclss\">$sendername</small>&nbsp;&nbsp;<span class=\"$dateclss\">".$row['PostDate']."</span>&nbsp;$trashIcon&nbsp;</div><div class=\"direct-chat-text\">".$row['Message']."&nbsp;&nbsp;$fileicon$painmapicon$postureicon&nbsp;</div></div></div>";
	}
	}
?>

Hi,
I think the problem is related to session. We should start session at the beginning of custom file.
Isn’t it?Thanks

mansour wrote:

We should start session at the beginning of custom file. Isn’t it?

Yes. That’s why you should not include the common files yourself and bypass PHPMaker code.As explained in the other topic (https://discourse.hkvstore.com/t/converting-v2020-custom-files-to-v2021/3731/1):

If you just need to return some result of an action (e.g. a value by > ExecuteScalar() > or a row by > ExecuteRow()> ), it is better and simpler to use > Route_Action > server event, then you can define your parameters in the route and get the value in your code by > $args[“xxx”] > or > Route(“xxx”)> .

If you use Route_Action server event, you may define route with parameter, you can also pass the patient ID as route parameter and there is no need to use cookie. Instead of echo, just write your result to the response by:
$response->getBody()->write($myResult); // Write to response body