0

Nothing wrong with the code it works fine but on some templates not.

Here is how it works:

I store an array of templates into session and shuffle them only if the session is empty. Each time the page is reloaded, i pop an element of the session. So each time a template is included in the page, it pops out from the array

The issue here, on some templates the array_pop function pops 2 elements of the array on page reload (the included template + an other one).

I have tried to delete some code on the "problematic" templates but i cannot find a solution.

I need some help how to identify this issue.

session_start();
$templates = array("t1.php","t2.php","t3.php"); #list of templates paths

if (!isset($_SESSION['templates']) || empty($_SESSION['templates'])) {
    shuffle($templates); #shuffle them 
    $_SESSION['templates'] = $templates; #store them in sesssion
}

$currentTemplate = array_pop($_SESSION['templates']); #pops one on each page reload

include $currentTemplate; #includes the next template of the array
#on each page reload an element will be popped out and the next one will    be included, the issue, is that sometimes two elements-templates are popped out of the array.

I detected that it pops two elements by the below code:

    foreach($_SESSION['templates']  as $key=>$value)
        {
 echo 'The value of session['."'".$key."'".'] is '."'".$value."'".' <br />';
        }

Not reload

The value of session['0'] is 't3.php'

The value of session['1'] is 't2.php'

Reload 1:

On some templates my code works fine, i repeat. I don't know what is going on :)

  • You know that `array_pop()` changes the original array, right? It returns the value _and removes it from the array_. – Patrick Q May 08 '18 at 15:48
  • You need to show what is not working and how you think it is popping 2 and/or some debugging. – AbraCadaver May 08 '18 at 15:48
  • @AbraCadaver i updated my code to make you understand better. Note that the code is fine, this only happens on some templates and i need help how to identify the issue. –  May 08 '18 at 15:54

1 Answers1

1

Edit #3 - After offline discussion

Turns out JS was initiating a 2nd request (in the background) to the PHP script which was decrementing the stored templates in the session.

Specifically, it was the preloader which was looping over images that initiated the extra request to index.php.

img = document.images;
jax = img.length;

for(var i=0; i<jax; i++) {
    console.log(img[i].src);
} 

11:38:39.711 VM322:5 http://plrtesting.herokuapp.com/index.php **This one

11:38:39.711 VM322:5 https://i.stack.imgur.com/TtQqE.gif

End edit

The code is doing exactly what you're telling it to do.

$currentTemplate = array_pop($_SESSION['templates']);

You are removing, not retrieving, the final element and assigning it to your variable. Each time you reload the page it is popping 1 element off the array. That is why you are seeing it reduced over time.

You need to retrieve it instead. If you want the last element then:

session_start();
$templates = array("t1.php","t2.php","t3.php"); #list of templates paths

if (!isset($_SESSION['templates']) || empty($_SESSION['templates'])) {
    shuffle($templates); #shuffle them 
    $_SESSION['templates'] = $templates; #store them in sesssion
}

$currentTemplate = end((array_values($_SESSION['templates'])));

Edit #1 - Make it shuffle on every page load

Note that there are multiple ways to randomize the template. Take a look at this way - Get random item from array.

session_start();
$templates = array("t1.php","t2.php","t3.php"); #list of templates paths

// Commented out the if statement so it shuffles on each page load.
//if (!isset($_SESSION['templates']) || empty($_SESSION['templates'])) {
shuffle($templates); #shuffle them
$_SESSION['templates'] = $templates; #store them in sesssion
//}

$currentTemplate = end((array_values($_SESSION['templates'])));

var_dump($currentTemplate);

EDIT #2 - Not sure if it is clear or not but your code is looping over the remaining elements; not the popped element.

waterloomatt
  • 3,662
  • 1
  • 19
  • 25
  • I tried this but the template is static, it does not pop on each reload and the templates do not shuffle –  May 08 '18 at 16:09
  • That is because you are only `shuffle` on the first page-load when the session is empty. On subsequent page loads, the session is set already so it doesn't go into your `if` statement. – waterloomatt May 08 '18 at 16:10
  • I will edit my answer to make it `shuffle` on every page load. – waterloomatt May 08 '18 at 16:11
  • yes i noticed on my code, that when the array is empty. E.g. when all the templates have been popped, it is popping 1 element. –  May 08 '18 at 16:13
  • **It is NOT popping 2 elements. It is ALWAYS ONLY removing/popping 1 element. When you first start it is removing 1 from 3 which leaves 2 behind, and then you are looping of the 2 remaining elements. Then you reloaded again and it removed 1 from 2 which leaves 1... and so forth. Not sure if it is clear or not but your code is looping over the **remaining elements; not the popped element. – waterloomatt May 08 '18 at 16:18
  • By the way i only want it to shuffle when the array is empty. I want to see all templates before shuffling again. –  May 08 '18 at 16:28
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/170631/discussion-between-gragas-incoming-and-waterloomatt). –  May 08 '18 at 16:44
  • I found the issue. i can't believe it was Javascript preloader. I will add the code in the discussion chat that was included in the template and was causing the issue. –  May 09 '18 at 15:13