PHP Session storing behavior into variables?

前提是你 提交于 2019-12-21 21:42:33

问题


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'];
?>

This will echo whatever $_SESSION['count'] was and then $_SESSION['count'] + 1 for every page load. I've tried unsetting the session, clearing the $_SESSION variables, and creating new files in various directories with the same script. I also tried it on http://codepad.viper-7.com/, and it works correctly. Anyone have an idea as to why this would happen? I've experienced similar behavior today when setting a session variable to a random number. On each page load, I get a new random number simply by echoing the variable. When I serialize or var_dump the variable, it simply gives back a string value.

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.


回答1:


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" 
  href="https://mysite.com/icon.png">
</head>

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



来源:https://stackoverflow.com/questions/19918256/php-session-storing-behavior-into-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!