简体   繁体   English

PHP的会话存储行为成变量?

[英]PHP Session storing behavior into variables?

this is only happening in Chrome 这仅在Chrome中发生

Has anyone ever experienced PHP sessions storing functional behavior into variables? 有没有人经历过将函数行为存储到变量中的PHP会话? 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. 您可能认为这会在每次加载页面时回显0、1、2、3等。 However, I'm getting 1, 3, 5, 7, and so on. 但是,我得到1、3、5、7,依此类推。 I found out that for some reason, $_SESSION['count'] acts as though its incrementing behavior has been stored within the variable. 我发现由于某种原因,$ _ SESSION ['count']的行为就好像其递增行为已存储在变量中。 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. 它似乎在每个页面加载时增加2的原因是,当调用$ _SESSION ['count']时,它会自动增加1。 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. 这将回显任何$ _SESSION ['count'],然后为每个页面加载$ _SESSION ['count'] + 1。 I've tried unsetting the session, clearing the $_SESSION variables, and creating new files in various directories with the same script. 我尝试取消会话设置,清除$ _SESSION变量,并使用相同的脚本在各个目录中创建新文件。 I also tried it on http://codepad.viper-7.com/ , and it works correctly. 我也在http://codepad.viper-7.com/上尝试过,它可以正常工作。 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. 当我序列化或var_dump变量时,它只是返回一个字符串值。

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; IE和Firefox正确增加1; however Chrome increments by 2. Any idea why the browser could influence this? 但是Chrome会增加2。您知道为什么浏览器会对此产生影响吗?

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. 我注意到的是,每当我需要index.php中的此脚本时(例如index.php只是具有),我都会再次遇到此问题。 So, something's going on with index.php. 因此,index.php发生了一些事情。 Could there be an issue with my .htaccess loading the page more than once? 我的.htaccess可能会多次加载页面吗? Here it is: 这里是:

My .htaccess Options +FollowSymLinks 我的.htaccess选项+ 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. 再次编辑:根据注释中每个user2086860的请求,我将遍历完整的代码流。

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. 此代码可以正常工作,显示0、1、2、3等。

Now, if I require this script via my index.php like so: 现在,如果我通过index.php需要此脚本,如下所示:

<?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. 数字开始增加2而不是1。这是通过我的index.php引起的。 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: 它也弄糟了我的脚本文件,因此直接访问test6.php现在增加了2,而不是1。您可以在这里看到它的运行情况:

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. index.php或test6.php中没有其他内容。 This is only happening in Chrome. 这仅在Chrome中发生。 Firefox and Internet Explorer are working correctly, incrementing by 1. Firefox和Internet Explorer正常运行,增量为1。

Chrome (and apparently other browsers) was looking for a favicon, or the image that appears next to the page title. Chrome浏览器(显然还有其他浏览器)正在寻找收藏夹图标或页面标题旁边显示的图像。 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! 感谢@ dev-null-dweller提出的建议!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM