Google chrome - PHP Session storing behavior into variables

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

 this is only happening in Chrome  

Has anyone ever experienced PHP sessions storing functional behavior into variables? I created the following script:

<?php
    session_start();

    $_SESSION[ count ] = (isset($_SESSION[ count ])) ? $_SESSION[ count ] + 1 : 0;

   echo $_SESSION[ count ];
?>

You d think this would echo 0, 1, 2, 3, etc. on each page load. However, I m getting 1, 3, 5, 7, and so on. I found out that for some reason, $_SESSION[ count ] acts as though its incrementing behavior has been stored within the variable. The reason it seems to increase by two on each page load is because when $_SESSION[ count ] is called, it s automatically increasing by one. To make it clear, the following script will output and higher number on each page load.

<?php
    session_start();

    echo $_SESSION[ count ];
?>

http://codepad.viper-7.com/ http://codepad.viper-7.com/

This is what I used to unset the session:

$_SESSION = array();
session_unset();
session_destroy();

I ve tried the script on several browsers. IE and Firefox correctly increment by 1; however Chrome increments by 2. Any idea why the browser could influence this?

EDIT: If I create a new script and change the variable, it works correctly. What I ve noticed is that whenever I require this script from index.php, such that index.php simply has , I begin to have this problem again. So, something s going on with index.php. Could there be an issue with my .htaccess loading the page more than once? Here it is:

My .htaccess

   Options +FollowSymLinks
RewriteEngine on
RewriteBase /

RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

DirectoryIndex /index.php
FallbackResource /index.php

#Allow cross domain AJAX
Header set Access-Control-Allow-Origin *

RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]

EDIT AGAIN: Per user2086860 s request in the comments, I m going to run through the complete code flow.

First, I create a script with the following code:

 <?php
     session_start();

     $_SESSION[ counter ] = (isset($_SESSION[ counter ])) ? $_SESSION[ counter ] + 1 : 0;

     echo $_SESSION[ counter ];

 ?>

This code on its own works as expected, printing 0, 1, 2, 3, etc.

Now, if I require this script via my index.php like so:

<?php
    require  test6.php ;
?>

The numbers begin to increase by 2 rather than 1. It is this requiring via my index.php that causes the problem. It also screws up my script file, such that accessing test6.php directly now increases by 2 instead of 1. You can see it in action here:

    test URL removed     

Just so I m clear, the above code is all of my code . There is nothing else included in index.php or test6.php. This is only happening in Chrome. Firefox and Internet Explorer are working correctly, incrementing by 1.

Answers

Chrome (and apparently other browsers) was looking for a favicon, or the image that appears next to the page title. As soon as I added a stock image to my page like this:

<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" 
  type="image/png" 
https://mysite.com/icon.png
https://mysite.com/icon.png
</head>

It worked. Thanks to @dev-null-dweller for suggesting this!

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/19918256/php-session-storing-behavior-into-variables

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils