Rishi Saravadaa
Rishi Saravadaa

Reputation: 31

Session values getting empty on second page in Iframe

I am having a website where one page includes an Iframe which shows a page from the same website and is in a different folder. There are 7 pages and I have loaded each page by load() of jquery. I am storing values in session and accessing it on the next page. But when I am trying to get the session values on second page it becomes empty. I have written session_start() in every page at the beginning. Also tried sending below header

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

Still getting empty.

<?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.");
        }

Above is the code of upper part of 1st page.

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

Above is the code of 2nd part at footer on 1st page.

<?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.");
            }

Above is the code of 2nd page.

I am not able to find the issue. The session value is displaying on the 1st page but not on 2nd page.

The url from which I am calling is http://www.localhost/xyz.com/custom-lens/vision.php?prod_id=286

The requested url is http://www.localhost/xyz.com/custom-lens/category-ajax.php?vid=1&prod_id=286

Upvotes: 0

Views: 136

Answers (1)

CharlesM
CharlesM

Reputation: 19

session_start must be the first thing to call in your page session_start

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

Upvotes: 1

Related Questions