0

here is the ordeal:

on index.php

<?php
    session_start();
    $url = $_SERVER['HTTP_REFERER'];
    $_SESSION['abc'] ='$url';
?>

i echo on the page $_SERVER['HTTP_REFERER']; and i get what i want

later on site i have a reservation page and i use the session to

<?php 
    session_start();

    if (isset($_SESSION['abc'])) {
        echo $_SESSION['abc'];
    } 
    else {
        echo 'error';
    }               
?>

it echo's error and i'm confused why the data isn't passing..

Any idea's from the php gurus out there, thanks. Just want it to print the HTTP_REFERER

Kannika
  • 2,538
  • 2
  • 27
  • 38
  • is session_start() called on all of the pages? cause you say.."later" on site. – Andreas May 30 '12 at 23:37
  • 2
    Try to `print_r($_SESSION)` in the second page. And in `index.html`, replace `$_SESSION['abc'] = '$url'` with `$_SESSION['abc'] = $url`. – flowfree May 30 '12 at 23:39
  • @bsdnoobz: Why put quotes around `$url` at all? Quotes are not needed. – Asaph May 30 '12 at 23:40
  • do I have to call session_start() on every single page? or just on pages I want to use $_SESSION superglobal – Michael O'Brien May 30 '12 at 23:42
  • You need to call session_start() on every page that you would like to access session data. Once you have started the session, session_start() only resumes the previously started session. – Aventuris May 30 '12 at 23:50
  • The code works fine when I test it, so it seems likey your session is acting up. Are you swapping between http and https? Or subdomains (including www.)? – John C May 31 '12 at 00:00
  • Did you check if cookies are enabled on your browser? The domain cookie contains the session ID which is passed during every page opening. That ID is used in order to restore the session with the session_start() function. – bitfox May 31 '12 at 00:11
  • yes the reservation page is HTTPS, does this affect the ability to use sessions? – Michael O'Brien May 31 '12 at 00:12
  • yes it does - Pearse has jumped in with one solution, alternatively the question has been [answered](http://stackoverflow.com/questions/5921545/php-session-http-to-https-problem) [elsewhere](http://stackoverflow.com/questions/441496/session-lost-when-switching-from-http-to-https-in-php). (now an answer) – John C May 31 '12 at 00:46

2 Answers2

0

Looking at the comments, you appear to be using both HTTP and HTTPS.

You will need to pass a session ID from HTTP to HTTPS.

index.php

<?php
    session_start();

    $_SESSION['abc'] = $_SERVER['HTTP_REFERER'];

    $session_id = session_id();
?>

You will then need to pass $session_id to your reservation page, perhaps via GET:

reservation.php

<?php
    session_start();

    session_id($_GET['session_id']);

    if (isset($_SESSION['abc']))
        echo $_SESSION['abc'];
?>
  • Thank you so much! My only issue now is that i'm getting the referer as the page before my reservation page, not the initial HTTP_REFERER, so it echo's "mysite.com/pagebeforereservation" how do i get the initail HTTP_REFERER to transfer over? – Michael O'Brien May 31 '12 at 00:45
0

Unfortunately, by default a session won't persist between HTTP and HTTPS. Rather than rehash the solutions, I'll point towards where the question has come up before:

Community
  • 1
  • 1
John C
  • 8,223
  • 2
  • 36
  • 47