Undefined index: for variable when using session_start()

Hi Guys,

First I hope all people here are good!

I am learning PHP and want to pass one variable between two PHP files by using session_start(), I used it …But got a strange result!
when I access the link of php file in web browser I got the right result but when I used post man I got undefined index error!

the first file page1.php:

<?php
session_start();

$Sort_Number = 100;

        $_SESSION['Sort_Number'] = "$Sort_Number";
 
?>

in second file page2.php:

<?php
session_start();
$Sort_Number = $_SESSION['Sort_Number'];
echo $Sort_Number;
?>

I couldn’t figure out what is the issue here! why in web browser I got right result but when use postman not get it!

this error I got:

<br />
<b>Notice</b>: Undefined index: Sort_Number in
<b>/home/u277252404/domains/1234.tech/public_html/test/page2.php</b> on line <b>3</b><br />

**For more information I am using postman to test the link because I am going to use in Arduino code thats why :grin:

and that’ll be why you’re getting the bad result - postman isnt preserving your session between page loads, because it dumps cookie data inbetween requests.

Google turns up this link for me which may be of assistance: https://www.toolsqa.com/postman/share-session-id-across-different-requests-in-postman/

1 Like

Thanks @m_hutley
So you mean I have to use cookies to store the data in my php file?

No, when PHP starts a session it sends a cookie to the browser. The browser stores that cookie and then sends it along with every request so PHP can resume that same session.

Postman also receives the cookie but immediately forgets it and doesn’t send it along with new requests, so PHP has no way to know it should resume the same session.

The link @m_hutley posted shows you how to make Postman send the cookie so that PHP can resume the session.

1 Like

Thanks for nice explanation… :grin:
I simulated with postman because I am going to use it in Arduino, so I have to find another solution to achieve what I want! with session_start is not possible…Right?

Is that what it says to do in the article that @m_hutley linked to?

I would suggest you actually read the article @m_hutley linked to. It has all the answers you seek.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.