php 会话无法正常工作 [英] php sessions not working correctly

查看:36
本文介绍了php 会话无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我在从一个页面到另一个页面保持会话时遇到问题,代码在我以前运行 php5 的服务器上有效,但在我最近的服务器上无效,我想知道这是否是一个错误?

Hello i am having problems holding sessions from page to page, code worked on my previous servers running php5 but not on my recent server, i am wondering whether its a bug?

<?php
session_start();
$_SESSION['session'] = $_POST['session'];

header("location: www.mysite.com/page1.php");
?>


<?php
session_start();

echo "Good morning" . $_SESSION['session'];   //returns empty session always.
?>

有什么想法吗?会话在第一页上,但不在第二页上.

ANy ideas? session is held on first page but not on the second.

推荐答案

如果您错过了,请确保在您使用 $ 的每个页面上执行 session_start()_SESSION 变量.

In case you missed it, make sure you do a session_start() at every page you're using the $_SESSION variable.

您应该检查您的 php.ini 文件,看看发生了什么.

You should check your php.ini file and see what's going on.

确保 session.use_cookies = 1session.save_handler = files.

使用此测试页查看是一般 PHP 问题还是您的代码问题.

Use this test page to see whether it's a general PHP problem or just your code.

<?php
    session_start();

    if(isset($_SESSION)){
        echo "Session variable exists<br/>";

        if(!isset($_SESSION['test'])){
            $_SESSION['test'] = "Success!";
            echo "Variable has been set, refresh the page and see if stored it properly.";
        }else{
            echo $_SESSION['test'];
        }
    }else{
        echo "No session variable has been created.";
    }
?>

如果那行得通,那么这与您的代码有关.

If that worked, then it's got to do with your code.

如果您将会话变量设置为 $_POST['session'],我是否假设您提交了一个包含名称为 session 的输入的表单?

If you're setting your session variable to $_POST['session'] am I to assume you submitted a form with an input with the name session?

这个设置应该有效.

index.php

<form action='page0.php' method='POST'>
    <input type='hidden' name='session' value='SPAAAAACE' />
    <input type='submit' />
</form>

Page0.php

<?php
    session_start();
    $_SESSION['session'] = $_POST['session'];

    header("location: www.mysite.com/page1.php");
?>

Page1.php

<?php
    session_start();

    echo "Good morning" . $_SESSION['session'];
?>

<小时>

出于完整性和调试目的

如果您使用无 cookie 会话,您必须手动添加 SID (session id) 到这样的头部重定向


For completeness and debugging purposes

In case you are using cookie-less sessions, you have to manually add the SID (session id) to the header redirect like this

header("location: www.mysite.com/page.php?".htmlspecialchars(SID));

<小时>

如果问题仍然存在,则可能是权限问题.
可能你不允许读取存储在服务器上的会话文件?


If the problem still persists, it could be a permission issue.
Maybe you're not allowed to read the session file stored on the server?

更新:OP 评论说这是一个权限问题,问题现已解决

这篇关于php 会话无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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