首页 > 解决方案 > 会话值在 iframe 的第二页上变空

问题描述

我有一个网站,其中一个页面包含一个 iframe,该 iframe 显示来自同一网站的页面并且位于不同的文件夹中。有 7 页,我已经通过 jquery 的 load() 加载了每一页。我在会话中存储值并在下一页上访问它。但是当我试图在第二页上获取会话值时,它变成了空的。我在开头的每一页都写了 session_start() 。还尝试发送以下标题

header('P3P: CP="CAO PSA OUR"');

还是越来越空。

<?php 
    session_start();
    error_reporting(1);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Vision</title>
    <?php $baseurl ='http://www.localhost/xyz.com/'; ?>

    <link href="<?= $baseurl ?>catalog/view/theme/test/stylesheet/stylesheet.css" rel="stylesheet">
    <link href="<?= $baseurl ?>custom-lens/custom-lens.css" rel="stylesheet">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
    <style>
        .sec-active{border:2px solid red !important;}
    </style>
    <?php
        if($_REQUEST['prod_id']){   
            $_SESSION['pre_session_data_'.$_REQUEST['prod_id']]['Product_id'] = $_REQUEST['prod_id'];
        }else{  
            die("Invalid Request. Something went wrong. Please reload the url and try again.");
        }

以上是第一页上半部分的代码。

$( ".vision-parent" ).click(function(){
        $("#ajax-content").load("./category-ajax.php?vid="+vid+"&prod_id=<?php echo $_REQUEST['prod_id'];?>");
    }
    return false;
});

以上是第一页页脚第二部分的代码。

<?php session_start(); ?>
<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <?php
            
            error_reporting(1);
            echo "<pre>";print_r($_SESSION);die;
    
            if($_REQUEST['prod_id'] != $_SESSION['pre_session_data_'.$_REQUEST['prod_id']]['Product_id']){
                die("Invalid Request. Something went wrong. Please reload the url and try again.");
            }

以上是第二页的代码。

我无法找到问题。会话值显示在第 1 页上,但不在第 2 页上。

我打电话的网址是 http://www.localhost/xyz.com/custom-lens/vision.php?prod_id=286

请求的网址是 http://www.localhost/xyz.com/custom-lens/category-ajax.php?vid=1&prod_id=286

标签: phpajaxsession

解决方案


session_start 必须是在您的页面 session_start中调用的第一件事

Note:    To use cookie-based sessions, session_start() must be called before outputting anything to the browser.

推荐阅读