2

Unable to get session from different PHP page other than where i initialized it

This is my first PHP page where i initialize the session.

<?php
   $i =1;

$team = htmlentities($_POST['team']);
$id = htmlentities($_POST['id1']);
$mobile = htmlentities($_POST['mobile1']);


if(isset($_POST['id2']))
{
    $ids = htmlentities($_POST['id2']);
    $mobiles = htmlentities($_POST['mobile2']);
 $i=2;   
}
if(isset($_POST['id3'])){
       $ids = $ids.','.htmlentities($_POST['id3']);
        $mobiles = $mobiles.','.htmlentities($_POST['mobile3']);
    $i=3;}
echo $team;
echo $i;

$connect =new mysqli('localhost', 'root', 'password','test');


if($connect->connect_error)
{
die("connection failed : ".$connect->connect_error);
}

$data = "INSERT INTO `Users`(`team_name`, `id`, `mobile`, `ids`, `mobiles`) VALUES ('$team','$id','$mobile','$ids','$mobiles')" ;
$createData="CREATE TABLE `$id`(
   `id` INT NOT NULL ,
   `ansOpChoosen` INT NOT NULL,
   `realAns` INT NOT NULL
   );";
echo 'pass';
$link ="/test.html";
$link2 = "/signups.html";
if(mysqli_query($connect,$data) && mysqli_query($connect,$createData) )
{ 
     session_start();
    $_SESSION['user'] = $id;  
header('Location: '.$link);    
echo "new record created successfully";
}

else{
   header('Location: '.$link2);    
  echo "error";
}

$connect->close();


?>

This is another php page where i try to retrive data but it doesnt fetch any thing

<?php
$id =    $_SESSION['user'];
$quesNo = $_POST['questionNo'];
$optionCho = $_POST['optionchoosen'];
$optionReal =$_POST['optionreal'];
echo $id;
//echo "hbbhkhb";
$connect =new mysqli('localhost','root','password`','test');
if($connect->error){
  echo "connection error";    
}
$check ="SELECT * FROM `$id` WHERE `id`=$quesNo";
if($res=mysqli_query($connect,$check)){
    $count = mysqli_num_rows($res);
    if($count>0)
    {
        $data ="UPDATE `$id` SET `ansOpChoosen`=$optionCho,`realAns`=$optionReal WHERE `id`=$quesNo";

    }
    else{
        $data = "INSERT INTO `$id`(`id`,`ansOpChoosen`,`realAns`) VALUES ($quesNo,$optionCho,$optionReal)";
    }
    $store=mysqli_query($connect,$data);

}

?>
karthick prasad
  • 103
  • 2
  • 9
  • How about to start the session again? – Rizier123 Jan 12 '15 at 17:57
  • possible duplicate of [How to use store and use session variables across pages?](http://stackoverflow.com/questions/5489365/how-to-use-store-and-use-session-variables-across-pages) – Rizier123 Jan 12 '15 at 17:58

3 Answers3

3

Put session_start(); at the top of every page that you want to use sessions on.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
1

You always have to call session_start() before doing something with the session.

session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.

Source: http://php.net/manual/en/function.session-start.php

Marcel Burkhard
  • 3,453
  • 1
  • 29
  • 35
0

A session is started with the session_start() function. Be careful : it must be top of every page.

For example :

<?php
session_start();
if (!isset($_SESSION['count'])) {
  $_SESSION['count'] = 0;
} else {
  $_SESSION['count']++;
}
?> 

Manual : http://php.net/manual/en/session.examples.basic.php

EngineerCoder
  • 1,445
  • 15
  • 30
  • Although be aware that what happens when you log in isn't the way that php interprets *starting a session*. Not saying you don't know this, it just that ```session_start``` is a bit misleading. – Marcel Burkhard Jan 12 '15 at 18:15