Home » Php » PHP session variable not pass

PHP session variable not pass

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have a problem with session variable, I can’t pass the session variable from one php page to others.

PHP version:

[[email protected] ~]# php --version
PHP 5.4.16 (cli) (built: Nov  1 2019 16:04:20)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies

Apache Version:

[[email protected] ~]# rpm -q httpd
httpd-2.4.6-90.el7.centos.x86_64

This is my code, index.php

<?php
ob_start();
session_start();
$_SESSION['logged']=null;

//index.php

//Include Configuration File
include('config/config.php');

$login_button = '';

//This $_GET["code"] variable value received after user has login into their Google Account redirct to PHP script then this variable value has been received
if(isset($_GET["code"]))
{
 //It will Attempt to exchange a code for an valid authentication token.
 $token = $google_client->fetchAccessTokenWithAuthCode($_GET["code"]);

 //This condition will check there is any error occur during geting authentication token. If there is no any error occur then it will execute if block of code/
 if(!isset($token['error']))
 {
  //Set the access token used for requests
  $google_client->setAccessToken($token['access_token']);

  //Store "access_token" value in $_SESSION variable for future use.
  $_SESSION['access_token'] = $token['access_token'];

  //Create Object of Google Service OAuth 2 class
  $google_service = new Google_Service_Oauth2($google_client);

  //Get user profile data from google
  $data = $google_service->userinfo->get();

  //Below you can find Get profile data and store into $_SESSION variable
  if(!empty($data['given_name']))
  {
   $_SESSION['user_first_name'] = $data['given_name'];
  }

  if(!empty($data['family_name']))
  {
   $_SESSION['user_last_name'] = $data['family_name'];
  }

  if(!empty($data['email']))
  {
    $_SESSION['user_email_address'] = $data['email'];
  }

  if(!empty($data['gender']))
  {
   $_SESSION['user_gender'] = $data['gender'];
  }

  if(!empty($data['picture']))
  {
   $_SESSION['user_image'] = $data['picture'];
  }
 }
}

//This is for check user has login into system by using Google account, if User not login into system then it will execute if block of code and make code for display Login link for Login using Google account.
if(!isset($_SESSION['access_token']))
{
 //Create a URL to obtain user authorization
$login_button = '<a href="'.$google_client->createAuthUrl().'"><img src="images/sign-in-with-google.png" /></a>';
 }
?>
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Log in/Sign up screen</title>
  <meta content='width=device-width, initial-scale=1, maximum-scale=1' name='viewport'/>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
  <link rel="stylesheet" href="css/style.css">
  <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,300,700' rel='stylesheet' type='text/css'>
 </head>
 <body>

<p id="hostname"></p>

        <script>
                document.getElementById("hostname").innerHTML =  "IP: node1 " + window.location.hostname;
        </script>

  <div class="container"> 

   <div class="frame">
    <div ng-app ng-init="checked = false">
        <h1><img align="center" src="images/logo_rouded.png" width="100" height="auto"></h1>


            <?php


            if($login_button == '')
            {
          //      echo "<form class=\"form-signin\" action=\"\" method=\"post\" name=\"form\">";
                echo "<p align=\"center\">Select your enviroment</p>";


                echo "<div class=\"btn-animate\">";
                echo "<a class=\"btn-signin\" onclick=\"window.open('https://test.mydomain.com/test.php','popUpWindow','height=650,width=1000,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes')\">MTEST</a>";
                echo "</div>";

                echo "<div class=\"btn-animate_signout\">";
                echo "<a class=\"btn-signin\" href=\"logout.php\">Sign out</a>";
                echo "</div>";
         //       echo "</form>";



            } else {
                echo "<p align=\"center\">Sign in with Activo Account</p>";
                echo '<div align="center">'.$login_button . '</div>';
                echo '<h3><b>logged :</b> '.$_SESSION['logged'].'</h3>';
            }


            ?>
          </div> 
 </div>

         <?php
            if($login_button == '')
            {
                 echo "<div class=\"panel panel-default\">";
                     echo "<div ng-app ng-init=\"checked = false\">";
                          echo '<div class="panel-heading">Welcome User</div><div class="panel-body">';
                          echo '<img src="'.$_SESSION["user_image"].'" class="img-responsive img-circle img-thumbnail" title="'.$_SESSION['user_last_name'].' '.$_SESSION['user_first_name'].'" style="width:150px;height:150px;"/>';
                          echo '<h3><b>Name :</b> '.$_SESSION['user_last_name'].' '.$_SESSION['user_first_name'].'</h3>';
                          echo '<h3><b>Email :</b> '.$_SESSION['user_email_address'].'</h3>';
                          $_SESSION['logged'] = true;
                          echo '<h3><b>logged :</b> '.$_SESSION['logged'].'</h3>';
                    echo "</div>"; 
                echo "</div>"; 
            }
        ?>
   </div>
 </body>
</html>

This is the page test.php that I try to pass the session variable $_SESSION[‘logged’]

<?php
ob_start();
session_start();

?>
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>test</title>
  <meta content='width=device-width, initial-scale=1, maximum-scale=1' name='viewport'/>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
  <link rel="stylesheet" href="css/style.css">
  <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,300,700' rel='stylesheet' type='text/css'>
 </head>
 <body>                 
            <?php    
            $logged = $_SESSION["logged"];
            echo "_SESSION[logged]: " . $logged . "</br>";

            if ($_SESSION['logged']==null) {
                echo "You are not login, please login!";
            } else {
                echo "<iframe width=\"100%\" height=\"100%\" src=\"https://test.mydomain.com:8080/index.html\"></iframe>";
            }           

            ?>
    </body>
</html>

when I call the page test.php I see that the session variable $_SESSION[‘logged’] is blank.

Where is my error?

UPDATE

I tried to vie the solution form this links, but I didn’t resolve my problem.

How to use store and use session variables across pages? (8 answers)

Reference – What does this error mean in PHP? (36 answers)

When I pass the variable between two pages, the receive page have always variable set to 0
For example, I created this test page index.php:

<?php
session_start();
$_SESSION['MYSESSIONTEST']="mysessionvalue";
?>
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>index.php -  test pass Session variable</title>
 </head>
 <body>
<a onclick="window.open('http://test.group-miki.com/passSession.php','popUpWindow','height=650,width=1000,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes')">Test pass session variable</a> 
<?php 
echo "</br>Session: " . $_SESSION['MYSESSIONTEST'];
?>
 </body>
</html>

And the second page, passSession.php:

<?php
session_start();
?>
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>passSession.php -  test pass Session variable</title>
 </head>
 <body>
<?php
if(isset($_SESSION['MYSESSIONTEST'])){
    echo "_SESSION[MYSESSIONTEST] exsist: " + $_SESSION['MYSESSIONTEST'];
} else {
    echo "_SESSION[MYSESSIONTEST] Not exsist: " + $_SESSION['MYSESSIONTEST'];
}
?> 
 </body>
</html>

The print out on the passSession.php is only 0

This is my /etc/php.ini
enter image description here

Update2

It’s very strange, now in the passSession.php I can see the session variable, but in the test.php I can’t see it, with this error:

Notice: Undefined index: logged in /var/www/html/googleaccess/mtest2.php on line 18
_SESSION[logged] Not exsist: 

Both site running on the same web server (in different path).

Update 3:

I noticed that when I click on the link to open the page https://test.mydomain.com/test.php the system create a new file for this session in /var/lib/php/session and not use the file generated on the first page (index.php)

How to&Answers: