John smith3
John smith3

Reputation: 149

PHP - Session variables not saving from page to page

BEFORE YOU MARK THIS AS DUPLICATE, I have read through all the answers on this topic and Non of them worked for me, this is why I am posting this.

So the problem is that the data for $_SESSION is not saving from page to page. Here is my test:

TestOne.php

<?php
session_start();

$_SESSION["user_id"] = 1;


if(isset($_SESSION["user_id"])) {
      header("Location: TestTwo.php");
}

?> 

TestTwo.php

<?php


if(isset($_SESSION["user_id"])) {
    echo $_SESSION["user_id"];
}


?>

It goes to page two but it is a blank page. Why is the data not saving from page to page? session_save in the php.ini is set to /tmp (I am using hostgator)

Upvotes: 2

Views: 1546

Answers (3)

You are missing session_start(); on your TestTwo.php

FYI : You need to call session_start(); on all of your PHP files, if you are making use of Sessions.

I have read through all the answers on this topic and Non of them worked for me, this is why I am posting this.

Really caught my attention btw.

Upvotes: 4

Bhadra
Bhadra

Reputation: 2104

for using session variables, u need to use session_start() before that

 session_start();
 if(isset($_SESSION["user_id"])) {
    echo $_SESSION["user_id"];
 }

Upvotes: 2

Michael
Michael

Reputation: 12806

You need session_start() on every page that requires the session.

Upvotes: 1

Related Questions