0

I created two files 1.php 2.php which are in the same folder(i am using xampp). In 1.php used session_start() and also used $_session['name']=abc. Then i opened 2.php to check whether session was created or not

2.php:

<?php

 if(isset($_session['name'])){

  echo $_session['name'];
 }
  else{
  echo "no session found!!";

  }
   ?>

and it keeps on saying "no session found!!"

Plz help ...

I searched a few sites n they say that by default d session is for whole folder containing d script and session_set_cookie_params($lifetime,'/') (where $lifetime=60*60) is also nt helping. On d other hand if at d end of1.php i use require("2.php") then abc is displayed.

Ranjan
  • 263
  • 2
  • 11

3 Answers3

2

What you have done is right in 1.php, however, 2.php must start the session before using it.

2.php

<?php
 session_start();
 if(isset($_SESSION['name'])) {
    echo $_SESSION['name'];
 }
 else{
     echo "no session found!!";
 }
?>
Jake1164
  • 12,062
  • 6
  • 41
  • 61
Ranjan
  • 263
  • 2
  • 11
1

You need to call session_start(); again at the top of every page where you want to access $_SESSION variables, not only on the page where you want to initiate the session.

<?php
session_start();
if(isset($_SESSION['name'])){
    echo $_SESSION['name'];
}else{
    echo "no session found!!";
}
?>
Mooseman
  • 18,150
  • 12
  • 67
  • 91
  • The session variable is still ALL UPPERCASE, not lowercase: `$_SESSION` - and this is mandatory. – Sven Jul 06 '13 at 11:39
1

You're missing session_start() at the top of your 2.php file which is needed to access $_SESSION variables.

<?php
session_start(); // missing
if(isset($_SESSION['name']))
{
    echo $_SESSION['name'];
}
else
{
    echo "no session found!!";
}
?>
Daniel Kmak
  • 16,209
  • 7
  • 65
  • 83