Question

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.

Was it helpful?

Solution

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!!";
 }
?>

OTHER TIPS

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!!";
}
?>

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!!";
}
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top