1

I am saving OTP value in SESSION and using it in other file. File is getting called by AJAX POST request.

Unfortunately it is not accessible in other file. Here is the code where session value getting store.

<?php
session_start();

error_reporting(E_ALL);

header("Access-Control-Allow-Origin: *");
header('Content-Type: application/json');
$mobile = '1234567890';
if(!empty($mobile)) {
    function generateOtp1() {
           return rand(1000, 9000);
       }
    $val = generateOtp1();
    echo $val;
    $_SESSION['otp'] = $val;
    print_r ($_SESSION);
}
?>

Here is the second file where I am calling $_SESSION['otp']:

<?php
session_start();
error_reporting(E_ALL);
header("Access-Control-Allow-Origin: *");
header('Content-Type: application/json');
$errors         = array();      // array to hold validation errors
$data           = array(); 

print_r ($_SESSION);

?>

It is showing empty in the output:

Array
(
)

This is the output I am getting. What am I doing wrong?

PHP is enabled in webserver.

Session Support enabled
Registered save handlers    files user
Registered serializer handlers  php_serialize php php_binary wddx

PHP version is 7.

halfer
  • 19,824
  • 17
  • 99
  • 186
Roxx
  • 3,738
  • 20
  • 92
  • 155
  • In the first file, what does `Print_r ($_SESSION);` print? and why the first letter is capital – Spoody Dec 16 '17 at 10:38
  • 1
    You have $mobile and $mob. Is it a mistake? – hans-könig Dec 16 '17 at 10:39
  • @MehdiBounya it is returning random value as per code. First file is totally fine i am getting the session output. – Roxx Dec 16 '17 at 10:42
  • in the first file you should `session_commit()` to write and close the actual file. – YvesLeBorg Dec 16 '17 at 10:42
  • @hans that was a typo. Please ignore. – Roxx Dec 16 '17 at 10:42
  • @YvesLeBorg i tried your suggestion but issue persist. – Roxx Dec 16 '17 at 10:45
  • Have you tested after correcting the typo? If the problem is still there, can you tell us how you call the two files? Do you use a real browser or some other client SW? Do you call both files via the same browser? Has the browser some special settings to remove cookies etc.? – Kağan Kayal Dec 16 '17 at 10:48
  • @KağanKayal Typo was in question not in the file. I am calling both file by Angular AJAX method. I am using google chrome and cookie is enabled. I am trying to fix the issue from 2 days but no success. – Roxx Dec 16 '17 at 10:51
  • 1
    You codes work well under my xampp/PHP7.1 environment. So the problem is from your environment. – Phil Dec 16 '17 at 10:56
  • There might be something strange about your server setup. See https://stackoverflow.com/questions/454635/where-are-session-variables-stored about where the session variables are stored and then you might be able to check if the problem is in that area. – Kağan Kayal Dec 16 '17 at 10:58
  • i checked with hosting provider and they said error in code not in hosting side. – Roxx Dec 16 '17 at 12:04
  • My last 5 cents would be to create your own web server environment on your own computer. Since @Phil has tested your code, I also believe the problem is not with the code itself, but the server environment. In case you see that your code works as desired, you can confront your hoster agin. Additionally, if you have ssh access to your host, you could check yourself what happens with the session variables (perhaps stored as files) instead of relying on the hosting support. – Kağan Kayal Dec 16 '17 at 13:03

0 Answers0