2

I have odd problem with session value. Here is my code:

      <?php
session_start();
if ((isset($_POST['potvrda'])) && ($_POST['potvrda'] === "potvrda")){
                $karte  = new karte($cijene[0]['jednosmjerna'], $cijene[0]['dnevna'], $cijene[0]['rijeka_jedna'], $cijene[0]['rijeka_pov'], $db);
                $data   = $karte->validate_form();
                $_SESSION['form_sub_data'] = array (
                              'datum' => $data[0][1]['date'],
                              'povratak' => $data[0][1]['datum_pov'],
                              'ime' => $data[0][1]['name']);

    if((empty($data[0][0])) && (!empty($_SESSION['form_sub_data']))){
              header("Location: " .SERVER ."purchase");
              die();
              }
    }

What it does is this: it checks if all form fields are filled propperly (class karte), my class returns array $data[0][0] that contains erros and $data[0][1] with proper values. Then I set all data from array [0][1] to $_SESSION, and if I var_dump() that session it's all there. Problem is after redirect. my $_SESSION is empty for some reason on second page.

On second page i have this code:

session_start();

if(empty($_SESSION['form_sub_data'])){header("Location: ".SERVER."");}

Now problem gets even more strange. Second page recives empty $_SESSION, it returns me to previous page, but when i fill form again. It all works as intended.

Tomek
  • 100
  • 2
  • 8
  • 5
    You have to call `session_start()` on **ALL** pages where you're reading/setting session data. If you didn't do that on your `isset()` code snippet, then anything you set in $_SESSION is lost when the script exits. – Marc B Feb 19 '14 at 19:00
  • But i do have session_start on top of both pages. – Tomek Feb 19 '14 at 19:02
  • Take a look at this: http://stackoverflow.com/questions/17242346/php-session-lost-after-redirect – sobyte Feb 19 '14 at 19:10
  • 1
    You also have to ensure that it's actually WORKING. If there's **ANY** output before the session_start() code, it will not work. Maybe it's just a cut&paste artifact, but you've got blank spaces before your opening ` – Marc B Feb 19 '14 at 19:10
  • I just noticed something, when i open my browser and when i go to my webpage, url is like: www.example.com, but as soon as i get to my webpage url becomes : example.com. And thats the problem with my session. is there any way i can fix that ? – Tomek Feb 19 '14 at 20:06

0 Answers0