PHP会话存储行为到变量? [英] PHP Session storing behavior into variables?

查看:80
本文介绍了PHP会话存储行为到变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人经历过将PHP函数存储到变量中的PHP会话?我创建了以下脚本:

 <?php 
session_start();

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

echo $ _SESSION ['count'];
?>

您会认为这会在每页加载时回显0,1,2,3等。但是,我得到1,3,5,7等等。我发现出于某种原因,$ _SESSION ['count']的行为好像它的增量行为已被存储在变量中一样。它在每个页面加载似乎增加两倍的原因是因为当调用$ _SESSION ['count']时,它自动增加1。为了清楚起见,下面的脚本会输出,并且每个页面加载的数字都会更高。

 <?php 
在session_start();

echo $ _SESSION ['count'];
?>

这会回显$ _SESSION ['count']是,然后是$ _SESSION ['count'] +1每页加载。我尝试了取消设置会话,清除$ _SESSION变量,并使用相同的脚本在各个目录中创建新文件。我也在 http://codepad.viper-7.com/ 上试过了,它能正常工作。任何人都有一个想法,为什么会发生这种情况?当将会话变量设置为随机数时,我今天遇到类似的行为。在每一页加载时,我只需通过回显变量就可以得到一个新的随机数。当我序列化或var_dump变量时,它只是返回一个字符串值。



这是我用来取消会话的设置:

  $ _ SESSION = array(); 
session_unset();
session_destroy();

我在几个浏览器上试过这个脚本。 IE和Firefox正确地增加1;但Chrome浏览器增加了2.任何想法为什么浏览器可能会影响这个?编辑:如果我创建一个新的脚本并更改该变量,它可以正常工作。我注意到的是,无论何时我需要index.php中的脚本,例如index.php,我都会再次遇到这个问题。所以,index.php正在发生。我的.htaccess不止一次加载页面会出现问题吗?这里是:

我的.htaccess
选项+ FollowSymLinks

  RewriteBase 
$ b $ RewriteCond%{HTTPS}!
RewriteRule(。*)https://%{HTTP_HOST}%{REQUEST_URI}

DirectoryIndex /index.php
FallbackResource /index.php

#Allow跨域AJAX
标题集Access-Control-Allow-Origin *

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

编辑再次:每个用户在注释中的请求2086860,我将通过完整的代码流。



首先,我用以下代码创建脚本:

 <?php 
session_start();

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

echo $ _SESSION ['counter'];

?>

此代码本身按预期工作,打印0,1,2,3等。



现在,如果我需要通过我的index.php这样的脚本,如下所示:

 <?php 
require'test6.php';
?>

数字开始增加2而不是1,这需要通过我的index.php导致问题。它也将我的脚本文件搞砸了,因此直接访问test6.php现在直接增加2而不是1.你可以在这里看到它:

删除了测试网址

我清楚,上面的代码是 我的所有代码 即可。 index.php或test6.php中没有其他内容。这只在Chrome中发生。 Firefox和IE浏览器正常运行,增加1。

或页面标题旁边显示的图像。

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

它有效。感谢@ dev-null-dweller提出这个建议!


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.

解决方案

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!

这篇关于PHP会话存储行为到变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆