This time we will customize the PHPMaker template in order to save the last users' visitted page. In other words, when your user is visitting the certain page in your web application, system will save that last visitted page which he/she has just visitted into your users table. This is very important, since we can manage this information for various uses. For example, when your user login back in the future, he/she will be redirected back to the last visitted page he/she visitted. Another example: you are able to redirect your user back to the last visitted page after he/she has done some actions in your web application which located in another page than he/she is visitting without having do the logout process.
I have successfully created this redirect to last page feature into websites that generated by PHPMaker. Now you are also able to define whether you want to enable or disable this feature by simple adjusting the related constant in your configuration (ewcfg*.php) file. Here is the conditions how you should enable or disable this feature. You should enable this redirect back to the last visitted page feature when you give the flexibility to your users to go back to their last visitted page. In the other side, you should disable this redirect to last visitted page feature when you want to force all of your users by redirecting them to the certain very important page other than their own last visitted page.
For that both cases, the constant value in the configuration file will overide the related setting in their user setting preferences. Now the control is in your hand. In the next article, I will show you how you can use this users' last visitted page for another important purpose.
[hidepost]
-
First of all, we have to alter the users table by adding two new fields:
- current_url (text)
- redirect_to_last_visitted_page_after_login (type=enum; values='Y', 'N'; default='Y') -
Open your /Script/ewcfg.php file, and find this code:
define("EW_USER_ID_FIELD", "<!--##=sFld##-->", TRUE);If you do not find it, then add the following code:
<!--## If PROJ.SecLoginIDFld <> "" Then Set FIELD = SECTABLE.Fields(PROJ.SecLoginIDFld) sFld = ew_FieldName End If ##--> define("EW_USER_ID_FIELD", "<!--##=sFld##-->", TRUE);before the following code:
define("EW_USER_NAME_FILTER", "<!--##=ew_Quote(sFilter)##-->", TRUE); -
Still in the /Script/ewcfg.php file, find again this code:
// Show SQL for debug define("EW_DEBUG_ENABLED", <!--##=ew_Val(PROJ.GetV("Debug"))##-->, TRUE); // TRUE to debug if (EW_DEBUG_ENABLED) error_reporting(-1); // Report all PHP errorsafter the last line of that code, please insert this following code:
// Redirect to URL after login define("EW_REDIRECT_TO_LAST_VISITTED_PAGE_AFTER_LOGIN", TRUE, TRUE); // whether to redirect to the last url/page or not -
Open your /Script/phpcommon-scripts.php file, and find this code:
<!--##~SYSTEMFUNCTIONS.Security##-->
after that line, please insert this following code:
// Begin of saving the last URL that visitted by users, by Masino Sinaga, December 11, 2011 $lastpage = ew_CurrentPage(); if ($lastpage!='logout.php' && $lastpage!='index.php') { $lasturl = ew_CurrentUrl(); $conn->Execute("UPDATE ".EW_USERS_TABLE." SET current_url = '".$lasturl."' WHERE ".EW_USER_ID_FIELD." = '".CurrentUserName()."'"); } // End of saving the last URL that visitted by users, by Masino Sinaga, December 11, 2011 -
The next step is, we will define the main of this customization. Open your PHPMaker project using PHPMaker application, and go to Server Events/Client Scripts -> Other -> Default Page -> Page_Redirecting, and then add this following code into your Page_Redirecting function (if you have never change this function, then the complete code should be like the following:
// Page Redirecting event function Page_Redirecting(&$url) { global $Security; if (!$Security->IsLoggedIn()) { $url = "login.php"; } else { global $conn; $sSql = "SELECT current_url, redirect_to_last_visitted_page_after_login FROM ".EW_USERS_TABLE." WHERE ".EW_USER_ID_FIELD." = '".CurrentUserName()."'"; $rs = $conn->Execute($sSql); if ($rs->fields("current_url")!="") { if (EW_REDIRECT_TO_LAST_VISITTED_PAGE_AFTER_LOGIN) { if ($rs->fields("redirect_to_last_visitted_page_after_login")=="Y") { $webdomain = ew_DomainUrl(); $url = $webdomain . $rs->fields("current_url"); } } } } } - Finally, re-generate all of your php script files using PHPMaker.
[/hidepost]
[…] Tips and Trick Home» Customize Template» How to Add Multi Themes Feature in the Websites That Generated by PHPMaker 8.0.3 How to Save The Last Users’ Visitted Page in Websites That Created with PHPMaker 8.0.3 […]