I'm using joomla 3++.
I'm calling a method from my component's class in my system plugin. Prior to calling this method, I wish to check if it's already been called?
public function onAfterInitialise()
{
MyClass::initialize();
}
if (class_exists('MyClass'))//always true when navigate among different components
if (method_exists('MyClass', 'initialize'))//always true also
var_dump(MyClass::initialize());//true
The problem I'm facing:
My system plugin is not running on other components if I don't call this MyClass::initialize().
At the same time, one of the components says a js file is already loaded if I called MyClass::initialize() at onAfterInitialize()
So I'm thinking probably to avoid a
xx.js file loaded already issue,
I could check first this MyClass::initialize() called or not.
you are calling MyClass::initialize() method right?, in that method use
JFactory::getSession()->set('init_called',1,'your_component');
in system plugin you can check
public function onAfterInitialise()
{
$is_called = JFactory::getSession()->get('init_called','','your_component');
if($is_called){
//already called, do any other code
}else{
// Not yet called, do any other code
}
}
Now you can check that function already called, make sure session will not clear until logout. any other place clear session use this
JFactory::getSession()->clear('init_called','your_component');
Related
I'm creating a WordPress plugin, and by debugging, I noticed its being called twice on each request. My plugin code is like this:
class Br1E_EngenhariaPlugin{
(...)
}
new Br1E_EngenhariaPlugin(); // A breakpoint here is called twice on each request
Debugging the code I see that the problem is that the wp-load.php file is being called twice. The first time the callstack started at index.php:
If I hit continue, it will stop again at the same breakpoint, this time the callstack started at wp-cron.php:
I tried to make my class a singleton, by using a static variable to ensure the class is loaded only once:
class Br1E_EngenhariaPlugin{
public static function LoadOnce() : Br1E_EngenhariaPlugin
{
if (self::$pluginInstance == null)
self::$pluginInstance = new Br1E_EngenhariaPlugin();
//register_activation_hook( __FILE__, array($br1Engenharia, 'install') );
return self::$pluginInstance;
}
(...)
}
Br1E_EngenhariaPlugin::LoadOnce();
But it didn't work. The $pluginInstance static variable is null on the second time it's called, it's like it's a different request.
And yes, it is a different HTML request called from spawn_cron() in the file wp-includes/cron.php:
$result = wp_remote_post( $cron_request['url'], $cron_request['args'] );
Here $cron_request['uri'] contains http://your-site.com/wp-cron.php. And you can see it in the second call stack.
wp_cron in the file wp-includes/cron.php is hooked on every' init' event. Then, if any cron jobs are to be executed, spawn_cron() is called, producing an independent HTML request to the site.
I'm wondering (I have full access to the serve in case it's a php.ini setting) if there's anyway to "disable parsing of functions if a function was already defined" instead of throwing an error/notice about it?
For example /www/main/deep/file/file.php has:
function homepage_filter_get_map () {
// example1
$generic_filter_array = td_generic_filter_array::get_array();
}
and in /www/main-child/custom.php has, which is called/included/parsed before the file above:
function homepage_filter_get_map () {
//do nothing
}
Essentially, I'm looking for a way to suppress any and all errors outputting about already defined functions while silently ignoring functions that might have the same exact name, but already parsed/defined.
My problem is that the theme I'm using doesn't have full support for Wordpress child themes, just loop files mainly. I know I can just tweak the original files but I want the ability to be able to keep the theme updated without erasing all of our custom tweaks every-time.
Note: Yes, I know you can conditionally call functions, but again I'm looking for a way to do this without editing any of the "main" files since any tweaks done to those get overwritten when updating the parent theme.
if(!(function_exists('homepage_filter_get_map'))){
function homepage_filter_get_map(){
//do code
}
}
This checks if the function 'homepage_filter_get_map' exists. If it doesn't, create the function.
I'm building my own MVC framework in order to learn the ropes properly.
Ive managed to get a login system working, but sessions dont seem to be persisting across page changes.
Ive done some reason reading around and am running session_start() in the controller as a few people seem to be directing.
On login, my processLogin method runs successfully and stores the session data as expected. I know this has happened because Im doing a var_dump on it in the main header file and its there when the login form loads (im not destroying it at any point).
The trouble I have is when it comes to do a location change after successful login, it runs the 'gallery' method, the session array is still there, but empty.
Its exasperating and Id really appreciate any help.
Heres my extended controller class for reference:
session_start();
class Home extends Controller {
public function index() {
require 'application/views/_templates/header.php';
require 'application/views/home/index.php';
require 'application/views/_templates/footer.php';
}
// login function (validation carried out client side)
public function processLogin() {
if (isset($_POST['loginUsername'])) {
$home_model = $this->loadModel("HomeModel");
$home_model->processLogin($_POST['loginUsername'], $_POST['loginPassword']);
}
}
public function gallery() {
require 'application/views/_templates/header.php';
require 'application/views/home/gallery.php';
require 'application/views/_templates/footer.php';
}
}
First thing you should use session_start() at the beginning of main file, usually index.php and not in Controller (because we don't know how your framework is build.
You should make sure that everywhere in your webpage you use the same domain - for example with www. or without www. Otherwise you should use session_set_cookie_params() to set it other way (for example for all subdomains)
I having issues with Codeigniter sessions dying on IE randomly, I search everywhere and tried everything, the bug just wouldnt dissappear, i tried the function to check if ajax and wont sess_update() not working either, so my question is, what is the setback if I initialize the CI session every controller call? I have both native and CI sessions, but It would take me a few more days to change everything to Native sessions. its a temp fix.
class Transactions extends Controller {
function Transactions()
{
session_start();
parent::Controller();
$this->load->model('Modelcontracts');
$this->load->model('Modelsignup');
$this->load->model('Modeltransactions');
$this->session->set_userdata('account_id',$_SESSION['account_id']);
$this->session->set_userdata('email',$_SESSION['email']);
$this->session->set_userdata('account_type',$_SESSION['account_type']);
$this->session->set_userdata('parent_account_id',$_SESSION['parent_account_id']);
$this->session->set_userdata('accountrole_id',$_SESSION['accountrole_id']);
$this->session->set_userdata('user_type_id',$_SESSION['user_type_id']);
}
function index()
{
I never experience any problems with CodeIgniters sessions. Have you created the MySQL table for ci_sessions?
The setback is basicly that it's an unlogical call. If that doesn't matter, then I can't see any setbacks with it.
You could ease up the code like this though:
$arr = array('account_id', 'email', 'account_type', 'parent_account_id', 'accountrole_id', 'user_type_id');
foreach($arr as $h)
if (isset($_SESSION[$h]))
$this->session->set_userdata($h, $_SESSION[$h]);
// else echo "Session [{$h}] doesn't exist!";
Or extend your session library to do a
foreach(array_keys($_SESSION) as $h)
$this->CI->session->set_userdata($h, $_SESSION[$h]);
When loaded.
I don't think you should be using session_start() if you're having CodeIgniter manage your sessions (which you are if you're using CodeIgniter's set_userdata() / get_userdata() functions).
It says right at the top of the CI user docs that CI doesn't use PHP's native session handling, so this may be causing you trouble. The session is started automatically by loading the session library, either automatically if you put it in the config file or explicitly with $this->load->library('session');.
http://codeigniter.com/user_guide/libraries/sessions.html
-Gus
Edit: I came across a CI forum post regarding IE/CI session issues. Apparently it's a well-known issue. http://codeigniter.com/forums/viewthread/211955/
I am learning how to use codeIgniter as my php framework. I am reading through the documentation and watching the intro video and just generally following along with the first tutorial, but it's not working for me.
I have created a controller called "test.php" and a view called "test_view". The controller for this class is exactly like "welcome.php" and the view file just has some static html. However, when I go to index.php/test I get a 404 error.
I have also tried manipulating the original welcome files so that instead of calling a view it just echos "testing", yet I still see the original welcome message! I've tried clearing my browsing cash and refreshing, but to no avail.
Any suggestions? Thanks.
Edit: Here's the code for controllers/test.php
<?php
class Test extends Controller {
//Just trying to get it to echo test
public function index()
{
echo "test";
//$this->load->view('test_view');
}
}
?>
Try looking at this page in the documentation - this might solve your problem.
This basically means you should try typing index.php?/test/ instead (notice the question-mark).
First of all, check the above link. Might be useful.
If not, then...
Try changing the default controller in the config file ('routes.php') to something else (probably, to 'test'), then try loading index.php. Just to test whether the whole system works (or not).
Check whether mod_rewrite is loaded (in your server .conf-file, if you're using Apache).
Try using the latest build of the framework. AFAIK, the name of the controller class for now is "CI_Controller".
Finally, try removing the word 'public' before the declaration of the function. AFAIR, CI enable you to make private functions in controllers just by using prefix (which is set in the config file) at the beginning of the name of the function (thus making all the other functions public).
But most certainly the problem is with the mod_rewrite. If not, try debugging with die('Page found'); instead of echo - this will allow you to track possible redirects on the page.