0

Development Environment/Local Host

The session data still exists after a page redirect using the header function.

Session Exists - Development Environment/Local Host

Production Environment/Hostinger

The session data does NOT exist after a page redirect using the header function.

Session Does NOT Exist - Production Environment/Hostinger

However, sessions work on other pages within the same folder/website where there is no redirect used.

Session Exists On Other Pages

Refer to the top left of the image to see the session data.

Therefore, sessions seem to work overall or somewhat on the production environment/Hostinger but not when there is a redirect.

I tried reaching out to Hostinger's customer service but still unclear on what's going on.

I tried the majority of the solutions outlined in a stackoverflow post labelled PHP session lost after redirect but not the solutions provided have worked.

I expected the session data to exist or persist but it always came up empty.

Code Used On Both Development Environment/Localhost & Production Environment/Hostinger

<?php

require_once 'dbConfig.php';

if(isset($_GET['id'])){

    echo "The id value from the URL is - ".$_GET['id'];
    echo "<br>";

    $_SESSION['last_event_id_management'] =  $_GET['id'];

    if(empty($_SESSION['last_event_id'])){
        echo "last_event_id is empty";
        echo "<br>";
    }else{
        echo "last_event_id is NOT empty ".$_SESSION['last_event_id']; 
        echo "<br>";
    }
    
    if(empty($_SESSION['last_event_id_management'])){
        echo "last_event_id_management is empty";
    }else{
        echo "last_event_id_management is NOT empty ".$_SESSION['last_event_id_management']; 
    }

    header("Location: $googleOauthURL");

}

if(isset($_GET['code'])){

    if(empty($_SESSION['last_event_id'])){
        echo "last_event_id is empty";
        echo "<br>";
    }else{
        echo "last_event_id is NOT empty ".$_SESSION['last_event_id']; 
        echo "<br>";
    }
    
    if(empty($_SESSION['last_event_id_management'])){
        echo "last_event_id_management is empty";
        echo "<br>";
    }else{
        echo "last_event_id_management is NOT empty ".$_SESSION['last_event_id_management']; 
    }

}

?>

UPDATE

After a lot of reading it does not seem to be possible to allow a cookie or session to continue existing after a redirect from a domain that one does not own (in this case Google Calendar) and back to your domain. The following are some of the posts I consulted and tried but the solutions did not work for me:

  1. Losing session after a redirect to another domain then back

  2. PHP session lost after redirect

  3. PHP session destroyed / lost after header

  4. PHP Session data lost after submitting form but only on production environment, test environment works

  5. Cross-Domain Cookies

The solution I've implemented is to create a text file and store the id value from the url inside of it. When the page redirects from Google Calendar and back to my domain, I will then access the contents of the text file that was created before the redirect. Finally, we will now be able to use that value to update the relevant row in the table.

Ozymandias
  • 39
  • 1
  • 7

0 Answers0