2

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)

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

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

1 Comment

yesvoteless as Bhadra says you need a session_start() at the beginning of EVERY page
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.