session value not updated within while loop in JOOMLA - php

I'm not very sure if this is Joomla or PHP issue in my code. Basically I've two functions, A and B that invoked on page load and onclick event respectively.
On page load, I set session value to loadA. Once click event triggered, I set session value to clickB.
Meanwhile the while loop inside function is running until the condition is met. Inside the loop, it's checking for session value. So if the value updated inside function B, it should also be updated in function A when the while loop check right?
But it's not giving the updated value. Function A still gives loadA as session value inside while loop even after click event took place. How to fix this please?
class XXY(){
public function A()
{
$session = JFactory::getSession();
//set session value on page laod
$this->setSession('loadA');
while (x == y)
{
usleep(10000);
clearstatcache();
$comet = $session->get('runningComet');//giving the first set value, loadA
if($comet !== 'loadA'){
break;
}
}
}
public function B()
{
$session = JFactory::getSession();
$this->setSession('loadB');
$comet = $session->get( 'runningComet');//giving the updated value, loadB
}
public function setSession($currentComet){
$session = JFactory::getSession();
$session->set('runningComet', $currentComet);
}
}

Related

CodeIgniter session holds value 1 instead of true

In my codeigniter application I have specified a session as below
$this->session->isLoggedIn = true;
echo "Session";
echo $this->session->isLoggedIn;
even though I assign true the output shows as 1 instead. What have I done wrong here?
would really appreciate some help
EDIT :- The problem was actually in the following
When I try to acess this session from another method the session value is set to null. My other function is defined as
public function isLoggedIn()
{
var_dump($this->session->isLoggedIn);
if ((isset($this->session->isLoggedIn)=="true"))
{
return "true";
}
else
{
return "Sorry,false";
}
}
the vardump that I am printing in this isLoggedIn function is Null even though the session variable was assigned as displayed in the initial code above

Trying to set a session variable with Zend_Session_Namespace, everytime NULL

I'm running an application where in the controller I'm trying to set session variables using Zend_Session and Zend_Session_Namespace:
Bootstrap.php
protected function _initSession()
{
Zend_Session::start();
}
SomeController.php
protected function updateQuestionViewsTotal($question)
{
$userSession = new Zend_Session_Namespace('QA_Session');
if (! is_array($userSession->questionViews)) {
$userSession->questionViews = array();
}
// create session array to contain the questions this
// user has viewed.
if(array_search($question->id, $userSession->questionViews) === false) {
$question->views_total++;
$question->save();
}
// ensure that this page is in the array
array_push($userSession->questionViews, $question->id);
$userSession->questionViews = array_unique($userSession->questionViews);
}
As you can see from above, I have within one of my controllers a method with an attempt to use session variables via Zend_Session_Namespace.
However, when I insert a var_dump on the second page load (refresh):
protected function updateQuestionViewsTotal($question)
{
$userSession = new Zend_Session_Namespace('QA_Session');
var_dump($userSession));
if (! is_array($userSession->questionViews)) {
$userSession->questionViews = array();
}
..Please note: this is AFTER I've run it once, so I'm expecting that the session variable has been set. Anyway on every occasion, it is NULL. So it would seem that the variable isn't being written to $_SESSION? What am I doing wrong?

PHP SESSION variable troubles

I'm sorry to trouble you, I have tried my best to solve this but I am not sure where I am going wrong and I was wondering if someone out there would be willing to help!
Basically, I am having some issues with $_SESSION variables; I would like for each occasion that a visitor came to the page that they would be shown a different content message.. The below code, when first landing on a page will seem to skip the first "content1", and will display "content2" instead, then "content3" after another revisit. I've put in an unset call, which eventually sends it there, am I not using _SESSIONS correctly?
I'm not sure how the session variable was assigned to 1, for it to land correctly in the if===1 statement without it first returning the original "content1"
if (empty($_SESSION)) {
session_start();
}
if (!isset($_SESSION['content'])) {
$content = "content1";
$_SESSION['content'] = 1;
return $content;
}
elseif ($_SESSION['content'] === 1) {
$content = "content2";
$_SESSION['content'] = 2;
return $content;
}
elseif($_SESSION['content'] === 2) {
$content = "content3";
unset($_SESSION['content']);
return $content;
}
Apologies for babbling or whether this was a simple fix / misunderstanding on my part. It's caused quite a headache!
Many thanks.
-edit-
This is a function that is called from within the same class, it has not gone through a loop anywhere either..
You are only calling session_start(); if the session has not been created.
What about the other times, when it's 1, or 2?
Call session_start(); regardless of your if (empty($_SESSION)) { statement
You should always use the session_start() function. If a session exists, it will continue it, otherwise it will create a new session.
Your code can then be simplified to the following:
// Start/Resume session
session_start();
// Get content
function display_message()
{
if ( ! isset($_SESSION['content']))
{
$_SESSION['content'] = 1;
}
if ($_SESSION['content'] == 1)
{
++$_SESSION['content']; // Increment session value
return 'Content #1';
}
elseif ($_SESSION['content'] == 2)
{
++$_SESSION['content']; // Increment session value
return 'Content #2';
}
elseif ($_SESSION['content'] == 3)
{
unset($_SESSION['content']); // Remove session value
return 'Content #3';
}
}
// Display content
echo display_message();
However, if someone visits your page a fourth time, they will be shown the first message again (because the session value is no longer tracking what they've been shown).
Perhaps this sort of functionality might be handled better with by using a cookie to track this information?

Order of execution in CodeIgniter

class Index extends CI_Controller {
private $data = array();
private $content_data = array();
public function __construct() {
parent::__construct();
if (isset($_GET['m2w'])) {
$stw = switch_to_web();
}
if (isset($_GET['w2m'])) {
$stm = switch_to_mobile();
}
// load mobile or desktop view
}
// Called within an helper function
function switch_to_web() {
return set_cookie('load-web', 'true', '86500');
}
function switch_to_mobile() {
return delete_cookie('load-web');
}
function is_mobile() {
// return true;
$CI = & get_instance();
if ($CI->input->cookie('load-web'))
return false;
$CI->load->library('user_agent');
if ($CI->agent->is_mobile()) {
return true;
}else
return false;
}
I have the above block of code to determine if to load the mobile or web view.
Expected order of execution:
if m2w is set, set the load-web cookie (this is done before the is_mobile function is called
is_mobile function sees the load-web cookie has been set and loads the desktop version
Actual order of execution:
if m2w is set, load-web cookie is called to be set, however the is_mobile function doesn't see it as set hence the desktop version is not loaded
the cookie is set after the is_mobile function has returned true, i check my browser cookies and observed that the cookie was actually set but not at when expected
What am I not getting right?
CI's cookies uses the native setcookie() method of PHP. Cookies requires to be sent in the HTTP headers before being available in the native $_COOKIE variable.
From the PHP doc :
Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS arrays.
Here's the cookie() method from CI :
function cookie($index = '', $xss_clean = FALSE)
{
// Simply fetch from the $_COOKIE array and do XSS_Clean if needed.
return $this->_fetch_from_array($_COOKIE, $index, $xss_clean);
}
In short, you set your cookie correctly, but it won't be available until your next request. It's all because the new value isn't in $_COOKIE array.
You have multiple alternatives to fix that.
You could extend the CI_Input class and modify the set_cookie method to also add your new value to the $_COOKIE array.
You could edit your is_mobile function to also check for the $_GET['m2w'] value. (Not just the cookie.)
Personalty, I think the 2nd solution is the cleanest and easiest to do, but I don't know the full scope of your project.
Hope this helps!

how do i check if a method has already been called?

I have a class that loads certain parts of my page. What I want to do is this
index.php
$obj->load('admin');
In my class in the constructor I have a check to see if the URI contains the segment /admin/ and if it does then it auto loads the admin page:
$this->load('admin');
now both work fine, I just realized that if I have both entries in my project then it will load the admin initialization twice.
Is there a way for my to check if the load() method has already been called and if the parameter is == to admin?
One way is to use static cache:
function load($what) {
static $loaded = array();
if (!isset($loaded[$what])) {
// Load..
// Mark as loaded
$loaded[$what] = true;
}
}
class Foo {
private $loaded = false;
function someFunc() {
$this->loaded = true;
}
function loaded() {
return $this->loaded;
}
}
May be Keep a object member variable variable and keep a flag when the load admin is called for first time. Check the flag for during the second call.
Easiest - create additional variable called say '$invokedAlready'. Set it first to 'false'. In your method place this:
if ( $invokedAlready )
{
// do sth
}
else
{
// first invocaion
// do sth else
$invokedFirst = true;
}

Categories